deteminant

deteminant

View Answers

November 7, 2009 at 11:24 AM

Hi Friend,

We are providing you a code that will find the determinant of 2x2 matrix.

public class Detrminant{
public double determinant(double[][] mat) {

double result = 0;

if(mat.length == 1) {
result = mat[0][0];
return result;
}

if(mat.length == 2) {
result = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
return result;
}

for(int i = 0; i < mat[0].length; i++) {
double temp[][] = new double[mat.length - 1][mat[0].length - 1];

for(int j = 1; j < mat.length; j++) {
for(int k = 0; k < mat[0].length; k++) {

if(k < i) {
temp[j - 1][k] = mat[j][k];
} else if(k > i) {
temp[j - 1][k - 1] = mat[j][k];
}

}
}

result += mat[0][i] * Math.pow(-1, (double)i) * determinant(temp);
}

return result;

}

public static void main(String []args){
double array[][]= {{5,6},{8,9}};
Detrminant d=new Detrminant();
double result= d.determinant(array);
System.out.println(result);
}
}

Thanks









Related Tutorials/Questions & Answers:
deteminant - Java Beginners
deteminant - Java Beginners
Advertisements

Ads