23 #ifndef _H_CHOLESKY_HPP_
24 #define _H_CHOLESKY_HPP_
29 #include <boost/numeric/ublas/vector.hpp>
30 #include <boost/numeric/ublas/vector_proxy.hpp>
32 #include <boost/numeric/ublas/matrix.hpp>
33 #include <boost/numeric/ublas/matrix_proxy.hpp>
35 #include <boost/numeric/ublas/vector_expression.hpp>
36 #include <boost/numeric/ublas/matrix_expression.hpp>
38 #include <boost/numeric/ublas/triangular.hpp>
51 template <
class MATRIX,
class TRIA >
54 using namespace ublas;
58 assert(
A.size1() ==
A.size2() );
59 assert(
A.size1() ==
L.size1() );
60 assert(
A.size2() ==
L.size2() );
62 const size_t n =
A.size1();
64 for (
size_t k=0 ;
k <
n;
k++) {
66 double qL_kk =
A(
k,
k) - inner_prod( project( row(
L,
k), range(0,
k) ),
67 project( row(
L,
k), range(0,
k) ) );
72 double L_kk = sqrt( qL_kk );
75 matrix_column<TRIA> cLk(
L,
k);
76 project( cLk, range(
k+1,
n) )
77 = ( project( column(
A,
k), range(
k+1,
n) )
78 - prod( project(
L, range(
k+1,
n), range(0,
k)),
79 project(row(
L,
k), range(0,
k) ) ) ) / L_kk;
93 template <
class MATRIX >
96 using namespace ublas;
100 const MATRIX& A_c(
A);
102 const size_t n =
A.size1();
104 for (
size_t k=0 ;
k <
n;
k++) {
106 double qL_kk = A_c(
k,
k) - inner_prod( project( row(A_c,
k), range(0,
k) ),
107 project( row(A_c,
k), range(0,
k) ) );
112 double L_kk = sqrt( qL_kk );
114 matrix_column<MATRIX> cLk(
A,
k);
115 project( cLk, range(
k+1,
n) )
116 = ( project( column(A_c,
k), range(
k+1,
n) )
117 - prod( project(A_c, range(
k+1,
n), range(0,
k)),
118 project(row(A_c,
k), range(0,
k) ) ) ) / L_kk;
126 using namespace ublas;
133 template<
class E1,
class E2>
134 void inplace_solve (
const matrix_expression<E1> &e1, vector_expression<E2> &e2,
135 lower_tag, column_major_tag) {
136 std::cout <<
" is_lc ";
137 typedef typename E2::size_type size_type;
138 typedef typename E2::difference_type difference_type;
139 typedef typename E2::value_type value_type;
141 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
142 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
143 size_type size = e2 ().size ();
144 for (size_type
n = 0;
n < size; ++
n) {
145 #ifndef BOOST_UBLAS_SINGULAR_CHECK
146 BOOST_UBLAS_CHECK (e1 () (
n,
n) != value_type(), singular ());
148 if (e1 () (
n,
n) == value_type())
149 singular ().raise ();
151 value_type
t = e2 () (
n) / e1 () (
n,
n);
153 if (
t != value_type()) {
154 project( e2 (), range(
n+1, size) )
155 .minus_assign(
t * project( column( e1 (),
n), range(
n+1, size) ) );
172 template <
class MATRIX >
175 using namespace ublas;
177 typedef typename MATRIX::value_type T;
180 const MATRIX& A_c(
A);
182 const size_t n =
A.size1();
184 for (
size_t k=0 ;
k <
n;
k++) {
186 double qL_kk = A_c(
k,
k) - inner_prod( project( row( A_c,
k ), range(0,
k) ),
187 project( row( A_c,
k ), range(0,
k) ) );
192 double L_kk = sqrt( qL_kk );
195 for (
size_t i =
k+1;
i <
A.size1(); ++
i) {
196 T* Aik =
A.find_element(
i,
k);
199 *Aik = ( *Aik - inner_prod( project( row( A_c,
k ), range(0,
k) ),
200 project( row( A_c,
i ), range(0,
k) ) ) ) / L_kk;
219 template <
class TRIA,
class VEC >
223 using namespace ublas;
225 inplace_solve(
L, x, lower_tag() );
226 inplace_solve(trans(
L), x, upper_tag());