Exception in thread “main” java.lang.RuntimeExcept

2020-02-14 19:58发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.



标签: java matrix jama