3

I'm just trying to create an inverse matrix of a 3x3 matrix following JAMA documentation. But every time it's giving me the following error -

Exception in thread "main" java.lang.RuntimeException: Matrix is singular

Can anyone help me in this regard?

Pow
  • 1,377
  • 10
  • 32
  • 56
  • 2
    What is your 3x3 matrix? My guess is it's not invertible - see http://en.wikipedia.org/wiki/Invertible_matrix – Jeff Foster Jun 09 '11 at 09:09
  • This is vague: your question doesn't reference to any source code and it has no stacktrace. Also, what JAMA documentation are you referring to? – Buhake Sindi Jun 09 '11 at 09:13

3 Answers3

3

The documentation for Jama is not very good.

In fact, if you look through the sourcecode, you will find that Matrix.inverse() ultimately calls LUDecomposition.solve(...) and the code says:

  270      /** Solve A*X = B
  271      @param  B   A Matrix with as many rows as A and any number of columns.
  272      @return     X so that L*U*X = B(piv,:)
  273      @exception  IllegalArgumentException Matrix row dimensions must agree.
  274      @exception  RuntimeException  Matrix is singular.
  275      */
  277      public Matrix solve (Matrix B) {
  278         if (B.getRowDimension() != m) {
  279            throw new IllegalArgumentException("Matrix row dimensions must agree.");
  280         }
  281         if (!this.isNonsingular()) {
  282            throw new RuntimeException("Matrix is singular.");
  283         }

As Wikipedia says:

"In linear algebra an n-by-n (square) matrix A is called invertible or nonsingular or nondegenerate, if there exists an n-by-n matrix B such that AB = BA = In where In denotes the n-by-n identity matrix and the multiplication used is ordinary matrix multiplication."

In short, singular means not invertible.


If you are unhappy with JAMA, take a look at the Apache Commons Maths libraries, in particular the Linear Algebra module.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

If you can calculate the determinant of your matrix, you'll find that it's zero (or close to it).

You might be able to tell by inspection. If one row is proportional to another, your matrix is not invertible.

3x3 is easy enough to invert by hand. Try it and see where it goes wrong.

Try a SVD solution. It'll tell you what the null space for your matrix is.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Thanks ... That helped.. Actually yes, I could not realize that my generated 3x3 Matrix got such a value where one row is proportional to another.. Apparently I fixed that out. :) – Pow Jun 09 '11 at 17:13
  • 1
    Right, one row proportional to another says that you can divide through by the proportionality value and end up with two expressions with the same left hand side. IF they have the same right hand sides, you really have just two equations. If they have different right hand sides, this is inconsistent. You see the problem in non-mathematical terms: x+y+z = 4 and x+y+z = 6 can't both be true. – duffymo Jun 09 '11 at 17:21
2

Well, it's telling you everything you need to know: the matrix you are trying to invert is singular.

Singular matrices are non-invertible.

If you think your matrix isn't singular, please post it and we'll take a look.

NPE
  • 486,780
  • 108
  • 951
  • 1,012