-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecompositions.js
More file actions
26 lines (24 loc) · 924 Bytes
/
decompositions.js
File metadata and controls
26 lines (24 loc) · 924 Bytes
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
import LuDecomposition from './lu.js';
import QrDecomposition from './qr.js';
import SingularValueDecomposition from './svd.js';
import Matrix from './matrix.js';
import WrapperMatrix2D from './WrapperMatrix2D.js';
export function inverse(matrix, useSVD = false) {
matrix = WrapperMatrix2D.checkMatrix(matrix);
if (useSVD) {
return new SingularValueDecomposition(matrix).inverse();
} else {
return solve(matrix, Matrix.eye(matrix.rows));
}
}
export function solve(leftHandSide, rightHandSide, useSVD = false) {
leftHandSide = WrapperMatrix2D.checkMatrix(leftHandSide);
rightHandSide = WrapperMatrix2D.checkMatrix(rightHandSide);
if (useSVD) {
return new SingularValueDecomposition(leftHandSide).solve(rightHandSide);
} else {
return leftHandSide.isSquare()
? new LuDecomposition(leftHandSide).solve(rightHandSide)
: new QrDecomposition(leftHandSide).solve(rightHandSide);
}
}