Created
April 10, 2015 09:55
-
-
Save Aleksey-Danchin/651bfabebdc0209f150d to your computer and use it in GitHub Desktop.
Determinant of quadratic matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function det (argStr) { | |
var numbers = argStr.split(' ').map(Number), | |
length = Math.sqrt(numbers.length); | |
if ((length % 1) !== 0) return false; | |
if (length === 1) return numbers[0]; | |
if (length === 2) return numbers[0] * numbers[3] - numbers[1] * numbers[2]; | |
var matrica = []; | |
for (var i = 0; i < length; i++) { | |
for (var j = 0, row = []; j < length; j++) | |
row.push(numbers.shift()); | |
matrica.push(row); | |
} | |
var detNumber = 0; | |
for (var i = 0; i < length; i++) { | |
var newArgStr = ''; | |
for (var j = 1; j < length; j++) | |
for (var k = 0; k < length; k++) | |
if (k !== i) newArgStr += matrica[j][k] + ' '; | |
newArgStr = newArgStr.trim(); | |
detNumber += Math.pow(-1, i) * matrica[0][i] * det(newArgStr); | |
} | |
return detNumber; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function det(a){var b=a.split(" ").map(Number),c=Math.sqrt(b.length);if(0!==c%1)return!1;if(1===c)return b[0];if(2===c)return b[0]*b[3]-b[1]*b[2];for(var d=[],e=0;c>e;e++){for(var f=0,g=[];c>f;f++)g.push(b.shift());d.push(g)}for(var h=0,e=0;c>e;e++){for(var i="",f=1;c>f;f++)for(var j=0;c>j;j++)j!==e&&(i+=d[f][j]+" ");i=i.trim(),h+=Math.pow(-1,e)*d[0][e]*det(i)}return h} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function det(a){var b=a.split(" ").map(Number),c=Math.sqrt(b.length);if(0!==c%1)return!1;if(1===c)return b[0];if(2===c)return b[0]*b[3]-b[1]*b[2];for(var d=[],e=0;c>e;e++){for(var f=0,g=[];c>f;f++)g.push(b.shift());d.push(g)}for(var h=0,e=0;c>e;e++){for(var i="",f=1;c>f;f++)for(var j=0;c>j;j++)j!==e&&(i+=d[f][j]+" ");i=i.trim(),h+=Math.pow(-1,e)*d[0][e]*det(i)}return h} | |
console.log(det('5 2 3 3 2 2 1 2 5')); // 16 | |
console.log(det('5 2 3 3 2 2 1 2 9')); // 32 | |
console.log(det('1 2 2 3')); // -1 | |
console.log(det('11 25 33 87')); // 132 | |
console.log(det('12 56 8 96 3 78 45 8 33')); // 17916 | |
// | 12 23 44 56 3 112 | | |
// | 12 11 144 456 233 3 | | |
// | 897 1 0 124 0 233 | | |
// | 88 2 4 1 2 3 | = -11887289955588 | |
// | 1 2 1 2 34 56 | | |
// | 12 55 876 3 112 809 | | |
console.log(det('12 23 44 56 3 112 12 11 144 456 233 3 897 1 0 124 0 233 88 2 4 1 2 3 1 2 1 2 34 56 12 55 876 3 11 809')); // -11887289955588 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment