You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
import SingularValueDecomposition from './dc/svd';
|
|
import Matrix from './matrix';
|
|
|
|
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;
|
|
}
|
|
|