File tree 2 files changed +81
-21
lines changed
main/java/com/thealgorithms/maths
test/java/com/thealgorithms/maths 2 files changed +81
-21
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .maths ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
/*
6
4
* @author Ojasva Jain
7
5
* Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant
@@ -10,8 +8,13 @@ public final class DeterminantOfMatrix {
10
8
private DeterminantOfMatrix () {
11
9
}
12
10
13
- // Determinant calculator
14
- //@return determinant of the input matrix
11
+ /**
12
+ * Calculates the determinant of a given matrix.
13
+ *
14
+ * @param a the input matrix
15
+ * @param n the size of the matrix
16
+ * @return the determinant of the matrix
17
+ */
15
18
static int determinant (int [][] a , int n ) {
16
19
int det = 0 ;
17
20
int sign = 1 ;
@@ -41,21 +44,4 @@ static int determinant(int[][] a, int n) {
41
44
}
42
45
return det ;
43
46
}
44
-
45
- // Driver Method
46
- public static void main (String [] args ) {
47
- Scanner in = new Scanner (System .in );
48
- // Input Matrix
49
- System .out .println ("Enter matrix size (Square matrix only)" );
50
- int n = in .nextInt ();
51
- System .out .println ("Enter matrix" );
52
- int [][] a = new int [n ][n ];
53
- for (int i = 0 ; i < n ; i ++) {
54
- for (int j = 0 ; j < n ; j ++) {
55
- a [i ][j ] = in .nextInt ();
56
- }
57
- }
58
- System .out .println (determinant (a , n ));
59
- in .close ();
60
- }
61
47
}
Original file line number Diff line number Diff line change
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
+ public void testDeterminant2x2Matrix () {
11
+ int [][] matrix = {
12
+ {1 , 2 },
13
+ {3 , 4 }
14
+ };
15
+ int expected = -2 ;
16
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 2 ));
17
+ }
18
+
19
+ @ Test
20
+ public void testDeterminant3x3Matrix () {
21
+ int [][] matrix = {
22
+ {1 , 2 , 3 },
23
+ {4 , 5 , 6 },
24
+ {7 , 8 , 9 }
25
+ };
26
+ int expected = 0 ;
27
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 3 ));
28
+ }
29
+
30
+ @ Test
31
+ public void testDeterminant3x3MatrixNonZero () {
32
+ int [][] matrix = {
33
+ {1 , 2 , 3 },
34
+ {0 , 1 , 4 },
35
+ {5 , 6 , 0 }
36
+ };
37
+ int expected = 1 ;
38
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 3 ));
39
+ }
40
+
41
+ @ Test
42
+ public void testDeterminant1x1Matrix () {
43
+ int [][] matrix = {
44
+ {7 }
45
+ };
46
+ int expected = 7 ;
47
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 1 ));
48
+ }
49
+
50
+ @ Test
51
+ public void testDeterminant4x4Matrix () {
52
+ int [][] matrix = {
53
+ {1 , 0 , 0 , 1 },
54
+ {0 , 1 , 0 , 0 },
55
+ {0 , 0 , 1 , 0 },
56
+ {1 , 0 , 0 , 1 }
57
+ };
58
+ int expected = 0 ;
59
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 4 ));
60
+ }
61
+
62
+ @ Test
63
+ public void testDeterminant4x4MatrixZero () {
64
+ int [][] matrix = {
65
+ {1 , 2 , 3 , 4 },
66
+ {5 , 6 , 7 , 8 },
67
+ {9 , 10 , 11 , 12 },
68
+ {13 , 14 , 15 , 16 }
69
+ };
70
+ int expected = 0 ;
71
+ assertEquals (expected , DeterminantOfMatrix .determinant (matrix , 4 ));
72
+ }
73
+ }
74
+
You can’t perform that action at this time.
0 commit comments