-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cholesky.cpp
executable file
·73 lines (62 loc) · 1.6 KB
/
Cholesky.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
#ifndef __CHOLESKY_CPP
#define __CHOLESKY_CPP
#include "matrix.h"
#include "Cholesky.h"
#include <iostream>
#include <math.h>
using namespace std;
// Calculate cholesky decomposition
// Because the matrices are assumed to be s.p.d.
// There is no s.p.d checking mechanism.
template <typename T>
QSMatrix<T> Cholesky<T>::decomposition(QSMatrix<T>& A) {
int i ,j;
int n = A.get_rows();
QSMatrix<T> p(n, 1, 0);
QSMatrix<T> out(n, n, 0);
T s = 0;
QSMatrix<T> a(A);
for (i = 0; i < n; ++i) {
s = a(i, i);
//p.init();
s = sqrt(s) / s;
for (j = i; j < n; ++j) {
T tmp = a(j, i) * s;
p(j, 0) = tmp;
out(j, i) = tmp;
}
a -= p * p.transpose();
}
return out;
}
// Using cholesky decomposition to calculate matrix determinant
// det(A) = (det(L)^2)
template <typename T>
T Cholesky<T>::determinant(QSMatrix<T> &A) {
int n = A.get_rows();
QSMatrix<T> L(decomposition(A));
T det = 1;
for (int i = 0; i < n; ++i) {
det *= L(i, i);
}
return pow(det, 2);
}
// Calcualte inverse
template <typename T>
QSMatrix<T> Cholesky<T>::inverse(QSMatrix<T> &A) {
int n = A.get_rows();
QSMatrix<T> L(decomposition(A));
QSMatrix<T> inverseL(n, n, 0);
T sum = 0;
for (int i = 0; i < n; ++i) {
inverseL(i, i) = 1 / L(i, i);
for (int j = i + 1; j < n; ++j) {
sum = 0;
for (int k = i; k < j; ++k)
sum -= L(j, k) * inverseL(k, i);
inverseL(j, i) = sum / L(j, j);
}
}
return (inverseL.transpose()) * inverseL;
}
#endif