Skip to content

Commit f93a562

Browse files
committed
fix(matrix): Ensure matrix is square and handle non-square matrices with an exception
1 parent eebdf56 commit f93a562

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

src/main/java/com/thealgorithms/matrix/MatrixDeterminant.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
// - You can use recursion or any other suitable algorithm.
1919
// - Bonus: Optimize for larger matrices (optional).
2020

21-
package matrix;
21+
package com.thealgorithms.matrix;
2222

2323
public class MatrixDeterminant {
2424

2525
public static double determinant(double[][] m) {
2626
int n = m.length;
27+
for (double[] row : m) {
28+
if (row.length != n) {
29+
throw new IllegalArgumentException("Matrix must be square");
30+
}
31+
}
2732
if (n == 1) return m[0][0];
2833
if (n == 2) return m[0][0]*m[1][1] - m[0][1]*m[1][0];
2934

@@ -49,11 +54,4 @@ private static double[][] minor(double[][] m, int row, int col) {
4954
}
5055
return min;
5156
}
52-
53-
public static void main(String[] args) {
54-
double[][] a = {{1,2},{3,4}};
55-
double[][] b = {{2,0,1},{3,0,0},{5,1,1}};
56-
System.out.println(determinant(a)); // -2
57-
System.out.println(determinant(b)); // 3
58-
}
5957
}
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
package matrix;
1+
package com.thealgorithms.matrix;
22

3-
public class MatrixDeterminant {
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
45

5-
public static double determinant(double[][] m) {
6-
int n = m.length;
7-
if (n == 1) return m[0][0];
8-
if (n == 2) return m[0][0]*m[1][1] - m[0][1]*m[1][0];
6+
class MatrixDeterminantTest {
97

10-
double det = 0;
11-
for (int c = 0; c < n; c++) {
12-
det += Math.pow(-1, c) * m[0][c] * determinant(minor(m, 0, c));
13-
}
14-
return det;
8+
@Test
9+
void test2x2Matrix() {
10+
double[][] matrix = {{1, 2}, {3, 4}};
11+
assertEquals(-2, MatrixDeterminant.determinant(matrix), 1e-9);
1512
}
1613

17-
private static double[][] minor(double[][] m, int row, int col) {
18-
int n = m.length;
19-
double[][] min = new double[n-1][n-1];
20-
int r = 0;
21-
for (int i = 0; i < n; i++) {
22-
if (i == row) continue;
23-
int c = 0;
24-
for (int j = 0; j < n; j++) {
25-
if (j == col) continue;
26-
min[r][c++] = m[i][j];
27-
}
28-
r++;
29-
}
30-
return min;
14+
@Test
15+
void test3x3Matrix() {
16+
double[][] matrix = {{2, 0, 1}, {3, 0, 0}, {5, 1, 1}};
17+
assertEquals(3, MatrixDeterminant.determinant(matrix), 1e-9);
3118
}
3219

33-
public static void main(String[] args) {
34-
double[][] a = {{1,2},{3,4}};
35-
double[][] b = {{2,0,1},{3,0,0},{5,1,1}};
36-
System.out.println(determinant(a)); // -2
37-
System.out.println(determinant(b)); // 3
20+
@Test
21+
void test1x1Matrix() {
22+
double[][] matrix = {{5}};
23+
assertEquals(5, MatrixDeterminant.determinant(matrix), 1e-9);
24+
}
25+
26+
@Test
27+
void testSingularMatrix() {
28+
double[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
29+
assertEquals(0, MatrixDeterminant.determinant(matrix), 1e-9);
30+
}
31+
32+
@Test
33+
void testNonSquareMatrix() {
34+
double[][] matrix = {{1, 2, 3}, {4, 5, 6}};
35+
assertThrows(IllegalArgumentException.class, () -> MatrixDeterminant.determinant(matrix));
3836
}
3937
}

0 commit comments

Comments
 (0)