
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Determinant of a Matrix in C++?
The determinant of a matrix can be calculated only for a square matrix by multiplying the first row cofactor by the determinant of the corresponding cofactor and adding them with alternate signs to get the final result.
$$A = \begin{bmatrix}a & b & c\d & e &f \g & h &i \ \end{bmatrix} |A| = a(ei-fh)-b(di-gf)+c(dh-eg)$$
First we have the determinantOfMatrix(int mat[N][N], int dimension) function that takes the matrix and the dimension value of the matrix. If the matrix is of only 1 dimension then it returns the [0][0] matrix value. This condition is also used as a base condition since we recursively iterate our matrix with reducing the dimensions on each recursive call.
int determinantOfMatrix(int mat[N][N], int dimension){ int Det = 0; if (dimension == 1) return mat[0][0];
We then declare the cofactorMat[N][N] which will be passed to the cofactor(int mat[N][N], int temp[N][N], int p, int q, int n) function till the firstRow is less then the dimension. The determinant of the matrix is stored in the Det variable with signs alternating on each for loop iteration. This det is then returned to the main function where it is printed.
int cofactorMat[N][N]; int sign = 1; for (int firstRow = 0; firstRow < dimension; firstRow++){ cofactor(mat, cofactorMat, 0, firstRow, dimension); Det += sign * mat[0][firstRow] * determinantOfMatrix(cofactorMat, dimension - 1); sign = -sign; } return Det; }
The cofactor(int mat[N][N], int temp[N][N], int p,int q, int n) function takes the matrix, the cofactor matrix, 0, firstRow value and the dimension of the matrix as parameter values. The nested for loop then helps us iterate over the matrix and where the p & q values are not equal to row and column values respectively, those values are stored in the temp matrix.
void cofactor(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 column = 0; column < n; column++){ if (row != p && column != q){ temp[i][j++] = mat[row][column];
Once the row gets filled we increase the row index and reset the col index.
if (j == n - 1){ j = 0; i++; }
Finally we have our display(int mat[N][N], int row, int col) that takes the matrix and no of rows and columns and iterate over the matrix as 2d array and print those values on each row and column.
void display(int mat[N][N], int row, int col){ for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++) cout<<mat[i][j]<<" "; cout<<endl; } cout<<endl; }
Example
Let us look at the following implementation to find the determinant of the matrix.
#include <iostream> using namespace std; const int N = 3; void cofactor(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 column = 0; column < n; column++){ if (row != p && column != q){ temp[i][j++] = mat[row][column]; if (j == n - 1){ j = 0; i++; } } } } } int determinantOfMatrix(int mat[N][N], int dimension){ int Det = 0; if (dimension == 1) return mat[0][0]; int cofactorMat[N][N]; int sign = 1; for (int firstRow = 0; firstRow < dimension; firstRow++){ cofactor(mat, cofactorMat, 0, firstRow, dimension); Det += sign * mat[0][firstRow] * determinantOfMatrix(cofactorMat, dimension - 1); sign = -sign; } return Det; } void display(int mat[N][N], int row, int col){ for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++) cout<<mat[i][j]<<" "; cout<<endl; } cout<<endl; } int main(){ int mat[3][3] = { { 1, 0, 2}, { 3, 0, 0}, { 2, 1, 4}}; cout<<"The matrix is "<<endl; display(mat,3,3); cout<<"Determinant of the matrix is "<<determinantOfMatrix(mat, N); return 0; }
Output
The above code will produce the following output −
The matrix is 1 0 2 3 0 0 2 1 4 Determinant of the matrix is 6
- Related Articles
- Determinant of a Matrix in C++ Program
- C++ Program to Compute Determinant of a Matrix
- Finding determinant of a square matrix using SciPy library
- Maximum determinant of a matrix with every values either 0 or n in C++
- PyTorch – How to compute the determinant of a square matrix?
- Python Program to find out the determinant of a given special matrix
- How can SciPy be used to calculate the determinant value of a matrix in Python?
- Compute the determinant of a Two-Dimensional array in linear algebra in Python
- Compute the determinant for a stack of matrices in linear algebra in Python
- Implementation of a Falling Matrix in C++
- Compute the determinant of an array in linear algebra in Python
- Transpose a matrix in C#
- Mean and Median of a matrix in C++
- Find orientation of a pattern in a matrix in C++
- Find number of cavities in a matrix in C++
