30 assert(n == (
int)(M.
rows()));
32 if (std::fabs(M(0,n-1) - M(n-1,0)) > 1e-8) {
33 std::cerr <<
"vnl_ldl_cholesky: WARNING: non-symmetric: " << M << std::endl;
40 std::cerr <<
"vnl_ldl_cholesky: " <<
num_dims_rank_def_ <<
" dimensions of non-posdeffness\n";
46 std::cerr <<
"vnl_ldl_cholesky: rcond=" <<
rcond_ <<
" so " <<
num_dims_rank_def_ <<
" dimensions of non-posdeffness\n";
56 for (
int i=0; i<n; ++i)
59 d_[i]=sqrt_d[i]*sqrt_d[i];
63 for (
int i=0; i<n; ++i)
66 for (
int j=0; j<i; ++j) row[j]/=sqrt_d[j];
68 for (
int j=i+1; j<n; ++j) row[j]=0.0;
73 inline double dot(
const double* v1,
const double* v2,
unsigned n)
76 for (
unsigned i=0;i<n;++i) sum+= v1[i]*v2[i];
80 inline double dot(
const double* v1,
unsigned s,
const double* v2,
unsigned n)
83 for (
unsigned i=0;i<n;++i,v1+=s) sum+= (*v1)*v2[i];
92 for (
unsigned i=1;i<n;++i)
103 for (
unsigned i=1;i<n;++i)
104 x[i] -=
dot(
L_[i],x,i);
107 for (
unsigned i=0;i<n;++i) x[i]/=
d_[i];
110 const double* L_data = &
L_(n-1,n-2);
111 const double* x_data = &x[n-1];
113 for (
int i=n-2;i>=0;--i,L_data-=(n+1),--x_data,++c)
115 x[i] -=
dot(L_data,n,x_data,c);
128 double sum = y[0]*y[0]/
d_[0];
129 for (
unsigned i=1;i<n;++i)
132 sum += y[i]*y[i]/
d_[i];
147 for (
unsigned i=0;i<n;++i,++xd,L_col+=(n+1),--c)
149 double xLi =
dot(L_col,n,xd,c);
182 for (
unsigned i=0;i<n;++i) det*=
d_[i];
198 for (
unsigned j=0;j<n;++j)
200 double a2=a+w[j]*w[j]/
d_[j];
202 double gamma = w[j]/
d_[j];
206 for (
unsigned p=j+1;p<n;++p)
220 assert(W0.
rows()==n);
225 for (
unsigned j=0;j<n;++j)
228 for (
unsigned i=0;i<r;++i)
230 double a2=a[i]+Wj[i]*Wj[i]/
d_[j];
232 gamma[i]=Wj[i]/
d_[j];
236 for (
unsigned p=j+1;p<n;++p)
240 for (
unsigned i=0;i<r;++i)
243 Lp[j]+=gamma[i]*Wp[i];
253 std::cerr <<
"vnl_ldl_cholesky: Calling inverse() on rank-deficient matrix\n";
257 unsigned int n =
d_.
size();
263 for (
unsigned int i=0; i<n; ++i)
double xt_m_x(const vnl_vector< double > &x) const
Efficient computation of x' * M * x.
bool set_size(size_t n)
Resize to n elements.
double determinant() const
Compute determinant.
size_t size() const
Return the length, number of elements, dimension of this vector.
T const * data_block() const
Access the contiguous block storing the elements in the vector. O(1).
double rcond_
1/(condition number).
vnl_vector< double > d_
Elements of diagonal matrix.
void inplace_solve(double *x) const
Solve Mx=b, overwriting input vector with the solution.
void rank1_update(const vnl_vector< double > &v)
Compute rank-1 update, ie the decomposition of (M+v.v').
double xt_m_inv_x(const vnl_vector< double > &x) const
Efficient computation of x' * inv(M) * x.
double dot(const double *v1, const double *v2, unsigned n)
Sum of v1[i]*v2i.
Declare in a central place the list of symbols from netlib.
void solve_lx(vnl_vector< double > &y)
Solve equation of form Lx=y (in-place).
Operation
Modes of computation. See constructor for details.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
vnl_matrix< double > inverse() const
Compute inverse. Not efficient.
vnl_vector< double > solve(vnl_vector< double > const &b) const
Solve LS problem M x = b.
Updateable Cholesky decomposition: A=LDL'.
unsigned int rows() const
Return the number of rows.
void update(const vnl_matrix< double > &W)
Multi-rank update, ie the decomposition of (M+W.W').
unsigned int columns() const
Return the number of columns.
vnl_matrix & set_identity()
Sets this matrix to an identity matrix, then returns "*this".
vnl_matrix< double > L_
Lower triangular matrix.
vnl_ldl_cholesky(vnl_matrix< double > const &M, Operation mode=verbose)
Make cholesky decomposition of M optionally computing the reciprocal condition number.