mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* feat: add row echelon matrix algorithm * test: add self-tests for row echelon algorithm * fix: replace rounding with float tolerance * chore: use correct style * fix: use error tolerance and segregate testcases * chore: add necessary explaining comments
90 lines
1.9 KiB
JavaScript
90 lines
1.9 KiB
JavaScript
import { rowEchelon } from '../RowEchelon'
|
|
describe('Determinant', () => {
|
|
const tolerance = 0.000001
|
|
test.each([
|
|
[
|
|
[
|
|
[8, 1, 3, 5],
|
|
[4, 6, 8, 2],
|
|
[3, 5, 6, 8]
|
|
],
|
|
[
|
|
[1, 0.125, 0.375, 0.625],
|
|
[0, 1, 1.18182, -0.09091],
|
|
[0, 0, 1, -11.0769]
|
|
]
|
|
],
|
|
[
|
|
[
|
|
[6, 8, 1, 3, 5],
|
|
[1, 4, 6, 8, 2],
|
|
[0, 3, 5, 6, 8],
|
|
[2, 5, 9, 7, 8],
|
|
[5, 5, 7, 0, 1]
|
|
],
|
|
[
|
|
[1, 1.33333, 0.16667, 0.5, 0.83333],
|
|
[0, 1, 2.1875, 2.8125, 0.4375],
|
|
[0, 0, 1, 1.56, -4.28003],
|
|
[0, 0, 0, 1, -3.3595],
|
|
[0, 0, 0, 0, 1]
|
|
]
|
|
],
|
|
[
|
|
[
|
|
[1, 3, 5],
|
|
[6, 8, 2],
|
|
[5, 6, 8],
|
|
[7, 9, 9],
|
|
[5, 0, 6]
|
|
],
|
|
[
|
|
[1, 3, 5],
|
|
[0, 1, 2.8],
|
|
[0, 0, 1],
|
|
[0, 0, 0],
|
|
[0, 0, 0]
|
|
]
|
|
],
|
|
[
|
|
[
|
|
[0, 7, 8, 1, 3, 5],
|
|
[0, 6, 4, 6, 8, 2],
|
|
[0, 7, 3, 5, 6, 8],
|
|
[6, 8, 1, 0, 0, 4],
|
|
[3, 3, 5, 7, 3, 1],
|
|
[1, 2, 1, 0, 9, 7],
|
|
[8, 8, 0, 2, 3, 1]
|
|
],
|
|
[
|
|
[1, 1.33333, 0.16667, 0, 0, 0.66667],
|
|
[0, 1, 0.66667, 1, 1.33333, 0.33333],
|
|
[0, 0, 1, 1.2, 1.99999, -3.4],
|
|
[0, 0, 0, 1, 1.3, -1.4],
|
|
[0, 0, 0, 0, 1, -2.32854],
|
|
[0, 0, 0, 0, 0, 1],
|
|
[0, 0, 0, 0, 0, 0]
|
|
]
|
|
]
|
|
])('Should return the matrix in row echelon form.', (matrix, expected) => {
|
|
for (let i = 0; i < matrix.length; i++) {
|
|
for (let j = 0; j < matrix[i].length; j++) {
|
|
expect(rowEchelon(matrix)[i][j]).toBeCloseTo(expected[i][j], tolerance)
|
|
}
|
|
}
|
|
})
|
|
|
|
test.each([
|
|
[
|
|
[
|
|
[8, 1, 3, 5],
|
|
[4, 6, 8, 2, 7],
|
|
[3, 5, 6, 8]
|
|
],
|
|
'Input is not a valid 2D matrix.'
|
|
]
|
|
])('Should return the error message.', (matrix, expected) => {
|
|
expect(() => rowEchelon(matrix)).toThrowError(expected)
|
|
})
|
|
})
|