mathajax

Matrix Determinant, Matrix Adjoint and Matrix Inverse

The Java program implements following three important matrix operation in this code. and these operation is applied on a square matrix size of 3x3. They are,

  • Matrix Determinant
  • Matrix singular or not
  • Adjoint matrix
  • Matrix Inversion
  • AA-1 = I
  • (A-1)-1 = A

Matrix Determinant - algorithm

matrix A - a matrix size of 3x3
A-cofactor(0,c) - read cofactor matrix of 0th row and cth column of the matrix A


      set M[] =0
      set D=0
      Read matrix A
        
        For c=0 : A-column
            matrix cf = A-cofactor(0,c)     
            M[c]=cf[[0][0]*cf[[1][1]  -   cf[[0][1]*cf[[1][0]
        End

         
        For c=1 : A-column
           D = D + pow(-1,c+0) *M[c]
        End  

         print "determinant" D 
         
 

Matrix Inverse - algorithm

matrix A - a matrix size of 3x3.
determinant(matrix A) - function finds and returns determinant value of the matrix A.
adjoint-matrix(matrix A) - function finds and return adjoint matrix of the matrix A.

      Read matrix A

        D = determinant(matrix A)  
        IF D== 0 
             print "matrix A is singular matrix" 
             print "and so matrix A is not invertable"
             return

        ELSE                   
          matrix adjA = adjoint-matrix(matrix A)
          matrix A-1  = 1 /D * ( adjA )    

        END 
        print A-1 
   

Matrix Determinant Adjoint Inverse - Java program

The Java program class has the following 3 static membership function to finds determinant value of a matrix 3x3 and adjoint of a matrix 3x3 and inverse of a matrix 3x3.

The three static membership functions are

  1. determinant - The function/method which takes a matrix object as an argument, finds determinant of the matrix and returns determinant value as the execution result.

  2. adjmatrix - The function/method which takes a matrix object as an argument, finds adjoint of the matrix and returns an adjoint matrix object as the execution result.

  3. inverse - The function/method which takes a matrix object as an argument, finds inverse matrix of the matrix and returns an inverse matrix object as the execution result.

 
public class MatrixOpr2 {

   
  public static double determinant(Matrix mat) 
   { 
      double det=0;
      double M[] = new   double[mat.getNcol()];
   
     for(int c=0;c<mat.getNcol();c++)
      {
          Matrix cmat=mat.cofactor(0, c);
          M[c] =(cmat.getElement(0,0)* cmat.getElement(1,1) - 
          cmat.getElement(0,1)* cmat.getElement(1,0)) ;       
      }
   
      int r=0; 
      for(int c=0;c<mat.getNcol();c++)
        det = det + Math.pow(-1, c+r)*mat.getElement(r, c) * M[c]; 
   
    return det;
  }
   
  public static Matrix adjmatrix(Matrix mat) 
   {  
     Matrix adjmat =new Matrix(mat.getNrow(),mat.getNcol());  
     for(int r=0;r<mat.getNrow();r++)
     {     
      for(int c=0;c<mat.getNcol();c++) 
       {
          Matrix cmat=mat.cofactor(r, c);
         double val= Math.pow(-1, c+r)   
                           * ( cmat.getElement(0,0)* cmat.getElement(1,1)
                          -    cmat.getElement(0,1)* cmat.getElement(1,0)) );
         adjmat.setElement(r, c, val);   
       }
    }  
      return adjmat;
  }
 
 
   public static Matrix inverse(Matrix mat) 
    {             
       double det=MatrixOpr2.determinant(mat);
        Matrix adj=MatrixOpr2.adjmatrix(mat);
        Matrix tadj=adj.transpose();              
        Matrix imat= Scalar.divide(tadj, det);       
       return imat;  
   }
 
   public static void main(String[] args) 
  {

      double val[][]={{3,1,2},{2,-1,1},{1,3,-1}};
      Matrix A=new Matrix(val);  
      System.out.println("Input Matrx :");
      System.out.println(A.toString());
  
     double det=MatrixOpr2.determinant(A);
     System.out.println("Determinant of Matrix  A");  
     System.out.println(det);

     if (det==0) {
        System.out.println(" Matrix A is singular ");    
        return;
     }
  
     Matrix adjmat=MatrixOpr2.adjmatrix(A);
     System.out.println("nAdjoint Matrix of A :");
     System.out.println(adjmat.toString());
    
     Matrix Ai=MatrixOpr2.inverse(A);
     System.out.println("nInverse Matrix of A:");
     System.out.println(Ai.toString());
    
     Matrix A2=MatrixOpr2.inverse(Ai);
     System.out.println("nInverse Matrix of A1 =A :");
     System.out.println(A2.toString());
  
     Matrix I=MatrixOpr.multiply(A, Ai);
     System.out.println("nIdentity Matrix : AA1=I");
     System.out.println(I.toString());
  
 }

}

Matrix Determinant Adjoint Inverse - Java program Output


Input Matrix A
3.0  1.0  2.0  
2.0  -1.0  1.0  
1.0  3.0  -1.0  

Determinant of Matrix  A
11.0

Adjoint of Matrix A 
-2.0  3.0  7.0  
7.0  -5.0  -8.0  
3.0  1.0  -5.0  

Inverse Matrix of A
-0.18181818181818182  0.6363636363636364  0.2727272727272727  
0.2727272727272727  -0.45454545454545453  0.09090909090909091  
0.6363636363636364  -0.7272727272727273  -0.45454545454545453  



Prove that (A-1)-1= A
Inverse Matrix of A-1  
2.9999999999999996  1.0  2.0  
2.0  -0.9999999999999997  0.9999999999999999  
1.0  2.9999999999999996  -0.9999999999999997  


Prove that AA-1=I
Identity Matrix I 
1.0  0.0  0.0  
0.0  1.0  -5.551115123125783E-17  
-1.1102230246251565E-16  1.1102230246251565E-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

Binary 1's and 2's Complement