- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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++
Here we will see how to check whether a matrix is invertible or not. If one matrix is M, then the inverted matrix M-1 will be −
$$M^-1=\frac{adj(M)}{|M\lvert}$$
So if the determinant of M is non-zero, then only we can get the inverse, otherwise, we will not get the inverse of it. So here we have to check the determinant is non-zero or not. Finding a determinant is one recursive process. We have to find submatrix, then find the determinant of it, then use that result for the final calculation. Let us see the code to get a better idea.
Example
#include <iostream> #define N 4 using namespace std; void findCoFactor(int mat[N][N], int mat2[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) { mat2[i][j++] = mat[row][col]; if (j == n - 1) { j = 0; i++; } } } } } int getDeterminant(int mat[N][N], int n) { int determinant = 0; if (n == 1) return mat[0][0]; int temp[N][N]; int sign = 1; for (int f = 0; f < n; f++) { findCoFactor(mat, temp, 0, f, n); determinant += sign * mat[0][f] * getDeterminant(temp, n - 1); sign = -sign; } return determinant; } bool isMatrixInvertible(int mat[N][N], int n) { if (getDeterminant(mat, N) != 0) return true; else return false; } int main() { int matrix[N][N] = { { 1, 0, 2, -1 }, { 3, 0, 0, 5 }, { 2, 1, 4, -3 }, { 1, 0, 5, 0 } }; if (isMatrixInvertible(matrix, N)) cout << "The matrix is invetible"; else cout << "The matrix is not invetible"; }
Output
The matrix is invetible
- Related Articles
- C++ Program to Check if a Matrix is Invertible
- How to check if a matrix is invertible or not in R?
- Program to check if a matrix is Binary matrix or not in C++
- Program to check if a matrix is symmetric in C++
- Check if a given matrix is sparse or not in C++
- Check if a given matrix is Hankel or not in C++
- C++ Program to Check if it is a Sparse Matrix
- Program to check if matrix is lower triangular in C++
- Program to check if matrix is upper triangular in C++
- C Program to check if matrix is singular or not
- Check if a pair with given product exists in a Matrix in C++
- How to check if a matrix in R is in binary form?
- Signals and Systems: Invertible and Non-Invertible Systems
- How to check whether the given matrix is a Toeplitz matrix using C#?
- Check if a File is hidden in C#

Advertisements