lessthanoptimal / ejml

A fast and easy to use linear algebra library written in Java for dense, sparse, real, and complex matrices.
https://ejml.org
565 stars 117 forks source link

Different result for pseudo-inverse matrix compared to Numpy #198

Open elect86 opened 5 months ago

elect86 commented 5 months ago

I'm porting some code from Python to Kotlin and I'm using Ejml, specifically this

np.linalg.pinv(cell)

which "Compute the (Moore-Penrose) pseudo-inverse of a matrix." and where cell is a 3x3 float64 matrix

[[2.259825867767797, 0.0, 0.0], [-1.1299129338838985, 1.9570656971336065, 0.0], [0.0, 0.0, 3.5698785608749377]]

my float counterpart is

[0.44251198, -8.326673E-17, 0.0] [0.25548452, 0.51096904, 0.0] [0.0, 0.0, 0.28012156]

on Python I do get

[[4.42511972e-01 2.83417670e-17 0.00000000e+00], [2.55484525e-01 5.10969050e-01 0.00000000e+00], [0.00000000e+00 0.00000000e+00 2.80121574e-01]]

on Ejml, with SimpleMatrix::pseudoInverse

[0.44251198, -2.9802322E-8, 0.0] [0.25548458, 0.5109691, 0.0] [0.0, 0.0, 0.28012162]

You can see that there is just one problematic value, at [0, 1]

if I hardcode the float64 values of Python into Kotlin, I do get a closer value but still not quite the same

[0.44251198, -8.326673E-17, 0.0] [0.25548452, 0.51096904, 0.0] [0.0, 0.0, 0.28012156]

Any idea how I could improve this?

Python code is here