Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 51 additions & 55 deletions src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
package com.thealgorithms.maths;

import java.util.*;

/*
* @author Ojasva Jain
* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant
*/
public class DeterminantOfMatrix {

// Determinant calculator
//@return determinant of the input matrix
static int determinant(int[][] a, int n) {
int det = 0, sign = 1, p = 0, q = 0;
if (n == 1) {
det = a[0][0];
} else {
int[][] b = new int[n - 1][n - 1];
for (int x = 0; x < n; x++) {
p = 0;
q = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != x) {
b[p][q++] = a[i][j];
if (q % (n - 1) == 0) {
p++;
q = 0;
}
}
}
}
det = det + a[0][x] * determinant(b, n - 1) * sign;
sign = -sign;
}
}
return det;
}

// Driver Method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Input Matrix
System.out.println("Enter matrix size (Square matrix only)");
int n = in.nextInt();
System.out.println("Enter matrix");
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}
System.out.println(determinant(a, n));
}
}
package com.thealgorithms.maths;
import java.util.*;
/*
* Determinant of Matrix :https://textbooks.math.gatech.edu/ila/determinants-cofactors.html
*/
public class DeterminantOfMatrix {
public static double[][] submatrix(double[][] mat,int order,int c) {
//creating reference for the sub matrix
double[][] subMat=new double[order-1][order-1];
//sub_c stores the column column index of the sub matrix
int subC;
for(int i=1;i<order;i++) {
//making the column count zero after each row-addition
subC=0;
for(int k=0;k<order;k++) {
if(k==c) {
continue;//skipping the column where the mat[0][c] of operation is in
}
subMat[i-1][subC]=mat[i][k];
subC+=1;//increasing the column count
}
}
return(subMat);
}
public static double determinant(double[][] mat,int order) {
/*determinant of the matrix with one element(order 1) :
is the element itself*/
if (order==1) {
return(mat[0][0]);
}
return(operation(mat,order));
}
public static double operation(double[][] mat,int order) {
int check;
double det=0;
for(int c=0;c<order;++c) {
if(c%2==0) {
check=1;
}
else {
check=-1;
}
det+=(check*(mat[0][c])*determinant(submatrix(mat,order,c),order-1));
}
return(det);
}
}
/*the above code will work for all square matrix whose order are greater than one.
---to initiate this recursive algorithm pass the matrix and its order to the operation() method
---if the order is 1 return the determinant of the matrix directly
(determinant of the matrix with one element(order 1) :is the element itself)*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class DeterminantOfMatrixTest {

@Test
void testDeterminantOfMatrix() {
double[][] matrix1 = { { 1, 0, 0}, {0, 1, 0}, {0, 0, 1} };
assertEquals(1.0, DeterminantOfMatrix.operation(matrix1, 3));

double[][] matrix2 = { { 2.0, 3.0 }, { -4.0, 5.0 } };
assertEquals(22.0, DeterminantOfMatrix.operation(matrix2, 2));

double[][] matrix3 = { { -1.0, 2.5, 3.0 }, { 4.0, -5.2, 6.0 }, { 7.3, -8.1, 9.6 } };
assertEquals(31.50000000000001, DeterminantOfMatrix.operation(matrix3, 3));

double[][] matrix4 = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1, 2}, {3, 4, 5, 6} };
assertEquals(0.0, DeterminantOfMatrix.operation(matrix4, 4));

}
}