How to Inverse A Matrix in C++
This article will explain the matrix inverse and its implementation using C++. To easily understand the C++ implementation, we need to understand the concept of the matrix inverse first.
The Inverse of a Matrix
There are three steps to finding the inverse of the matrix. The explanation of the steps is given below.
-
In the first step, compute the determinant of the given matrix.
-
In the second step, compute the adjoint of the given matrix if the determinant is not equal to zero.
-
Finally, multiply the matrix obtained in Step 2 with
1/determinant
.
Mathematical Formula of the Matrix Inverse
Let S be a 3 x 3 matrix. Then the formula to find its inverse is given below.
Note: We cannot find the inverse of a matrix if the determinant of the given matrix is zero (0), i.e., the matrix is singular.
Below is a program to find the inverse of a matrix of order 3x3 in C++. We have used an array for storing the matrix for simplicity.
The program will find the inverse of the matrix only if it is non-singular. We can verify our answer by the following property.
Here I
is the identity matrix having entry 1 in its diagonal. If the above property holds, our answer will be true; otherwise, false.
The proposed program is divided into modules.
C++ Implementation of Inverse for a 3x3 Matrix
The matrix inverse can be implemented using vectors, templates, and classes. However, for the sake of simplicity, we will use 2D arrays for determining the inverse of a 3x3 matrix.
The solution can then be generalized to find the inverse of the NxN matrices.
Let’s explain the constituents of the program one by one.
Main Function
int main() // main function
{
float matrix3X3[3][3];
int r, c;
float determinant = 0;
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++) {
cout << "Enter the value at index [" << r << "][" << c << "] : ";
cin >> matrix3X3[r][c];
}
}
display(matrix3X3);
determinant = findDeterminant(matrix3X3);
cout << "\n\nDeteterminant of the given matrix is : " << determinant;
if (determinant != 0) {
adjoint(matrix3X3);
inverse(matrix3X3, determinant);
} else {
cout << " As the determinant of the given matrix is 0, so we cannot take "
"find it's Inverse :";
}
}
In the main function, we have created the matrix whose inverse is to be found. Then, we applied conditional checks to determine if the matrix is singular or not.
If non-singular, then the inverse of the matrix will be calculated by function. Here is the explanation of each function we have created.
Display Function
void display(float matrix3X3[][3]) {
printf("\nThe Elements of the matrix are :");
for (int r = 0; r < 3; r++) {
cout << "\n";
for (int c = 0; c < 3; c++) {
cout << matrix3X3[r][c] << "\t";
}
}
}
The display function is used to display a matrix. Here matrix3X3
is the matrix whose values are to be displayed.
Determinant Calculation Function
float findDeterminant(float matrix3X3[][3]) {
float det = 0; // here det is the determinant of the matrix.
for (int r = 0; r < 3; r++) {
det = det + (matrix3X3[0][r] *
(matrix3X3[1][(r + 1) % 3] * matrix3X3[2][(r + 2) % 3] -
matrix3X3[1][(r + 2) % 3] * matrix3X3[2][(r + 1) % 3]));
}
return det;
}
The findDeterminant
function is used to find the determinant of the matrix. Here, det
is the matrix matrix3X3
determinant.
After seeing the determinant, we can conclude whether or not the matrix inverse is possible.
Adjoint Function
void adjoint(float matrix3X3[][3]) {
cout << "\n\nAdjoint of matrix is: \n";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
cout << ((matrix3X3[(c + 1) % 3][(r + 1) % 3] *
matrix3X3[(c + 2) % 3][(r + 2) % 3]) -
(matrix3X3[(c + 1) % 3][(r + 2) % 3] *
matrix3X3[(c + 2) % 3][(r + 1) % 3]))
<< "\t";
}
cout << endl;
}
}
The adjoint
function is used to find the adjoint of the matrix. After finding the adjoint matrix, we will multiply it with the reciprocal of the determinant to find the inverse.
Inverse Function
void inverse(float matrix3X3[][3], float d) {
cout << "\n\nInverse of matrix is: \n";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
cout << ((matrix3X3[(c + 1) % 3][(r + 1) % 3] *
matrix3X3[(c + 2) % 3][(r + 2) % 3]) -
(matrix3X3[(c + 1) % 3][(r + 2) % 3] *
matrix3X3[(c + 2) % 3][(r + 1) % 3])) /
d
<< "\t";
}
cout << endl;
}
}
Finally, the inverse function is used to calculate the inverse of the matrix. In this function, each entry of the adjoint matrix is divided by the determinant to find the matrix inverse.
Now let’s look at a complete code of the inverse of the 3x3 matrix.
Full C++ Program
#include <iostream>
using namespace std;
void display(float matrix3X3[][3]) {
printf("\nThe Elements of the matrix are :");
for (int r = 0; r < 3; r++) {
cout << "\n";
for (int c = 0; c < 3; c++) {
cout << matrix3X3[r][c] << "\t";
}
}
}
// This function will calculate the determinant of the given matrix
float findDeterminant(float matrix3X3[][3]) {
float det = 0; // here det is the determinant of the matrix.
for (int r = 0; r < 3; r++) {
det = det + (matrix3X3[0][r] *
(matrix3X3[1][(r + 1) % 3] * matrix3X3[2][(r + 2) % 3] -
matrix3X3[1][(r + 2) % 3] * matrix3X3[2][(r + 1) % 3]));
}
return det;
}
// This function will calculate the adjoint of the given matrix
void adjoint(float matrix3X3[][3]) {
cout << "\n\nAdjoint of matrix is: \n";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
cout << ((matrix3X3[(c + 1) % 3][(r + 1) % 3] *
matrix3X3[(c + 2) % 3][(r + 2) % 3]) -
(matrix3X3[(c + 1) % 3][(r + 2) % 3] *
matrix3X3[(c + 2) % 3][(r + 1) % 3]))
<< "\t";
}
cout << endl;
}
}
// This function will find the Inverse of the given matrix
void inverse(float matrix3X3[][3], float d) {
cout << "\n\nInverse of matrix is: \n";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
cout << ((matrix3X3[(c + 1) % 3][(r + 1) % 3] *
matrix3X3[(c + 2) % 3][(r + 2) % 3]) -
(matrix3X3[(c + 1) % 3][(r + 2) % 3] *
matrix3X3[(c + 2) % 3][(r + 1) % 3])) /
d
<< "\t";
}
cout << endl;
}
}
int main() // main function
{
float matrix3X3[3][3];
int r, c;
float determinant = 0;
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++) {
cout << "Enter the value at index [" << r << "][" << c << "] : ";
cin >> matrix3X3[r][c];
}
}
display(matrix3X3);
determinant = findDeterminant(matrix3X3);
cout << "\n\nDeteterminant of the given matrix is : " << determinant;
if (determinant != 0) {
adjoint(matrix3X3);
inverse(matrix3X3, determinant);
} else {
cout << " As determinant of the given matrix is 0, so we cannot take find "
"it's Inverse :";
}
}
Output when the input matrix is singular:
The output shows that our algorithm does not calculate the inverse of the matrix when the matrix is singular.
Output when the input matrix is non-singular:
The output shows that our algorithm calculates the inverse only if the matrix is non-singular.