jingyuzhu / efficient-java-matrix-library

Automatically exported from code.google.com/p/efficient-java-matrix-library
0 stars 0 forks source link

SimpleMatrix invert: no exception for singular matrix #45

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
When trying to invert a Singular Matrix using SimpleMatrix interface, an 
IlegalArgumentException is expected, but not thrown.

Code snippet:
SimpleMatrix m = new SimpleMatrix(new double[][]{{1,2,3},{4,5,6},{7,8,9}});
System.out.println("det(m) = "+m.determinant());
m.invert();
System.out.println("An exception should have been thrown !");

Produces:
det(m) = 0.0
An exception should have been thrown !

Which is weird because SimpleMatrix#invert() seems to call 
CommonOps#invert(m,m_inv) which produces the expected behaviour when called 
directly...

I am using ejml v0.24

Original issue reported on code.google.com by sylvain....@gmail.com on 9 Jun 2014 at 7:42

GoogleCodeExporter commented 8 years ago
Erratum: CommonOps#invert(m,m_inv) does not produce the expected behaviour 
either.
The only method that produces a warning is using a LinearSolver and checking 
manually for solver quality().
Why not adding an automatic check on determinant in SimpleMatrix#invert() ?

Original comment by sylvain....@gmail.com on 9 Jun 2014 at 7:59

GoogleCodeExporter commented 8 years ago
It appears to be doing exactly what it says it does.  The JavaDoc includes a 
warning that an exception might not be thrown if the system is singular/nearly 
singular.  Here's what octave produces:

octave:1> A=[1,2,3;4,5,6;7,8,9];
octave:2> det(A)
ans = -1.3326e-15

The other question is that should it throw an exception?  The problem with 
checking a numerical value of the quality (for which there is no clear "best" 
metric) is that a threshold must be selected.  In your application you might 
want an exception to be thrown, in others people might want any sort of 
solution they can get.  See the problem?

Original comment by peter.ab...@gmail.com on 9 Jun 2014 at 12:29

GoogleCodeExporter commented 8 years ago
Many thanks for your quick answer.
I fully understand that two different users will have opposite expectations for 
the same library.
I am just surprised that returning a matrix filled with NaN and +/-Infinity is 
allowed by the library. I agree that it could be checked at application level 
but as you had the excellent idea to offer 3 levels of complexity to access 
your library, I would have expected that the SimpleMatrix level designed for 
non-specialists would naturally intercept these kind of situations and alert 
the user.
About the threshold issue, you could have one invert(double 
singularityThreshold) method and one invert() method with a default threshold. 
This is the approach taken by CommonsMath library.
Users that do not care about having NaN in their resulting matrix could still 
use 0.0 as a threshold. And if you know by experience that this is the 
behaviour expected by most users, you could also use 0.0 as default threshold...
Well, this is only my point of view. Do not hesitate to close the topic if you 
think it is not relevant.

Original comment by sylvain....@gmail.com on 9 Jun 2014 at 2:04

GoogleCodeExporter commented 8 years ago
I forgot to include the test matrix that produces NaN and +/-Infinity when 
trying to invert:
m=
[1,2,3,4,5,6,7
;2,3,4,5,6,7,8
;3,4,5,6,7,8,9
;4,5,6,7,8,9,10
;5,6,7,8,9,10,11
;6,7,8,9,10,11,12
;7,8,9,10,11,12,13];

Original comment by sylvain....@gmail.com on 9 Jun 2014 at 2:15

GoogleCodeExporter commented 8 years ago
I modified SimpleMatrix.solve and invert so that it checks for NaN and 
infinity.  Trying to decide if I should do the same for determinant, probably 
yes.

For the optional tolerance parameter, I'm still thinking about it.  I want to 
keep that interface as simple as possible, but knowing when an operation failed 
seems like a good idea.

Original comment by peter.ab...@gmail.com on 9 Jun 2014 at 3:45

GoogleCodeExporter commented 8 years ago
Just going to close this.  Current behavior is explained in javadoc and 
reasonable.

Original comment by peter.ab...@gmail.com on 17 Nov 2014 at 8:59