Skip to content
Advertisement

Java inverse matrix calculation

I’m trying to calculate the inverse matrix in Java.

I’m following the adjoint method (first calculation of the adjoint matrix, then transpose this matrix and finally, multiply it for the inverse of the value of the determinant).

It works when the matrix is not too big. I’ve checked that for matrixes up to a size of 12×12 the result is quickly provided. However, when the matrix is bigger than 12×12 the time it needs to complete the calculation increases exponentially.

The matrix I need to invert is 19×19, and it takes too much time. The method that more time consumes is the method used for the calculation of the determinant.

The code I’m using is:

JavaScript

Does anybody know how to calculate the determinant of a large matrix more efficiently? If not, does anyone knows how to calcultate the inverse of a large matrix using other algorithm?

Thanks

Advertisement

Answer

Exponentially? No, I believe matrix inversion is O(N^3).

I would recommend using LU decomposition to solve a matrix equation. You don’t have to solve for the determinant when you use it.

Better yet, look into a package to help you. JAMA comes to mind.

12×12 or 19×19 are not large matricies. It’s common to solve problems with tens or hundreds of thousands of degrees of freedom.

Here’s a working example of how to use JAMA. You have to have the JAMA JAR in your CLASSPATH when you compile and run:

JavaScript

Here’s the same problem solved using Apache Commons Math, per quant_dev’s recommendation:

JavaScript

Adapt these to your situation.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement