Finding inverse of a matrix using Gauss Jordan Method in C++


In this problem, we are given an 2D matrix mat[][]. Our task is to find inverse of a matrix using Gauss Jordan Method.

Now, lets understand the basics of the problem,

MATRIX is a two dimensional array of numbers.

Example

$\begin{bmatrix}2&5&4 \1&6&7 \9&3&8\end{bmatrix}$

Inverse of Matrix [A-1]

It is an operation performed on square matrix. The following are the properties that are required for a matrix to have an inverse −

  • Initial matrix should be square matrix.

  • It must be non-singular matrix.

  • An identity matrix I exist for the matrix A such that,

$$AA^{-1} = A^{-1}.A = I$$

Their is a formula that can be used to find the inverse of ant given matrix. It is

$A^{-1}\:=\:\left(\frac{adj(A)}{\det(A)}\right)$

adj(A) is adjoint of matrix A

det(A) is determinant of matrix A.

Their are multiple ways using with we can find the inverse of a matrix. In this article, we will be learning about Gauss Jordan Method which is also known as Elementary Row Opeation.

It is a step by step method to find the inverse of a matrix, Here are the steps involved −

  • Finding augmented matrix using the identity matrix.

  • Find the echelon form of the matrix by performing row reduction operation on the augmented matrix found in step 1.

  • Some operation that can be performed on the augmented matrix in the process are

    • Row interchange (you can interchange any two rows)

    • Multiplication (each elements of the row can be multiplied by a constant values other than 0).

    • Row Interchange (replace the row with the sum of the row and constant multiple of another row of matrix).

Example

Program to illustrate the working of our solution

#include <iostream>
#include <vector>
using namespace std;
void printMatrixValues(float** arr, int n, int m){
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
         cout<<arr[i][j]<<"\t";
      }
      cout<<endl;
   }
   return;
}
void printInverseMatrix(float** arr, int n, int m){
   for (int i = 0; i < n; i++) {
      for (int j = n; j < m; j++) {
         printf("%.3f\t", arr[i][j]);
      }
      cout<<endl;
   }
   return;
}
void findInvMatGaussJordan(float** mat, int order){
   float temp;
   printf("The inverse of matrix : A = \n");
   printMatrixValues(mat, order, order);
   for (int i = 0; i < order; i++) {
      for (int j = 0; j < 2 * order; j++) {
         if (j == (i + order))
            mat[i][j] = 1;
      }
   }
   for (int i = order - 1; i > 0; i--) {
      if (mat[i - 1][0] < mat[i][0]) {
         float* temp = mat[i];
         mat[i] = mat[i - 1];
         mat[i - 1] = temp;
      }
   }
   for (int i = 0; i < order; i++) {
      for (int j = 0; j < order; j++) {
         if (j != i) {
            temp = mat[j][i] / mat[i][i];
            for (int k = 0; k < 2 * order; k++) {
               mat[j][k] -= mat[i][k] * temp;
            }
         }
      }
   }
   for (int i = 0; i < order; i++) {
      temp = mat[i][i];
      for (int j = 0; j < 2 * order; j++) {
         mat[i][j] = mat[i][j] / temp;
      }
   }
   cout<<"A' =\n";
   printInverseMatrix(mat, order, 2 * order);
   return;
}
int main(){
   int order = 3;
   float** mat = new float*[20];
   for (int i = 0; i < 20; i++)
   mat[i] = new float[20];
   mat[0][0] = 6; mat[0][1] = 9; mat[0][2] = 5;
   mat[1][0] = 8; mat[1][1] = 3; mat[1][2] = 2;
   mat[2][0] = 1; mat[2][1] = 4; mat[2][2] = 7;
   findInvMatGaussJordan(mat, order);
   return 0;
}

Output

The inverse of matrix : A =
6 9 5
8 3 2
1 4 7
A' =
-0.049  0.163  -0.011
0.205  -0.141  -0.106
-0.110  0.057  0.205

Updated on: 01-Feb-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements