# Matrix Inverse : Matrix Inversion Technique

## Matrix Inverse : Matrix Inversion Technique

The inverse of a matrix

The inverse of a square n × n matrix A is another n × n matrix denoted by A-1 such that

AA-1=A-1A=I

where I is the n × n identity matrix. That is, multiplying a matrix by its inverse produces an identity matrix. Not all square matrices have an inverse matrix. If the determinant of the matrix is zero, then it will not have an inverse, and the matrix is said to be singular. Only non-singular matrices have inverses.

A formula for finding the inverse

Given any non-singular matrix A, its inverse can be found from the formula A-1 = adj A |A| where adj A is the adjoint

matrix and |A| is the determinant of A. The procedure for finding the adjoint matrix is given below.

The adjoint of a matrix A is found in stages:

Find the transpose of A, which is denoted by AT. The transpose is found by interchanging the rows and columns of A. So, for example, the first column of A is the first row of the transposed matrix; the second column of A is the second row of the transposed matrix, and so on.

The minor of any element is found by covering up the elements in its row and column and finding the determinant of the remaining matrix. By replacing each element of AT by its minor, we can write down a matrix of minors of AT.

The cofactor of any element is found by taking its minor and imposing a place sign according to the following rule

This means, for example, that to find the cofactor of an element in the first row, second column, the sign of the minor is changed. On the other hand to find the cofactor of an element in the second row, second column, the sign of the minor is unaltered. This is equivalent to multiplying the minor by ‘+1’ or ‘−1’ depending upon its position. In this way, we can form a matrix of cofactors of AT. This matrix is called the adjoint of A, denoted adj A. The matrix of cofactors of the transpose of A, is called the adjoint Matrix, adj A.

C program to find value using matrix inversion technique

```#include<stdio.h>
#include<conio.h>
#include<math.h>

int main(){
int n,i,k,j,p,q;
float pivot,term,a;
printf("Matrix Inversion");
printf("\nEnter Dimension of System of equation\n");
scanf("%d",&n);
printf("\nEnter the coefficients of the Matrix\n");
for(i=0;i<n; i++)
for(j=0;j<n;j++){
scanf("%f",&a[i][j]);
}

for(i=0; i<n; i++){
for(j=n;j<2*n;j++){
if(i==j-n)
a[i][j]=1;
else
a[i][j]=0;
}
}

for(k=0; k<n; k++){
pivot=a[k][k];
for(p=0; p<2*n;p++)
a[k][p]=a[k][p]/pivot;
for(i=0;i<n;i++){
term=a[i][k];
if(k!=i)
for(j=0;j<2*n;j++){
a[i][j]=a[i][j]-a[k][j]*term;
}
}
}

printf("\nMatrix Inverse is:\n");
for(i=0; i<n;i++){
for(j=n;j<2*n;j++)
printf("%f\t",a[i][j]);
printf("\n");
}
getch();
return 0;
}```

## Java code to find inverse of matrix

```//This is sample program to find the inverse of a matrix

import java.util.Scanner;

public class Inverse
{
public static void main(String argv[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the dimension of square matrix: ");
int n = input.nextInt();
double a[][]= new double[n][n];
System.out.println("Enter the elements of matrix: ");
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j] = input.nextDouble();

double d[][] = invert(a);

System.out.println("The inverse is: ");
for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
System.out.print(d[i][j]+" ");
}
System.out.println();
}
input.close();
}

public static double[][] invert(double a[][])
{
int n = a.length;
double x[][] = new double[n][n];
double b[][] = new double[n][n];
int index[] = new int[n];
for (int i=0; i<n; ++i)
b[i][i] = 1;

// Transform the matrix into an upper triangle
gaussian(a, index);

// Update the matrix b[i][j] with the ratios stored
for (int i=0; i<n-1; ++i)
for (int j=i+1; j<n; ++j)
for (int k=0; k<n; ++k)
b[index[j]][k]
-= a[index[j]][i]*b[index[i]][k];

// Perform backward substitutions
for (int i=0; i<n; ++i)
{
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];
for (int j=n-2; j>=0; --j)
{
x[j][i] = b[index[j]][i];
for (int k=j+1; k<n; ++k)
{
x[j][i] -= a[index[j]][k]*x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}

// Method to carry out the partial-pivoting Gaussian
// elimination. Here index[] stores pivoting order.

public static void gaussian(double a[][], int index[])
{
int n = index.length;
double c[] = new double[n];

// Initialize the index
for (int i=0; i<n; ++i)
index[i] = i;

// Find the rescaling factors, one from each row
for (int i=0; i<n; ++i)
{
double c1 = 0;
for (int j=0; j<n; ++j)
{
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}

// Search the pivoting element from each column
int k = 0;
for (int j=0; j<n-1; ++j)
{
double pi1 = 0;
for (int i=j; i<n; ++i)
{
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1)
{
pi1 = pi0;
k = i;
}
}

// Interchange rows according to the pivoting order
int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i)
{
double pj = a[index[i]][j]/a[index[j]][j];

// Record pivoting ratios below the diagonal
a[index[i]][j] = pj;

// Modify other elements accordingly
for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}
}```