C Program to Find Determinant of a Matrix
Last Updated :
15 Mar, 2023
What is the Determinant of a Matrix?
The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the matrix in terms of a real number which can be used in solving a system of a linear equation and finding the inverse of a matrix.
How to calculate?
The value of the determinant of a matrix can be calculated by the following procedure –
For each element of the first row or first column get the cofactor of those elements and then multiply the element with the determinant of the corresponding cofactor, and finally add them with alternate signs. As a base case, the value of the determinant of a 1*1 matrix is the single value itself.
Cofactor of an element is a matrix that we can get by removing the row and column of that element from that matrix.
Determinant of 2 x 2 Matrix:
Determinant of 3 x 3 Matrix:
C
#include <stdio.h>
#define N 4
void getCofactor( int mat[N][N], int temp[N][N],
int p, int q, int n)
{
int i = 0, j = 0;
for ( int row = 0; row < n; row++)
{
for ( int col = 0; col < n; col++)
{
if (row != p && col != q)
{
temp[i][j++] = mat[row][col];
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
int determinantOfMatrix( int mat[N][N], int n)
{
int D = 0;
if (n == 1)
return mat[0][0];
int temp[N][N];
int sign = 1;
for ( int f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return D;
}
void display( int mat[N][N],
int row, int col)
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
printf ( " %d" , mat[i][j]);
printf ( "n" );
}
}
int main()
{
int mat[N][N] = {{1, 0, 2, -1},
{3, 0, 0, 5},
{2, 1, 4, -3},
{1, 0, 5, 0}};
printf ( "Determinant of the matrix is : %d" ,
determinantOfMatrix(mat, N));
return 0;
}
|
OutputDeterminant of the matrix is : 30
Time Complexity: O(n4)
Space Complexity: O(n2), Auxiliary space used for storing cofactors.
Efficient Approach:
Determinant of a Matrix using Determinant properties:
- In this method, we are using the properties of Determinant.
- Converting the given matrix into an upper triangular matrix using determinant properties
- The determinant of the upper triangular matrix is the product of all diagonal elements.
- Iterating every diagonal element and making all the elements down the diagonal as zero using determinant properties
- If the diagonal element is zero then search for the next non-zero element in the same column.
There exist two cases:
- Case 1: If there is no non-zero element. In this case, the determinant of a matrix is zero
- Case 2: If there exists a non-zero element there exist two cases:
- Case A: If the index is with a respective diagonal row element. Using the determinant properties make all the column elements down to it zero
- Case B: Swap the row with respect to the diagonal element column and continue the Case A operation.
Below is the implementation of the above approach:
C++
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 4
void swap( int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int determinantOfMatrix( int mat[N][N], int n)
{
int num1, num2, det = 1, index,
total = 1;
int temp[n + 1];
for ( int i = 0; i < n; i++)
{
index = i;
while (index < n && mat[index][i] == 0)
{
index++;
}
if (index == n)
{
continue ;
}
if (index != i)
{
for ( int j = 0; j < n; j++)
{
swap(&mat[index][j], &mat[i][j]);
}
det = det * pow (-1, index - i);
}
for ( int j = 0; j < n; j++)
{
temp[j] = mat[i][j];
}
for ( int j = i + 1; j < n; j++)
{
num1 = temp[i];
num2 = mat[j][i];
for ( int k = 0; k < n; k++)
{
mat[j][k]
= (num1 * mat[j][k]) - (num2 * temp[k]);
}
total = total * num1;
}
}
for ( int i = 0; i < n; i++)
{
det = det * mat[i][i];
}
return (det / total);
}
int main()
{
int mat[N][N] = { { 1, 0, 2, -1 },
{ 3, 0, 0, 5 },
{ 2, 1, 4, -3 },
{ 1, 0, 5, 0 } };
printf ( "Determinant of the matrix is : %d" ,
determinantOfMatrix(mat, N));
return 0;
}
|
OutputDeterminant of the matrix is : 30
Time Complexity: O(N*N*N), where N is the size of the matrix
Space Complexity: O(N) as temp array has been created to store row.
For more details, refer to the article – Determinant of a Matrix
Share your thoughts in the comments
Please Login to comment...