Java Program to Find the Determinant of a Matrix
An equal number of rows and columns in a matrix defines its determinant, which is a real number that can only be specified for square matrices. In addition, the inverse of the given matrix and the system of the linear equation can both be identified using it.
The determinant of a matrix is a number specially defined only for square matrices. Determinants are mathematical objects that are very useful in the analysis and solution of systems of linear equations. They are useful for solving linear equations and capturing how linear transformation changes area or volume.
Example 1:
Input:
int mat = [5, 1]
[3, 3]
Output:
The determinant of the given matrix is 6.
Explanation:
For the given 2*2 matrix, the determinant can be calculated as follows:
[5, 1]
[3, 3]
= (5*3) - (1*3)
= 15-3
= 12
Example 2:
Input:
int mat = [2, 1, -4]
[-1, 4, 1]
[2, 0, -2]
Output:
The determinant of the given matrix is 6.
Explanation:
For the given 3*3 matrix, the determinant can be calculated as follows:
[2, 1, -4]
[-1, 4, 1]
[2, 0, -2]
= 2(-8-0) -1(2-2) +(-4)(0-8)
= -2(-8) -1(0) + (-4)(-8)
= 16-0+32
= 48
Approach: Using Recursion
The element itself is the determinant of a 1*1 matrix. First, we must determine the cofactor of each matrix element in the first row or column. By removing the element's row and column from the given matrix, the cofactor of any element in the matrix can be found. Next, apply the corresponding cofactor to each element in the first row or first column. Subsequently, we must sum them up using different signs.
Implementation:
FileName: DetMatrixRecursion.java
import java.io.*;
import java.util.*;
public class DetMatrixRecursion
{
// Square matrix dimension of input
static final int N = 2;
// Function retrieves the cofactor
// of matrix[p][q] in temp[][].
//The current dimension of the matrix is denoted by num.
static void getCofactor(int matrix[][], int temp[][], int m, int n, int num)
{
int i = 0, j = 0;
// Looping this for every matrix element
for (int r = 0; r < num; r++)
{
for (int c = 0; c < num; c++)
{
// Copying only the elements
// that are not in the specified
// row and column into a temporary matrix
if (r != m && c != n)
{
temp[i][j++] = matrix[r][c];
// Since the row is filled,
// reset the column index and
// increase the row index.
if (j == num - 1)
{
j = 0;
i++;
}
}
}
}
}
// Determinant of matrix found by a recursive function.
// The matrix's current dimension is indicated by num[][].
static int determinant(int matrix[][], int num)
{
int D = 0;
// Set the initial result
// Initial case: If there is only
// one element in the matrix,
if (num == 1)
return matrix[0][0];
// for storing the cofactors
int temp[][] = new int[N][N];
// for storing the sign multiplier
int signmulti = 1;
// Loop for every element in the first row.
for (int first = 0; first < num; first++)
{
// getting the matrix's cofactor [0][First]
getCofactor(matrix, temp, 0, first, num);
D += signmulti * matrix[0][first] * determinant(temp, num - 1);
// Terms need to be added
// with an alternate indication.
signmulti = -signmulti;
}
return D;
}
// function that shows the matrix
static void display(int matrix[][], int r, int c)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
System.out.print(matrix[i][j]);
System.out.print("\n");
}
}
public static void main(String[] args)
{
int matrix[][] = { { 5, 1 }, { 3, 3 } };
System.out.print("The Determinant of the given matrix is: " + determinant(matrix, N));
}
}
Output:
The Determinant of the given matrix is: 12
Complexity Analysis:
The time complexity is O(N3), and the space complexity is O(N), where N represents the dimension of the matrix.
Approach: Non-Recursive Procedure
The Java code provided computes a square matrix's determinant in a non-recursive manner. Iteratively traversing over the matrix, it executes row operations to minimize it to upper triangular form while checking sign changes in the determinant. In order to get the determinant value, it multiplies the diagonal elements last. The determinant computation is accomplished without recursion by means of matrix operations and effective row shifting in the code.
Implementation:
FileName: DetMatWithoutRecursion.java
import java.io.*;
import java.util.*;
public class DetMatWithoutRecursion
{
// Square matrix dimension of the input
static final int N = 2;
// Function to obtain the matrix determinant
static int determinant(int matrix[][], int num)
{
int n1, n2, det = 1, ind, result = 1; // Initialize result
// Row is stored in a temporary array.
int[] temp = new int[num + 1];
// loop to iterate over the diagonal elements
for (int i = 0; i < num; i++)
{
ind = i; // initialization of the index
// identifying the index with a value that is non zero
while (matrix[ind][i] == 0 && ind < num)
{
ind++;
}
if (ind == num) // If an element is not zero, then
{
//The matrix's determinant is zero
continue;
}
if (ind != i)
{
// loop for switching the index row and diagonal element row
for (int j = 0; j < num; j++)
{
swap(matrix, ind, j, i, j);
}
// The sign of the determinant changes as we move
// rows pass through aspects of that determinant
det = (int)(det * Math.pow(-1, ind - i));
}
// storing the diagonal row items' values
for (int j = 0; j < num; j++)
{
temp[j] = matrix[i][j];
}
// traversing each row that is below the diagonal
for (int j = i + 1; j < num; j++)
{
n1 = temp[i]; // value of the diagonal element
n2 = matrix[j][i]; // value of the next row element
// iterating through each row's column
// and multiplying by each row
for (int k = 0; k < num; k++)
{
// multiplication to equalize the diagonal
// element and the element in the next row
matrix[j][k] = (n1 * matrix[j][k]) - (n2 * temp[k]);
}
result = result * n1; // Det(kA)=kDet(A);
}
}
// determining the determinant by
// multiplying the diagonal elements
for (int i = 0; i < num; i++)
{
det = det * matrix[i][i];
}
return (det / result); // Det(kA)/k=Det(A);
}
static int[][] swap(int[][] a, int i1, int j1, int i2, int j2)
{
int temp = a[i1][j1];
a[i1][j1] = a[i2][j2];
a[i2][j2] = temp;
return a;
}
public static void main(String[] args)
{
int matrix[][] = { { 5, 1 }, { 3, 3 } };
System.out.print("The Determinant of the given matrix is: " + determinant(matrix, N));
}
}
Output:
The Determinant of the given matrix is: 12
Complexity Analysis:
The time complexity is O(N3), and the space complexity is O(N), where N represents the dimension of the matrix.