mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
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:
63
Maths/test/Determinant.test.js
Normal file
63
Maths/test/Determinant.test.js
Normal 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)
|
||||
}
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user