Matrix Determinant by Upper Triangular Matrix
Determinant, a properties of matrix determines the matrix is singular or not. Upper triangular 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 upper triangular matrix U by elementary row operation and then multiplication of main diagonal elements is called determinant of the matrix A.
- Read matrix A
- Convert matrix A into U by applying Row operation
- Read Main Diagonal elements from U
- Determinant = product of Main Diagonal elements
Algorithm steps for Determinant by Upper Triangular 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=i+1 To N RowOperation.add(j,i ,-aii/aji) end end Determinant = a11 x a22 x ... x ann
Java program for Determinant by Upper Triangular 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 UpperTriangular() {
for(int r=0;r<mat.getNrow();r++) {
for(int r2=r+1;r2<mat.getNrow();r2++) {
double ratio= mat.getElement(r2, r) / mat.getElement(r, r);
RowOperation.add(mat, r2, r, -ratio);
}
}
}
public double detbyU() {
System.out.println( " matrix M " );
System.out.println( this.toString() );
this.maxdiagonal();
this.UpperTriangular();
System.out.println( "Upper Trianguar matrix " );
System.out.println( this.toString() );
double D=mat.getElement(0,0);
for(int r=1;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 du =gj.detbyU();
System.out.println( "Determinant of matrix M ="+ du);
double M3[][]={{4, 5,-2},{7,-1,2},{3,1,4}} ;
Determinant dm = new Determinant(M3);
double du3 =dm.detbyU();
System.out.println( "Determinant of matrix M ="+ du3);
}
}
Java program output for matrix 2x2 and 3x3 Determinant by Upper Triangular Matrix
matrix M 2x2 4.0 6.0 3.0 8.0 Upper Triangular matrix 4.0 6.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 Upper Triangular matrix 7.0 -1.0 2.0 0.0 5.5714 -3.1428 0.0 0.0 3.94871 Determinant of matrix M =154.0
Comments
Post a Comment