core.matrix icon indicating copy to clipboard operation
core.matrix copied to clipboard

Bug in default inverse?

Open mrmcc3 opened this issue 11 years ago • 8 comments

$ lein try net.mikera/core.matrix net.mikera/vectorz-clj

user=> (require '[clojure.core.matrix :as m])
user=> (def a (m/matrix [[0 1 0] [1 1 0] [1 -1 -1]]))
user=> (m/inverse a)
#<NDArrayDouble [[0.0 0.0 0.0] [-0.0 1.0 -0.0] [-0.0 -1.0 -1.0]]>
user=> (m/mmul a (m/inverse a))
[[0.0 1.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]

user=> (m/set-current-implementation :vectorz)
user=> (def a (m/matrix [[0 1 0] [1 1 0] [1 -1 -1]]))
user=> (m/inverse a)
#<Matrix33 [[-1.0,1.0,0.0],[1.0,-0.0,0.0],[-2.0,1.0,-1.0]]>
user=> (m/mmul a (m/inverse a))
#<Matrix33 [[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]]>

mrmcc3 avatar Apr 14 '14 03:04 mrmcc3

Yeah that looks like an NDArray bug. Thanks for the report!

mikera avatar Apr 14 '14 03:04 mikera

Any progress on this? I also have a case where the inverse matrix becomes singular:

(def a m/matrix [[1 0 0 0.0 0 0.0]
                 [1 0 1 0.0 0 1.0]
                 [1 1 0 1.0 0 0.0]
                 [1 0 1/2 0.0 0N 0.25]
                 [1 1/2 1/2 0.25 1/4 0.25]
                 [1 1/2 0 0.25 0N 0.0]])

(det a) ;  => 0.015625
(det (inverse a)) ; => IllegalArgumentException lu-decompose can't decompose singular matrix  ...

I tested it on both 0.34 and 0.35. Vectorz works fine, as above.

rbuchmann avatar Jun 04 '15 12:06 rbuchmann

Don't have time to look at this myself right now. I use Vectorz basically all the time.... might just disable this functionality in case it hurts someone.

mikera avatar Jun 04 '15 15:06 mikera

I'd vote for that option then, I was pretty confused and lost quite a bit of time figuring out what the problem was. It is the default implementation after all.

rbuchmann avatar Jun 04 '15 15:06 rbuchmann

Hmmm I found a better option I think. Try to use Vectorz if available, throw an error otherwise. Sound sensible?

mikera avatar Jun 04 '15 16:06 mikera

Yes, even better, unless the transparent conversion introduces a problem elsewhere. The documentation for inverse should mention it too.

rbuchmann avatar Jun 05 '15 08:06 rbuchmann

OK, pushed some changes. Should all be available in next release.

mikera avatar Jun 05 '15 11:06 mikera

Leaving open in case anyone wants to take on fixing / rewriting the default implementation.

mikera avatar Jun 05 '15 11:06 mikera