Cholesky.cc 3.37 KB
 Peter Gottschling committed Feb 15, 2008 1 2 3 4 ``````#include "Cholesky.h" bool Cholesky::factorization(Matrix *A, Vector *p) { `````` Thomas Witkowski committed Apr 29, 2009 5 `````` FUNCNAME("Cholesky::factorization()"); `````` Peter Gottschling committed Feb 15, 2008 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 `````` int n = A->getNumRows(); // Checking memory for vector P of diagonal elements of factorization. static Vector *pT = NULL; if (p) { if (p->getSize() != n) p->resize(n); } else { if (pT) { if (pT->getSize() != n) pT->resize(n); } else `````` Thomas Witkowski committed May 25, 2009 25 `````` pT = new Vector(n); `````` Peter Gottschling committed Feb 15, 2008 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 `````` p = pT; } // Constructs the Cholesky factorization A = L*L, with L lower triangular. // Only the upper triangle need be given; it is not modified. // The Cholesky factor L is stored in the lower triangle of A, except for // its diagonal which is stored in P. int i, j, k; double sum; for (i=0; i=0; k--) sum -= (*A)[i][k] * (*A)[j][k]; if (i==j) { if (sum<=0) { MSG("Matrix is (numerically) not positive definite!\n"); MSG("Cholesky decomposition does not work; choose another method for solving the system.\n"); return false; } (*p)[i] = sqrt(sum); } else (*A)[j][i] = sum / (*p)[i]; } } return true; } bool Cholesky::solve(Matrix *A, Vector *b, Vector *x, Vector *p) { FUNCNAME("Cholesky::solve"); bool success = true; int n = b->getSize(); TEST_EXIT(n == A->getNumRows()) ("Dimensions of matrix and vector do not match!\n"); // Checking memory for solution vector X. if (x && (x->getSize() != n)) x->resize(n); if (!x) `````` Thomas Witkowski committed May 25, 2009 78 `````` x = new Vector(n); `````` Peter Gottschling committed Feb 15, 2008 79 80 81 82 83 84 85 `````` // Checking vector P. static Vector *pT = NULL; if (!p || (p->getSize() != n)) { if (pT && pT->getSize() != n) `````` Thomas Witkowski committed May 25, 2009 86 `````` delete pT; `````` Peter Gottschling committed Feb 15, 2008 87 88 `````` if (!pT) `````` Thomas Witkowski committed May 25, 2009 89 `````` pT = new Vector(n); `````` Peter Gottschling committed Feb 15, 2008 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 `````` p = pT; success = factorization(A, p); } // Now solve the system by backsubstitution. int i, k; double sum; for (i=0; i=0; k--) sum -= (*A)[i][k] * (*x)[k]; (*x)[i] = sum / (*p)[i]; } for (i=n-1; i>=0; i--) // Solve L^T*X = Y. { for (sum=(*x)[i], k=i+1; k *A, Vector > *b, Vector > *x, Vector *p) { FUNCNAME("Cholesky::solve"); bool success = true; int n = b->getSize(); TEST_EXIT(n == A->getNumRows()) ("Dimensions of matrix and vector do not match!\n"); // Checking memory for solution vector X. if (x && (x->getSize() != n)) x->resize(n); if (!x) `````` Thomas Witkowski committed May 25, 2009 132 `````` x = new Vector >(n); `````` Peter Gottschling committed Feb 15, 2008 133 134 135 136 137 138 `````` // Checking vector P. static Vector *pT = NULL; if (!p || (p->getSize() != n)) { `````` Thomas Witkowski committed May 25, 2009 139 `````` pT = new Vector(n); `````` Peter Gottschling committed Feb 15, 2008 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 `````` p = pT; success = factorization(A, p); } // Now solve the system by backsubstitution. int i, k; WorldVector vec_sum; for (i=0; i=0; k--) vec_sum -= (*x)[k] * (*A)[i][k] ; (*x)[i] = vec_sum * (1.0/(*p)[i]); } for (i=n-1; i>=0; i--) // Solve L^T*X = Y. { for (vec_sum=(*x)[i], k=i+1; k