Skip to content

Instantly share code, notes, and snippets.

@e-mihaylin
Created June 19, 2018 13:19
Show Gist options
  • Save e-mihaylin/4a467b500b76a540cebb9875a32c9d6b to your computer and use it in GitHub Desktop.
Save e-mihaylin/4a467b500b76a540cebb9875a32c9d6b to your computer and use it in GitHub Desktop.
const determinant = m => {
const l = m.length;
if (l === 1) return m[0][0];
if (l === 2) return m[0][0] * m[1][1] - m[1][0] * m[0][1];
let result = 0;
for (let i = 0; i < l; i++) {
result += Math.pow(-1, i) * m[0][i] * determinant(minor(m, i, 0));
}
return result;
};
const minor = (m, i, j) =>
m.map(e => e.filter((_, index) => index !== i))
.filter((_, index) => index !== j);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment