-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcholesky.hhs
36 lines (29 loc) · 927 Bytes
/
cholesky.hhs
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
function cholesky(inputMatrix:Mat) {
L: Mat;
if(inputMatrix.rows !== inputMatrix.cols || inputMatrix.rows == 0 || inputMatrix.cols == 0) {
throw new Error('Wrong dimension for'+inputMatrix);
}
if(!inputMatrix.equals(inputMatrix.transpose())) {
throw new Error('Matrix is not symmetric:'+inputMatrix)
}
/*if (!isSymmetric(inputMatrix)) { throw new Error('Not symmetric')} */
//dimension n
const n = inputMatrix.rows;
//matrix L
const L = new Mat().zeros(n, n);
//iteration
for (let i = 0; i < n; i++) {
for (let k = 0; k < i + 1; k++) {
let sum = 0;
for (let j = 0; j < k; j++) {
sum += L.val[i][j] * L.val[k][j];
}
if (i === k) {
L.val[i][k] = Math.sqrt(inputMatrix.val[i][i] - sum);
} else {
L.val[i][k] = (1.0 / L.val[k][k]) * (inputMatrix.val[i][k] - sum);
}
}
}
return L;
}