Search code examples
javalinear-algebrajamamatrix-inverse

Test for invertability using Jama.Matrix


I have a program that uses JAMA and need to test is a matrix can be inverted. I know that I can just try it and catch an exception but that seems like a bad idea (having a catch block as part of the "normal" code path seems to be bad form).

A test that also returns the inverse (or run in a better O() than the inverse operation) would be preferred.


Solution

  • In general, if you can't solve the matrix, it's singluar (non-invertable). I believe the way that JAMA does this is to try to solve the matrix using LU factorization, and if it fails, it returns "true" for isSingular().

    There isn't really a general way to just look at the elements of a matrix and determine if it is singular - you need to check each column to see if it is orthogonal with the others (i.e. the nullspace for the matrix is 0). LU factorization is quite fast, usually... there are times where it takes the bulk of an operation, however.

    Do you have an actual speed problem you're trying to overcome?