-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeterminant.js
43 lines (39 loc) · 1.23 KB
/
determinant.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
import LuDecomposition from './lu.js';
import Matrix from './matrix.js';
import MatrixSelectionView from './selection.js';
export function determinant(matrix) {
matrix = Matrix.checkMatrix(matrix);
if (matrix.isSquare()) {
if (matrix.columns === 0) {
return 1;
}
let a, b, c, d;
if (matrix.columns === 2) {
// 2 x 2 matrix
a = matrix.get(0, 0);
b = matrix.get(0, 1);
c = matrix.get(1, 0);
d = matrix.get(1, 1);
return a * d - b * c;
} else if (matrix.columns === 3) {
// 3 x 3 matrix
let subMatrix0, subMatrix1, subMatrix2;
subMatrix0 = new MatrixSelectionView(matrix, [1, 2], [1, 2]);
subMatrix1 = new MatrixSelectionView(matrix, [1, 2], [0, 2]);
subMatrix2 = new MatrixSelectionView(matrix, [1, 2], [0, 1]);
a = matrix.get(0, 0);
b = matrix.get(0, 1);
c = matrix.get(0, 2);
return (
a * determinant(subMatrix0) -
b * determinant(subMatrix1) +
c * determinant(subMatrix2)
);
} else {
// general purpose determinant using the LU decomposition
return new LuDecomposition(matrix).determinant;
}
} else {
throw Error('determinant can only be calculated for a square matrix');
}
}