29 #ifndef TOON_INCLUDE_CHOLESKY_H 30 #define TOON_INCLUDE_CHOLESKY_H 32 #include <TooN/TooN.h> 66 template <
int Size=Dynamic,
class Precision=DefaultPrecision>
74 template<
class P2,
class B2>
96 int size=my_cholesky.num_rows();
97 for(
int col=0; col<size; col++){
98 Precision inv_diag = 1;
99 for(
int row=col; row < size; row++){
101 Precision val = my_cholesky(row,col);
102 for(
int col2=0; col2<col; col2++){
104 val-=my_cholesky(col2,col)*my_cholesky(row,col2);
108 my_cholesky(row,col)=val;
116 my_cholesky(col,row)=val;
118 my_cholesky(row,col)=val*inv_diag;
129 template<
int Size2,
class P2,
class B2>
131 int size=my_cholesky.num_rows();
136 for(
int i=0; i<size; i++){
137 Precision val = v[i];
138 for(
int j=0; j<i; j++){
139 val -= my_cholesky(i,j)*y[j];
145 for(
int i=0; i<size; i++){
146 y[i]/=my_cholesky(i,i);
151 for(
int i=size-1; i>=0; i--){
152 Precision val = y[i];
153 for(
int j=i+1; j<size; j++){
154 val -= my_cholesky(j,i)*result[j];
164 template<
int Size2,
int C2,
class P2,
class B2>
166 int size=my_cholesky.num_rows();
171 for(
int i=0; i<size; i++){
173 for(
int j=0; j<i; j++){
174 val -= my_cholesky(i,j)*y[j];
180 for(
int i=0; i<size; i++){
181 y[i]*=(1/my_cholesky(i,i));
186 for(
int i=size-1; i>=0; i--){
188 for(
int j=i+1; j<size; j++){
189 val -= my_cholesky(j,i)*result[j];
207 Precision answer=my_cholesky(0,0);
208 for(
int i=1; i<my_cholesky.num_rows(); i++){
209 answer*=my_cholesky(i,i);
214 template <
int Size2,
typename P2,
typename B2>
221 my_cholesky.num_rows());
223 for (
int i=1;i<my_cholesky.num_rows();i++) {
224 for (
int j=0;j<i;j++) {
225 m(i,j)=my_cholesky(i,j);
233 my_cholesky.num_rows());
235 for (
int i=0;i<my_cholesky.num_rows();i++) {
236 m(i,i)=my_cholesky(i,i);
244 my_cholesky.num_rows());
246 for (
int j=0;j<my_cholesky.num_cols();j++) {
247 Precision sqrtd=
sqrt(my_cholesky(j,j));
249 for (
int i=j+1;i<my_cholesky.num_rows();i++) {
250 m(i,j)=my_cholesky(i,j)*sqrtd;
256 int rank()
const {
return my_rank; }
Decomposes a positive-semidefinite symmetric matrix A (such as a covariance) into L*D*L^T...
Definition: Cholesky.h:67
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
Precision determinant()
Compute the determinant.
Definition: Cholesky.h:206
A matrix.
Definition: matrix.hh:105
Vector< Size, Precision > backsub(const Vector< Size2, P2, B2 > &v) const
Compute x = A^-1*v Run time is O(N^2)
Definition: Cholesky.h:130
Cholesky(const Matrix< Size, Size, P2, B2 > &m)
Construct the Cholesky decomposition of a matrix.
Definition: Cholesky.h:75
Cholesky(int size)
Constructor for Size=Dynamic.
Definition: Cholesky.h:82
Matrix< Size, C2, Precision > backsub(const Matrix< Size2, C2, P2, B2 > &m) const
overload
Definition: Cholesky.h:165
void compute(const Matrix< Size, Size, P2, B2 > &m)
Compute the LDL^T decomposition of another matrix.
Definition: Cholesky.h:87
Matrix< Size, Size, Precision > get_inverse()
Compute A^-1 and store in M Run time is O(N^3)
Definition: Cholesky.h:200
Matrix< R, C, P > sqrt(const Matrix< R, C, P, B > &m)
computes a matrix square root of a matrix m by the product form of the Denman and Beavers iteration a...
Definition: helpers.h:350
Definition: size_mismatch.hh:103