-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: add determinant algorithm #1438
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13a5d57
feat: add determinant calculating algorithm
piyushk77 7c5e6fb
test: add self-tests for determinant algorithm
piyushk77 4284d3b
chore: add wikipedia info link
piyushk77 a94b419
fix: change initialization to zero
piyushk77 7f6a7b6
fix: add error throw and general code improvements
piyushk77 55a04d3
fix: add error try and catch
piyushk77 9506205
fix: seperate the test loops of error cases
piyushk77 dd43ae4
clean up a bit
appgurueu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.