I am calculating the inverse of a matrix using the adjoint method. first i have to calculate the determinant of the matrix. to calculate the determinant I first create an upper triangular matrix and then multiply the diagonals to get the determinant of the matrix. the fomular for calculating the determinant is given below.
for(int cr = 1 ;cr < dd.length;cr++)
{ double factor = 0.0;
final double[] firstrow = new double[dd.length] ; /* dd is a matrix*/
for(int r = 0 ;r < dd.length; r++)
{ firstrow[r] = dd[cr - 1][r]; }
for( int rowcount = cr + 0 ; rowcount< dd.length ; rowcount++)
{
factor = dd[rowcount][cr - 1] / firstrow[cr - 1];
for( int m = cr - 1 ; m < firstrow.length ; m++)
{
dd[cr - 1][m] = firstrow[m] * factor; /* multioly row by factor */
dd[rowcount][m] = dd[rowcount][m] - dd[cr - 1][m]; /* our current row minus factored row */
dd[cr - 1][m] = firstrow[m]; /* restore the original values to row */
} }}
for(int d = 0 ; d < dd.length;d++)
{det *= dd[d][d];} /* det is the determinant */
after getting the lower triangular matrix , the product of the diagonal values becomes the determinant of the matrix. I have tried this method and it works. however when I use it to calculate the determinant of a 13 by 13 matrix with floating point values , I get a not a number value that is NaN as the determinant.
Can someone please explain to me what is happening.
I have below example values of the matrix.
65.15078176822551
731.664756199619
1.5309584518179011E9
1.7388182254012366E11
3.3604905770182707E17
1.77135880331128576E17
thank all
I suspect that the problem is something to do with your use of
factorto (I’m guessing) reduce the scale of the numbers. My bet is thatfirstrow[cr - 1]is zero for some index. The ensuing division by zero will create and INF or a NaN, and that will propagate through the rest of the calculation.Incidentally, this “formula” doesn’t look like the standard method for calculating determinants, as explained here:
http://www.math.dartmouth.edu/archive/m8s00/public_html/handouts/matrices3/node7.html
The standard version doesn’t involve any division. Are you sure that your “formula” is correct?