Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear Algebra: Determinant of a Matrix Added #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"no-param-reassign": "off",
"class-methods-use-this": "off",
"no-bitwise": "off",
"no-continue": "off"
"no-continue": "off",
"no-plusplus": "off"
}
}
4 changes: 3 additions & 1 deletion src/algorithms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const math = require('./math');
const string = require('./string');
const search = require('./search');
const sort = require('./sort');
const linearAlgebra = require('./linear algebra');

module.exports = {
geometry,
math,
string,
search,
sort
sort,
linearAlgebra
};
5 changes: 5 additions & 0 deletions src/algorithms/linear algebra/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const matrixDeterminant = require('./matrix_determinant');

module.exports = {
matrixDeterminant
};
66 changes: 66 additions & 0 deletions src/algorithms/linear algebra/matrix_determinant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Determinant of a square matrix
* @param {Array} mat input matrix
* @param {Number} n no. of rows
* @return {Number} determinant
*/
const create2dArr = (len) => {
const arr = [];
for (let i = 0; i < len; i++) {
arr.push([]);
}
return arr;
};

const getCoMatrix = (mat, p, q, len) => {
let i = 0;
let j = 0;
const coMatrix = create2dArr(len);

for (let row = 0; row < len; row++) {
for (let col = 0; col < len; col++) {
// copy into temporary matrix
if (row !== p && col !== q) {
coMatrix[i][j++] = mat[row][col];

// one row filled, go to next one
if (j === len - 1) {
j = 0;
i++;
}
}
}
}

return coMatrix;
};

const matrixDeterminant = (mat, len) => {
let D = 0;
// to store co matrices
let coMat;
// to toggle signs
let sign = 1;

// check whether it is square or not
if (len !== mat[0].length) {
throw new Error('Not a square matrix!');
}

// base case: if matrix contains single element
if (len === 1) { return mat[0][0]; }

// iterate for each element of first row
for (let f = 0; f < len; f++) {
// get co matrix
coMat = getCoMatrix(mat, 0, f, len);
D += sign * mat[0][f] * matrixDeterminant(coMat, len - 1);

// toggle sign
sign = -sign;
}

return D;
};

module.exports = matrixDeterminant;
37 changes: 37 additions & 0 deletions test/algorithms/linear algebra/testMatrixDeterminant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env mocha */
const matrixDeterminant = require('../../../src').algorithms.linearAlgebra.matrixDeterminant;

const assert = require('assert');

const matOK1 = [
[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
];

const matOK2 = [
[1, 2, 3, 8],
[8, 9, 4, 1],
[7, 6, 5, 3],
[2, 4, 5, 6]
];

const matNotOK = [
[1, 2],
[3, 4],
[5, 6]
];

describe('Determinant of Matrix', () => {
it('should throw an error if the matrix is not a square', () => {
assert.throws(() => matrixDeterminant(matNotOK, matNotOK.length));
});

it('should return appropriate determinant for 3x3 matrix', () => {
assert.equal(matrixDeterminant(matOK1, matOK1.length), -48);
});

it('should return appropriate determinant for 4x4 matrix', () => {
assert.equal(matrixDeterminant(matOK2, matOK2.length), 347);
});
});