Skip to content

Instantly share code, notes, and snippets.

@austinpray
Last active August 29, 2015 14:06
Show Gist options
  • Save austinpray/8a91be8489a88fed1089 to your computer and use it in GitHub Desktop.
Save austinpray/8a91be8489a88fed1089 to your computer and use it in GitHub Desktop.
function determinant(m) {
var l = m.length - 1;
if(l === 0) {
return m[0][0];
} else {
if(l === 1) {
return m[0][0]*m[l][l]-m[0][l]*m[l][0];
} else {
return m.reduce(function (p, c, i, a) {
var sign = i % 2 === 0 ? 1 : -1;
var minor = a.slice(0)
minor.splice(0,1);
minor = minor.map(function(val) {
val = val.slice(0);
val.splice(i,1);
return val;
});
return p + (sign * m[0][i] * determinant(minor));
}, 0);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment