-
Notifications
You must be signed in to change notification settings - Fork 0
/
cholesky.js
87 lines (72 loc) · 2 KB
/
cholesky.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Matrix from './matrix.js';
import WrapperMatrix2D from './WrapperMatrix2D.js';
export default class CholeskyDecomposition {
constructor(value) {
value = WrapperMatrix2D.checkMatrix(value);
if (!value.isSymmetric()) {
throw new Error('Matrix is not symmetric');
}
let a = value;
let dimension = a.rows;
let l = new Matrix(dimension, dimension);
let positiveDefinite = true;
let i, j, k;
for (j = 0; j < dimension; j++) {
let d = 0;
for (k = 0; k < j; k++) {
let s = 0;
for (i = 0; i < k; i++) {
s += l.get(k, i) * l.get(j, i);
}
s = (a.get(j, k) - s) / l.get(k, k);
l.set(j, k, s);
d = d + s * s;
}
d = a.get(j, j) - d;
positiveDefinite &= d > 0;
l.set(j, j, Math.sqrt(Math.max(d, 0)));
for (k = j + 1; k < dimension; k++) {
l.set(j, k, 0);
}
}
this.L = l;
this.positiveDefinite = Boolean(positiveDefinite);
}
isPositiveDefinite() {
return this.positiveDefinite;
}
solve(value) {
value = WrapperMatrix2D.checkMatrix(value);
let l = this.L;
let dimension = l.rows;
if (value.rows !== dimension) {
throw new Error('Matrix dimensions do not match');
}
if (this.isPositiveDefinite() === false) {
throw new Error('Matrix is not positive definite');
}
let count = value.columns;
let B = value.clone();
let i, j, k;
for (k = 0; k < dimension; k++) {
for (j = 0; j < count; j++) {
for (i = 0; i < k; i++) {
B.set(k, j, B.get(k, j) - B.get(i, j) * l.get(k, i));
}
B.set(k, j, B.get(k, j) / l.get(k, k));
}
}
for (k = dimension - 1; k >= 0; k--) {
for (j = 0; j < count; j++) {
for (i = k + 1; i < dimension; i++) {
B.set(k, j, B.get(k, j) - B.get(i, j) * l.get(i, k));
}
B.set(k, j, B.get(k, j) / l.get(k, k));
}
}
return B;
}
get lowerTriangularMatrix() {
return this.L;
}
}