-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearDependencies.js
53 lines (48 loc) · 1.31 KB
/
linearDependencies.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
import SingularValueDecomposition from './svd.js';
import Matrix from './matrix.js';
function xrange(n, exception) {
let range = [];
for (let i = 0; i < n; i++) {
if (i !== exception) {
range.push(i);
}
}
return range;
}
function dependenciesOneRow(
error,
matrix,
index,
thresholdValue = 10e-10,
thresholdError = 10e-10,
) {
if (error > thresholdError) {
return new Array(matrix.rows + 1).fill(0);
} else {
let returnArray = matrix.addRow(index, [0]);
for (let i = 0; i < returnArray.rows; i++) {
if (Math.abs(returnArray.get(i, 0)) < thresholdValue) {
returnArray.set(i, 0, 0);
}
}
return returnArray.to1DArray();
}
}
export function linearDependencies(matrix, options = {}) {
const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options;
matrix = Matrix.checkMatrix(matrix);
let n = matrix.rows;
let results = new Matrix(n, n);
for (let i = 0; i < n; i++) {
let b = Matrix.columnVector(matrix.getRow(i));
let Abis = matrix.subMatrixRow(xrange(n, i)).transpose();
let svd = new SingularValueDecomposition(Abis);
let x = svd.solve(b);
let error = Matrix.sub(b, Abis.mmul(x)).abs().max();
results.setRow(
i,
dependenciesOneRow(error, x, i, thresholdValue, thresholdError),
);
}
return results;
}