Check if a Matrix is Invertible in C++



A matrix is invertible if it has an inverse. An inverse of a matrix exists only when its determinant is non-zero. If the determinant of a matrix is zero, the matrix is called singular and cannot be inverted. In this article, we'll write a C++ program to check if a matrix is invertible using its determinant.

To better understand this, let's take the following 3x3 matrix as an example:

Given 3*3 Matrix:
4  2  1  
2  1  1  
9  3  2
To check if this matrix is invertible, we calculate its determinant using the formula for a 3x3 matrix:

Determinant = 4 * (1 * 2 - 1 * 3) - 2 * (2 * 2 - 1 * 9) + 1 * ( 2 * 3 - 1 * 9)
= 4 * (-1) - 2 * (-5) + 1 * (-3) 
= -4 + 10 - 3 = 3

Since the determinant = 3 (non-zero), the matrix is invertible.

Steps to Check if a Matrix is Invertible

To check whether a matrix is invertible, we need to calculate its determinant. If the determinant is zero, the matrix is not invertible. Otherwise, it is invertible.

  • First, we define a determinant function that takes a square matrix and its size.
  • Second, if the size is 1x1, we return the single element; if it's 2x2, we return (a * d - b * c).
  • Next, for larger matrices (3x3 or more), we create submatrices by removing the row and column of each element in the first row, then call the function recursively on those submatrices.
  • Then, we apply the Laplace expansion by summing each element in the first row multiplied by the determinant of its submatrix, applying plus or minus signs based on their positions.
  • Finally, we check the determinant value. If it is zero, the matrix is singular and not invertible; otherwise, it is invertible, and we display the result accordingly.

C++ Program to Check if a Matrix is Invertible

Below is a complete C++ program where we check if a square matrix is invertible by calculating its determinant and showing the result.

#include<iostream>
#include<math.h>
using namespace std;

// Function to calculate the determinant of a matrix
int determinant(int matrix[10][10], int n) {
   int det = 0;
   int submatrix[10][10];

   // Base case: if matrix is 2x2, return direct determinant
   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++;
         }
         // Recursively calculate determinant using cofactor expansion
         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];

   // Input: size of the matrix
   cout << "Enter the size of the matrix:\n";
   cin >> n;

   // Input: elements of the matrix
   cout << "Enter the elements of the matrix:\n";
   for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
         cin >> matrix[i][j];

   // Display the entered matrix
   cout << "The entered matrix is:" << endl;
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++)
         cout << matrix[i][j] << " ";
      cout << endl;
   }

   // Calculate and print the determinant
   d = determinant(matrix, n);
   cout << "Determinant of the matrix is " << d << endl;

   // Check if the matrix is invertible
   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;
}

The output of the above program is shown below. It's just a sample output for your understanding. You can enter the matrix according to your choice and check.

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.

Time Complexity: O(n!) because the function recursively computes determinants of n smaller (n-1)x(n-1) submatrices.

Space Complexity: O(n^3) because of storing submatrices at each recursion level.

Updated on: 2025-05-30T18:04:19+05:30

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements