mathajax

Matrix Determinant by Diagonal Matrix

A matrix diagonal transformation method is preferred over minor or cofactor of matrix method while finding determinant of the matrix's size over 3x3.

The matrix A is converted into Diagonal matrix D by elementary row operation or reduction and then product of main diagonal elements is called determinant of the matrix A.

matrix determinant by diagonal matrix
  • Read matrix A
  • Convert matrix A into diagonal matrix D by applying Row operation or reduction technique
  • Read Main Diagonal elements from D
  • Determinant = product of Main Diagonal elements

Algorithm steps for Determinant by Diagonal matrix

  Read matrix A
  a - element of the matrix A
  i,j - position of a element in the matrix  
  
  for i=1  To  N 
  
    for j=1 To  N
 if i not-equal j
           RowOperation.add(j,i ,-aii/aji)       
  end   
    end 

 end
 
  Determinant = a11  x a22 x ... x ann

 

Java program for a matrix Determinant by Diagonal matrix

 
public class Determinant {

 Matrix mat;
 public Determinant(double M[][]) {
      
  int row=M.length;
  int col=M[0].length;
  
  mat = new Matrix(row,col);
  for(int r=0;r<row;r++) {
   for(int c=0;c<col;c++) 
        mat.setElement(r, c, M[r][c]);    
  }
 }
 
 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 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 double detbyD() {
  
  System.out.println( " matrix A " );
  System.out.println( this.toString() );
  
  this.maxdiagonal();
  
  this.diagonalize();
  System.out.println( "Diagonal matrix " );
  System.out.println( this.toString() );
      
  double D=1.0;    
  for(int r=0;r<mat.getNrow();r++) {   
        D= D * mat.getElement(r,r);   
  }
    return D;
    
 }
    
   public String toString() {
   return mat.toString();
 }
 
 public static void main(String[] args) {
  
  
  double M2[][]={{4, 6},{3,8}} ;  
  Determinant gj = new Determinant(M2);  
  
  double dd =gj.detbyD();
  System.out.println( "Determinant of  matrix M ="+ dd);
  
  
  double M3[][]={{4, 5,-2},{7,-1,2},{3,1,4}} ;  
  Determinant dm = new Determinant(M3);
  
  double dd3 =dm.detbyD();
  System.out.println( "Determinant of  matrix M ="+ dd3);
              
 }
}

Java program output for matrix 2x2 and 3x3 Determinant by Diagonal matrix


matrix M 2x2 
4.0  6.0  
3.0  8.0  

Diagonal matrix 
4.0  0.0  
0.0  3.5  

Determinant of  matrix M =14.0

matrix M 3x3 
4.0  5.0  -2.0  
7.0  -1.0  2.0  
3.0  1.0  4.0  

Diagonal matrix 
7.0  0.0  0.0  
0.0  5.57142  0.0  
0.0  0.0  3.94871  

Determinant of  matrix M =154.0

Comments

Post a Comment

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