Skip to content

Commit c3a44df

Browse files
committed
feat(matrix): Implement determinant calculation for square matrices
1 parent 2da0465 commit c3a44df

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Problem: Determinant of a Matrix
2+
//
3+
// Implement a function to calculate the determinant of a square matrix.
4+
//
5+
// Requirements:
6+
// - The function should work for any n x n matrix.
7+
// - Return 0 if the matrix is singular (determinant = 0).
8+
// - Only square matrices are allowed; handle error for non-square matrices.
9+
//
10+
// Example:
11+
// double[][] matrix1 = {{1,2},{3,4}};
12+
// determinant(matrix1); // returns -2
13+
//
14+
// double[][] matrix2 = {{2,0,1},{3,0,0},{5,1,1}};
15+
// determinant(matrix2); // returns 3
16+
//
17+
// Notes:
18+
// - You can use recursion or any other suitable algorithm.
19+
// - Bonus: Optimize for larger matrices (optional).
20+
21+
package matrix;
22+
23+
public class MatrixDeterminant {
24+
25+
public static double determinant(double[][] m) {
26+
int n = m.length;
27+
if (n == 1) return m[0][0];
28+
if (n == 2) return m[0][0]*m[1][1] - m[0][1]*m[1][0];
29+
30+
double det = 0;
31+
for (int c = 0; c < n; c++) {
32+
det += Math.pow(-1, c) * m[0][c] * determinant(minor(m, 0, c));
33+
}
34+
return det;
35+
}
36+
37+
private static double[][] minor(double[][] m, int row, int col) {
38+
int n = m.length;
39+
double[][] min = new double[n-1][n-1];
40+
int r = 0;
41+
for (int i = 0; i < n; i++) {
42+
if (i == row) continue;
43+
int c = 0;
44+
for (int j = 0; j < n; j++) {
45+
if (j == col) continue;
46+
min[r][c++] = m[i][j];
47+
}
48+
r++;
49+
}
50+
return min;
51+
}
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+
}
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package matrix;
2+
3+
public class MatrixDeterminant {
4+
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];
9+
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;
15+
}
16+
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;
31+
}
32+
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
38+
}
39+
}

0 commit comments

Comments
 (0)