C Program to Find Determinant of a Matrix
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
// C program to find Determinant // of a matrix #include <stdio.h> // Dimension of input square matrix #define N 4 // Function to get cofactor of mat[p][q] // in temp[][]. n is current dimension // of mat[][] void getCofactor( int mat[N][N], int temp[N][N], int p, int q, int n) { int i = 0, j = 0; // Looping for each element of the matrix for ( int row = 0; row < n; row++) { for ( int col = 0; col < n; col++) { // Copying into temporary matrix // only those element which are // not in given row and column if (row != p && col != q) { temp[i][j++] = mat[row][col]; // Row is filled, so increase row // index and reset col index if (j == n - 1) { j = 0; i++; } } } } } /* Recursive function for finding the determinant of matrix. n is current dimension of mat[][]. */ int determinantOfMatrix( int mat[N][N], int n) { // Initialize result int D = 0; // Base case : if matrix contains // single element if (n == 1) return mat[0][0]; // To store cofactors int temp[N][N]; // To store sign multiplier int sign = 1; // Iterate for each element of // first row for ( int f = 0; f < n; f++) { // Getting Cofactor of mat[0][f] getCofactor(mat, temp, 0, f, n); D += sign * mat[0][f] * determinantOfMatrix(temp, n - 1); // Terms are to be added with alternate sign sign = -sign; } return D; } // Function for displaying the matrix 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" ); } } // Driver code int main() { int mat[N][N] = {{1, 0, 2, -1}, {3, 0, 0, 5}, {2, 1, 4, -3}, {1, 0, 5, 0}}; // Function call printf ( "Determinant of the matrix is : %d" , determinantOfMatrix(mat, N)); return 0; } |
Output
Determinant 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++
// C program to find Determinant of a matrix #include <stdio.h> #include<stdlib.h> #include<math.h> // Dimension of input square matrix #define N 4 // This function swaps values pointed by xp and yp void swap( int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } // Function to get determinant of matrix int determinantOfMatrix( int mat[N][N], int n) { int num1, num2, det = 1, index, total = 1; // Initialize result // temporary array for storing row int temp[n + 1]; // loop for traversing the diagonal elements for ( int i = 0; i < n; i++) { index = i; // initialize the index // finding the index which has non zero value while (index < n && mat[index][i] == 0) { index++; } if (index == n) // if there is non zero element { // the determinant of matrix as zero continue ; } if (index != i) { // loop for swapping the diagonal element row and // index row for ( int j = 0; j < n; j++) { swap(&mat[index][j], &mat[i][j]); } // determinant sign changes when we shift rows // go through determinant properties det = det * pow (-1, index - i); } // storing the values of diagonal row elements for ( int j = 0; j < n; j++) { temp[j] = mat[i][j]; } // traversing every row below the diagonal element for ( int j = i + 1; j < n; j++) { num1 = temp[i]; // value of diagonal element num2 = mat[j][i]; // value of next row element // traversing every column of row // and multiplying to every row for ( int k = 0; k < n; k++) { // multiplying to make the diagonal // element and next row element equal mat[j][k] = (num1 * mat[j][k]) - (num2 * temp[k]); } total = total * num1; // Det(kA)=kDet(A); } } // multiplying the diagonal elements to get determinant for ( int i = 0; i < n; i++) { det = det * mat[i][i]; } return (det / total); // Det(kA)/k=Det(A); } // Driver code int main() { /*int mat[N][N] = {{6, 1, 1}, {4, -2, 5}, {2, 8, 7}}; */ int mat[N][N] = { { 1, 0, 2, -1 }, { 3, 0, 0, 5 }, { 2, 1, 4, -3 }, { 1, 0, 5, 0 } }; // Function call printf ( "Determinant of the matrix is : %d" , determinantOfMatrix(mat, N)); return 0; } |
Output
Determinant 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
Contact Us