mathajax

Inverse Matrix by Gauss Jordan Elimination

It finds N x N inverse matrix for a matrix which has NxN elements by Gauss-Jordan elimination method.

guass-jordan inverse A|I  to I|A-1

Gauss-Jordan elimination method

  • A, matrix has N x N elements
  • I, Identity matrix has N x N elements
  • A|I is a augmented matrix
  • Elementary row operation is applied to augmented matrix A|I and it transforms A|I into I|A-1.


Algorithm Steps for Inverse matrix by Gauss-Jordan Elimination

     
  Read Matrix A 
  form Augmented matrix,  A|I
  
  For i=1  To  N 
  
    For j=1 To  N

   IF  i not-equal j
           apply RowOperation.add(j,i ,-aii/aji) on augmented matrix A|I      
   End   
    End 
 
 End
 
  divide  each row of A|I  by main diagonal elements, aii     

  

Gauss Jordan Inverse - Java programming code

 
public class Gaussjordan {

 Matrix mat;
 
 public Gaussjordan(double A[][]) {
  
    int row=A.length;
    int col=A[0].length;  
    mat = new Matrix(row,col+col);

   for(int r=0;r<row;r++) {
      for(int c=0;c<col;c++) 
          mat.setElement(r, c, A[r][c]);    
     }
  
    for(int r=0;r<row;r++)    
      mat.setElement(r, col+r, 1);
          
 }
 
 public void maxdiagonal() {
    
  for(int r=0;r<mat.getNrow();r++)  
  {
       double mx=0; int mr=r;   
       for (int c=r;c<mat.getNrow();c++){ 
       if (  Math.abs(mat.getElement(c, r)) > mx ) {
           mx = Math.abs(mat.getElement(c, r));
           mr = c;
         }      
      }       
     if ( mr!=r )
       RowOperation.swap(mat, r, mr);           
  }  
 }
 
 public void divbydiag() {
  
   for(int r=0;r<mat.getNrow();r++)  
    {
      double idia = mat.getElement(r, r);    
      for (int c=mat.getNrow();c<mat.getNcol();c++){
        mat.setElement(r, c, mat.getElement(r, c) / idia); 
       }    
       mat.setElement(r, r, mat.getElement(r, r)/idia); 
    }
 }
 
 
 public void diagonalize() {      

     for(int r=0;r<mat.getNrow();r++) {             
        for(int r2=0;r2<mat.getNrow();r2++) {
            if ( r!=r2 ) { 
              double ratio= mat.getElement(r2, r) / mat.getElement(r, r);
              RowOperation.add(mat, r2, r, -ratio);          
             }         
         }      
      }     
  }
  
 public Matrix inverse() {
        
  this.maxdiagonal();  
  this.diagonalize();  
  this.divbydiag();  
  
  Matrix imat = new Matrix(mat.getNrow(),mat.getNrow());  
  for(int r=0;r<mat.getNrow();r++) {
    for(int c=0;c<mat.getNrow();c++) 
         imat.setElement(r, c, mat.getElement(r,mat.getNrow()+c));   
    }
   return imat;
    
 }
 
 public String toString() {
   return mat.toString();
 }
 
 public static void main(String[] args) {
    
  double A[][]={{3,1,2},{2,-1,1},{1,3,-1}};
  
  Gaussjordan gj = new Gaussjordan(A);  
  Matrix imat =gj.inverse();
  System.out.println( "Inverse matrix A" );
  System.out.println( imat.toString() );
  
  
  Matrix mat = new Matrix(A);
  
  Matrix mat2=MatrixOpr.multiply(imat, mat);
  System.out.println( "Identity matrix" );
  System.out.println( mat2.toString() );

 }
}
 

Gauss Jordan Inverse - Java programming output

 matrix A 
3.0  1.0  2.0  
2.0  -1.0  1.0 
1.0  3.0  -1.0 

Augmented matrix A|I 3.0 1.0 2.0 1.0 0.0 0.0 2.0 -1.0 1.0 0.0 1.0 0.0 1.0 3.0 -1.0 0.0 0.0 1.0
Inverse matrix A -0.1818181 0.636363 0.27272 0.27272 -0.45454 0.09090 0.63636 -0.727272 -0.45454
Prove I= A*A-1 Identity matrix I 1.0 0.0 0.0 0.0 1.0 -5.551E-17 0.0 1.110E-16 1.0

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Distance Metric - Euclidean Distance