24 #ifndef _H_CHOLESKY_HPP_ 25 #define _H_CHOLESKY_HPP_ 30 #include <boost/numeric/ublas/vector.hpp> 31 #include <boost/numeric/ublas/vector_proxy.hpp> 33 #include <boost/numeric/ublas/matrix.hpp> 34 #include <boost/numeric/ublas/matrix_proxy.hpp> 36 #include <boost/numeric/ublas/vector_expression.hpp> 37 #include <boost/numeric/ublas/matrix_expression.hpp> 39 #include <boost/numeric/ublas/triangular.hpp> 45 namespace ublas = boost::numeric::ublas;
56 template <
class MATRIX,
class TRIA >
59 using namespace ublas;
61 typedef typename MATRIX::value_type T;
63 assert( A.size1() == A.size2() );
64 assert( A.size1() == L.size1() );
65 assert( A.size2() == L.size2() );
67 const size_t n = A.size1();
69 for (
size_t k=0 ; k < n; k++) {
71 double qL_kk = A(k,k) - inner_prod( project( row(L, k), range(0, k) ),
72 project( row(L, k), range(0, k) ) );
77 double L_kk = sqrt( qL_kk );
80 matrix_column<TRIA> cLk(L, k);
81 project( cLk, range(k+1, n) )
82 = ( project( column(A, k), range(k+1, n) )
83 - prod( project(L, range(k+1, n), range(0, k)),
84 project(row(L, k), range(0, k) ) ) ) / L_kk;
98 template <
class MATRIX >
101 using namespace ublas;
102 typedef typename MATRIX::value_type T;
104 const MATRIX& A_c(A);
106 const size_t n = A.size1();
108 for (
size_t k=0 ; k < n; k++) {
110 double qL_kk = A_c(k,k) - inner_prod( project( row(A_c, k), range(0, k) ),
111 project( row(A_c, k), range(0, k) ) );
116 double L_kk = sqrt( qL_kk );
118 matrix_column<MATRIX> cLk(A, k);
119 project( cLk, range(k+1, n) )
120 = ( project( column(A_c, k), range(k+1, n) )
121 - prod( project(A_c, range(k+1, n), range(0, k)),
122 project(row(A_c, k), range(0, k) ) ) ) / L_kk;
130 using namespace ublas;
137 template<
class E1,
class E2>
138 void inplace_solve (
const matrix_expression<E1> &e1, vector_expression<E2> &e2,
139 lower_tag, column_major_tag) {
140 std::cout <<
" is_lc ";
141 typedef typename E2::size_type size_type;
142 typedef typename E2::difference_type difference_type;
143 typedef typename E2::value_type value_type;
145 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
146 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
147 size_type size = e2 ().size ();
148 for (size_type n = 0; n < size; ++ n) {
149 #ifndef BOOST_UBLAS_SINGULAR_CHECK 150 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type(), singular ());
152 if (e1 () (n, n) == value_type())
153 singular ().raise ();
155 value_type t = e2 () (n) / e1 () (n, n);
157 if (t != value_type()) {
158 project( e2 (), range(n+1, size) )
159 .minus_assign( t * project( column( e1 (), n), range(n+1, size) ) );
174 template <
class MATRIX >
177 using namespace ublas;
179 typedef typename MATRIX::value_type T;
182 const MATRIX& A_c(A);
184 const size_t n = A.size1();
186 for (
size_t k=0 ; k < n; k++) {
188 double qL_kk = A_c(k,k) - inner_prod( project( row( A_c, k ), range(0, k) ),
189 project( row( A_c, k ), range(0, k) ) );
194 double L_kk = sqrt( qL_kk );
197 for (
size_t i = k+1; i < A.size1(); ++i) {
198 T* Aik = A.find_element(i, k);
201 *Aik = ( *Aik - inner_prod( project( row( A_c, k ), range(0, k) ),
202 project( row( A_c, i ), range(0, k) ) ) ) / L_kk;
220 template <
class TRIA,
class MATRIX >
224 using namespace ublas;
226 inplace_solve(L, x, lower_tag() );
227 inplace_solve(trans(L), x, upper_tag());
238 template <
class Min,
class Mout >
242 typedef typename Mout::value_type value_type;
244 size_t size = M.size1();
248 if (res != 0)
return res;
250 inverse.assign(ublas::identity_matrix<value_type>(size));
263 template <
class TRIA,
class VECTOR >
266 using namespace ublas;
267 typedef typename TRIA::value_type T;
269 assert( L.size1() == L.size2() );
270 assert( L.size1()+1 == v.size() );
272 const size_t n = v.size();
277 for (
size_t j = 0; j < n-1; ++j)
279 L_j = v(j) - inner_prod(project (row(L, j), range(0,j)),
280 project (row(L,n-1), range(0,j)));
281 L(n-1,j) = (L_j) / L(j,j);
283 L_j = v(n-1) - inner_prod(project (row(L, n-1), range(0,n-1)),
284 project (row(L,n-1), range(0,n-1)));
285 L(n-1,n-1) = sqrt(L_j);
void cholesky_add_row(TRIA &L, const VECTOR &v)
decompose the symmetric positive definit matrix A into product L L^T.
Namespace of the library interface.
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
void cholesky_solve(const TRIA &L, MATRIX &x, ublas::lower)
solve system L L^T x = b inplace
size_t incomplete_cholesky_decompose(MATRIX &A)
decompose the symmetric positive definit matrix A into product L L^T.
size_t inverse_cholesky(const Min &M, Mout &inverse)
Computes the inverse matrix of a symmetric positive definite matrix.