feat: add determinant algorithm (#1438)

* feat: add determinant calculating algorithm

* test: add self-tests for determinant algorithm

* chore: add wikipedia info link

* fix: change initialization to zero

* fix: add error throw and general code improvements

* fix: add error try and catch

* fix: seperate the test loops of error cases

* clean up a bit

---------

Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
Piyush Katyal
2023-10-11 11:33:02 +05:30
committed by GitHub
parent 13161bdadb
commit a24450a629
2 changed files with 141 additions and 0 deletions

78
Maths/Determinant.js Normal file
View File

@ -0,0 +1,78 @@
/**
* Given a square matrix, find its determinant using Laplace Expansion.
* Time Complexity : O(n!)
*
* For more info: https://en.wikipedia.org/wiki/Determinant
*
* @param {number[[]]} matrix - Two dimensional array of integers.
* @returns {number} - An integer equal to the determinant.
*
* @example
* const squareMatrix = [
* [2,3,4,6],
* [5,8,9,0],
* [7,4,3,9],
* [4,0,2,1]
* ];
*
* const result = determinant(squareMatrix);
* // The function should return 858 as the resultant determinant.
*/
const subMatrix = (matrix, i, j) => {
let matrixSize = matrix[0].length
if (matrixSize === 1) {
return matrix[0][0]
}
let subMatrix = []
for (let x = 0; x < matrixSize; x++) {
if (x === i) {
continue
}
subMatrix.push([])
for (let y = 0; y < matrixSize; y++) {
if (y === j) {
continue
}
subMatrix[subMatrix.length - 1].push(matrix[x][y])
}
}
return subMatrix
}
const isMatrixSquare = (matrix) => {
let numRows = matrix.length
for (let i = 0; i < numRows; i++) {
if (numRows !== matrix[i].length) {
return false
}
}
return true
}
const determinant = (matrix) => {
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
throw new Error('Input is not a valid 2D matrix.')
}
if (!isMatrixSquare(matrix)) {
throw new Error('Square matrix is required.')
}
let numCols = matrix[0].length
if (numCols === 1) {
return matrix[0][0]
}
let result = 0
let setIndex = 0
for (let i = 0; i < numCols; i++) {
result +=
Math.pow(-1, i) *
matrix[setIndex][i] *
determinant(subMatrix(matrix, setIndex, i))
}
return result
}
export { determinant }

View File

@ -0,0 +1,63 @@
import { expect } from 'vitest'
import { determinant } from '../Determinant'
describe('Determinant', () => {
test.each([
[
[
[8, 1, 6],
[1, 2, 3],
[4, 7, 5]
],
-87
],
[
[
[2, 1, 0, 2],
[1, 2, 4, 3],
[0, 4, 7, 5],
[4, 7, 9, 8]
],
25
],
[
[
[5, 9],
[3, 7]
],
8
],
[
[
[7, 5, 1, 4, 3],
[6, 8, 7, 9, 6],
[9, 8, 0, 4, 7],
[0, 3, 4, 7, 9],
[3, 6, 2, 8, 8]
],
2476
],
[[[23]], 23]
])(
'Should return the determinant of the square matrix.',
(matrix, expected) => {
expect(determinant(matrix)).toEqual(expected)
}
)
test.each([
[
[
[1, 6],
[1, 2, 3],
[4, 7, 5]
],
'Square matrix is required.'
],
[[1, 3, 2, [5, 8, 6], 3], 'Input is not a valid 2D matrix.']
])(
'Should return the error message.',
(matrix, expected) => {
expect(() => determinant(matrix)).toThrowError(expected)
}
)
})