Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita24450a

Browse files
feat: add determinant algorithm (TheAlgorithms#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>
1 parent13161bd commita24450a

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

‎Maths/Determinant.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Given a square matrix, find its determinant using Laplace Expansion.
3+
* Time Complexity : O(n!)
4+
*
5+
* For more info: https://en.wikipedia.org/wiki/Determinant
6+
*
7+
*@param {number[[]]} matrix - Two dimensional array of integers.
8+
*@returns {number} - An integer equal to the determinant.
9+
*
10+
*@example
11+
* const squareMatrix = [
12+
* [2,3,4,6],
13+
* [5,8,9,0],
14+
* [7,4,3,9],
15+
* [4,0,2,1]
16+
* ];
17+
*
18+
* const result = determinant(squareMatrix);
19+
* // The function should return 858 as the resultant determinant.
20+
*/
21+
22+
constsubMatrix=(matrix,i,j)=>{
23+
letmatrixSize=matrix[0].length
24+
if(matrixSize===1){
25+
returnmatrix[0][0]
26+
}
27+
letsubMatrix=[]
28+
for(letx=0;x<matrixSize;x++){
29+
if(x===i){
30+
continue
31+
}
32+
subMatrix.push([])
33+
for(lety=0;y<matrixSize;y++){
34+
if(y===j){
35+
continue
36+
}
37+
subMatrix[subMatrix.length-1].push(matrix[x][y])
38+
}
39+
}
40+
returnsubMatrix
41+
}
42+
43+
constisMatrixSquare=(matrix)=>{
44+
letnumRows=matrix.length
45+
for(leti=0;i<numRows;i++){
46+
if(numRows!==matrix[i].length){
47+
returnfalse
48+
}
49+
}
50+
returntrue
51+
}
52+
53+
constdeterminant=(matrix)=>{
54+
if(
55+
!Array.isArray(matrix)||
56+
matrix.length===0||
57+
!Array.isArray(matrix[0])
58+
){
59+
thrownewError('Input is not a valid 2D matrix.')
60+
}
61+
if(!isMatrixSquare(matrix)){
62+
thrownewError('Square matrix is required.')
63+
}
64+
letnumCols=matrix[0].length
65+
if(numCols===1){
66+
returnmatrix[0][0]
67+
}
68+
letresult=0
69+
letsetIndex=0
70+
for(leti=0;i<numCols;i++){
71+
result+=
72+
Math.pow(-1,i)*
73+
matrix[setIndex][i]*
74+
determinant(subMatrix(matrix,setIndex,i))
75+
}
76+
returnresult
77+
}
78+
export{determinant}

‎Maths/test/Determinant.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import{expect}from'vitest'
2+
import{determinant}from'../Determinant'
3+
describe('Determinant',()=>{
4+
test.each([
5+
[
6+
[
7+
[8,1,6],
8+
[1,2,3],
9+
[4,7,5]
10+
],
11+
-87
12+
],
13+
[
14+
[
15+
[2,1,0,2],
16+
[1,2,4,3],
17+
[0,4,7,5],
18+
[4,7,9,8]
19+
],
20+
25
21+
],
22+
[
23+
[
24+
[5,9],
25+
[3,7]
26+
],
27+
8
28+
],
29+
[
30+
[
31+
[7,5,1,4,3],
32+
[6,8,7,9,6],
33+
[9,8,0,4,7],
34+
[0,3,4,7,9],
35+
[3,6,2,8,8]
36+
],
37+
2476
38+
],
39+
[[[23]],23]
40+
])(
41+
'Should return the determinant of the square matrix.',
42+
(matrix,expected)=>{
43+
expect(determinant(matrix)).toEqual(expected)
44+
}
45+
)
46+
47+
test.each([
48+
[
49+
[
50+
[1,6],
51+
[1,2,3],
52+
[4,7,5]
53+
],
54+
'Square matrix is required.'
55+
],
56+
[[1,3,2,[5,8,6],3],'Input is not a valid 2D matrix.']
57+
])(
58+
'Should return the error message.',
59+
(matrix,expected)=>{
60+
expect(()=>determinant(matrix)).toThrowError(expected)
61+
}
62+
)
63+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp