
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Matrix is Invertible in C++
The determinant of a matrix can be used to find if it is invertible or not. The matrix is invertible if the determinant is non-zero. So if the determinant comes out to be zero, the matrix is not invertible. For example −
The given matrix is: 4 2 1 2 1 1 9 3 2 The determinant of the above matrix is: 3 So the matrix is invertible.
A program that checks if a matrix is invertible or not is as follows.
Example
#include<iostream> #include<math.h> using namespace std; int determinant( int matrix[10][10], int n) { int det = 0; int submatrix[10][10]; if (n == 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1])); else { for (int x = 0; x < n; x++) { int subi = 0; for (int i = 1; i < n; i++) { int subj = 0; for (int j = 0; j < n; j++) { if (j == x) continue; submatrix[subi][subj] = matrix[i][j]; subj++; } subi++; } det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 )); } } return det; } int main() { int n, d, i, j; int matrix[10][10]; cout << "Enter the size of the matrix:\n"; cin >> n; cout << "Enter the elements of the matrix:\n"; for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> matrix[i][j]; cout<<"The entered matrix is:"<<endl; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) cout << matrix[i][j] <<" "; cout<<endl; } d = determinant(matrix, n); cout<<"Determinant of the matrix is "<< d <<endl; if( d == 0 ) cout<<"This matrix is not invertible as the determinant is zero"; else cout<<"This matrix is invertible as the determinant is not zero"; return 0; }
output
Enter the size of the matrix: 3 Enter the elements of the matrix: 1 2 3 2 1 2 1 1 4 The entered matrix is: 1 2 3 2 1 2 1 1 4 Determinant of the matrix is -7 This matrix is invertible as the determinant is not zero
In the above program, the size and elements of the matrix are provided in the main() function. Then the function determinant() is called. It returns the determinant of the matrix which is stored in d. If the determinant is 0, then the matrix is not invertible and if the determinant is not 0, then the matrix is invertible. This is demonstrated with the following code snippet.
cout << "Enter the size of the matrix:\n"; cin >> n; cout << "Enter the elements of the matrix:\n"; for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> matrix[i][j]; cout<<"The entered matrix is:"<<endl; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) cout << matrix[i][j] <<" "; cout<<endl; } d = determinant(matrix, n); cout<<"Determinant of the matrix is "<< d <<endl; if( d == 0 ) cout<<"This matrix is not invertible as the determinant is zero"; else cout<<"This matrix is invertible as the determinant is not zero";
In the function determinant(), if the size of the matrix is 2, then the determinant is directly calculated and the value is returned. This is shown as follows.
if (n == 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
If the size of the matrix is not 2, then the determinant is calculated recursively. There are 3 nested for loops used with the loop variables x, i and j. These loops are used to calculate the determinant and the function determinant() is called recursively to calculate the inner determinant and then multiply it with the outer value. This is demonstrated by the following code snippet.
for (int x = 0; x < n; x++) { int subi = 0; for (int i = 1; i < n; i++) { int subj = 0; for (int j = 0; j < n; j++) { if (j == x) continue; submatrix[subi][subj] = matrix[i][j]; subj++; } subi++; } det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 )) }