Skip to content

Commit 3aec6dc

Browse files
authored
--test file included formatting adjusted
1 parent eae28cb commit 3aec6dc

3 files changed

Lines changed: 75 additions & 73 deletions

File tree

src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.maths;
2+
import java.util.*;
3+
/*
4+
* Determinant of Matrix :https://textbooks.math.gatech.edu/ila/determinants-cofactors.html
5+
*/
6+
public class DeterminantOfMatrix {
7+
public static double[][] submatrix(double[][] mat,int order,int c) {
8+
//creating reference for the sub matrix
9+
double[][] subMat=new double[order-1][order-1];
10+
//sub_c stores the column column index of the sub matrix
11+
int subC;
12+
for(int i=1;i<order;i++) {
13+
//making the column count zero after each row-addition
14+
subC=0;
15+
for(int k=0;k<order;k++) {
16+
if(k==c) {
17+
continue;//skipping the column where the mat[0][c] of operation is in
18+
}
19+
subMat[i-1][subC]=mat[i][k];
20+
subC+=1;//increasing the column count
21+
}
22+
}
23+
return(subMat);
24+
}
25+
public static double determinant(double[][] mat,int order) {
26+
/*determinant of the matrix with one element(order 1) :
27+
is the element itself*/
28+
if (order==1) {
29+
return(mat[0][0]);
30+
}
31+
return(operation(mat,order));
32+
}
33+
public static double operation(double[][] mat,int order) {
34+
int check;
35+
double det=0;
36+
for(int c=0;c<order;++c) {
37+
if(c%2==0) {
38+
check=-1;
39+
}
40+
else {
41+
check=1;
42+
}
43+
det+=(check*(mat[0][c])*determinant(submatrix(mat,order,c),order-1)));
44+
}
45+
return(det);
46+
}
47+
}
48+
/*the above code will work for all square matrix whose order are greater than one.
49+
---to initiate this recursive algorithm pass the matrix and its order to the operation() method
50+
---if the order is 1 return the determinant of the matrix directly
51+
(determinant of the matrix with one element(order 1) :is the element itself)*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class DeterminantOfMatrixTest {
8+
9+
@Test
10+
void testDeterminantOfMatrix() {
11+
double[][] matrix1 = { { 1.0 } };
12+
assertEquals(1.0, DeterminantOfMatrix.DET(matrix1, 1));
13+
14+
double[][] matrix2 = { { 2.0, 3.0 }, { -4.0, 5.0 } };
15+
assertEquals(22.0, DeterminantOfMatrix.DET(matrix2, 2));
16+
17+
double[][] matrix3 = { { -1.0, 2.5, 3.0 }, { 4.0, -5.2, 6.0 }, { 7.3, -8.1, 9.6 } };
18+
assertEquals(58.9, DeterminantOfMatrix.DET(matrix3, 3));
19+
20+
double[][] matrix4 = { { -1.2, 2.3, -3.4, 4.5 }, { 5.6, -6.7, 7.8, -8.9 }, { -9.0, 10.1, -11.2, 12.3 }, { 13.4, -14.5, 15.6, -16.7 } };
21+
assertEquals(2549.1228, DeterminantOfMatrix.DET(matrix4, 4));
22+
23+
}
24+
}

0 commit comments

Comments
 (0)