@rawify/vector3
Version:
The RAW JavaScript 3D Vector library
86 lines (63 loc) • 1.86 kB
JavaScript
var u = require("utils");
// Define Current Field to operate in: Z_7
var P = {
add: function(x, y) {
return u.math.mod(x + y, 7);
},
mul: function(x, y) {
return u.math.mod(x * y, 7);
},
sub: function(x, y) {
return u.math.mod(x - y, 7);
}
};
// Simple Laplace determinant calculation
function determinant(A) {
if (A.length < 2)
return null;
// Recursion end at 2x2 Matrix with ad-bc determinant
if (A.length === 2) {
return u.math.polysub(
u.math.polymul(A[0][0], A[1][1], P),
u.math.polymul(A[0][1], A[1][0], P), P);
}
// Little Helper, calculates the "Streichungsmatrix" - Matrix A without row R and column C
function S(A, R, C) {
var r = new Array(A.length - 1);
for (var i = 0; i < A.length; i++) {
if (i === R)
continue;
var m = i > R ? i - 1 : i;
r[m] = new Array(A.length - 1);
for (var j = 0; j < A.length; j++) {
if (j === C)
continue;
var n = j > C ? j - 1 : j;
r[m][n] = A[i][j];
}
}
return r;
}
// S([[1,2,3],[4,5,6],[7,8,9]], 0,0);
var k = 0; // k's row
var sum = [0, 0, 0]; // Result polynomial
for (var i = 0; i < A.length; i++) { // Loop over the array
sum = u.math.polyadd(sum,
u.math.polymul(A[k][i],
u.math.polymul([Math.pow(-1, i + k)], determinant(S(A, k, i)), P), P), P);
}
return sum;
}
var A = [
[[0], [1], [2], [3], [4]],
[[6], [0], [2], [3], [4]],
[[5], [5], [0], [1], [0, 1]],
[[4], [4], [6], [0], [0, 3]],
[[3], [3], [0, 6], [0, 4], [0]]
];
console.log(determinant(A));
/*
determinant([
[[4], [5]],
[[3], [-2]]
]);*/