Matrix Determinant by Lower Triangular Matrix
Determinant, a properties of matrix determines the matrix is singular or not. Lower Triangular matrix 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 Lower triangular matrix, L by elementary row operation or reduction and then product of main diagonal elements is called determinant of the matrix A.
- Read matrix A
- Convert matrix A into L by applying Row operation or reduction technique
- Read Main Diagonal elements from L
- Determinant = product of Main Diagonal elements
Algorithm steps for Determinant by Lower Triangular Matrix
Read matrix A a - elements of matrix A i,j - position of a element in the matrix for i=N-1 To 1 decrement by -1 for j=i-1 To 1 decrement by -1 RowOperation.add(j,i ,-aii/aji) end end Determinant = a11 x a22 x ... x ann
Java program for Determinant by Lower 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 Matrix LowerTriangular() {
for(int c=mat.getNrow()-1;c>0;c--) {
for(int c2=c-1;c2>=0;c2--)
{
double ratio= mat.getElement(c2, c) / mat.getElement(c, c);
RowOperation.add(mat, c2, c, -ratio);
}
}
return mat;
}
public double detbyL() {
System.out.println( " matrix M " + mat.getNrow() +" x "
+ mat.getNcol());
System.out.println( this.toString() );
this.maxdiagonal();
this.LowerTriangular();
System.out.println( "Lower 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 dl =gj.detbyL();
System.out.println( "Determinant of matrix M ="+ dl);
double M3[][]={{4, 5,-2},{7,-1,2},{3,1,4}} ;
Determinant dm = new Determinant(M3);
double dl3 =dm.detbyL();
System.out.println( "Determinant of matrix M ="+ dl3);
}
}
Java program output for Determinant by Lower Triangular Matrix
matrix M 2x2 4.0 6.0 3.0 8.0 Lower Triangular matrix 1.75 0.0 3.0 8.0 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 Lower Triangular matrix 7.0 0.0 0.0 5.5 5.5 0.0 3.0 1.0 4.0 Determinant of matrix M =154.0
Comments
Post a Comment