Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
cholesky.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 
34 #ifndef CHOLESKY_H
35 #define CHOLESKY_H
36 
37 #include <cmath>
38 
39 // index method
40 
41 namespace TNT
42 {
43 
44 
45  //
46  // Only upper part of A is used. Cholesky factor is returned in
47  // lower part of L. Returns 0 if successful, 1 otherwise.
48  //
49  template <class SPDMatrix, class SymmMatrix>
50  int Cholesky_upper_factorization(SPDMatrix &A, SymmMatrix &L)
51  {
52  Subscript M = A.dim(1);
53  Subscript N = A.dim(2);
54 
55  BIASASSERT(M == N); // make sure A is square
56 
57  // readjust size of L, if necessary
58 
59  if (M != L.dim(1) || N != L.dim(2))
60  L = SymmMatrix(N,N);
61 
62  Subscript i,j,k;
63 
64 
65  typename SPDMatrix::element_type dot=0;
66 
67 
68  for (j=1; j<=N; j++) // form column j of L
69  {
70  dot= 0;
71 
72  for (i=1; i<j; i++) // for k= 1 TO j-1
73  dot = dot + L(j,i)*L(j,i);
74 
75  L(j,j) = A(j,j) - dot;
76 
77  for (i=j+1; i<=N; i++)
78  {
79  dot = 0;
80  for (k=1; k<j; k++)
81  dot = dot + L(i,k)*L(j,k);
82  L(i,j) = A(j,i) - dot;
83  }
84 
85  if (L(j,j) <= 0.0) return 1;
86 
87  L(j,j) = sqrt( L(j,j) );
88 
89  for (i=j+1; i<=N; i++)
90  L(i,j) = L(i,j) / L(j,j);
91 
92  }
93 
94  return 0;
95  }
96 
97 
98 
99 
100 }
101 // namespace TNT
102 
103 #endif
104 // CHOLESKY_H
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
int Cholesky_upper_factorization(SPDMatrix &A, SymmMatrix &L)
Definition: cholesky.h:50