fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.06k stars 164 forks source link

linalg: Matrix Inverse #828

Closed perazz closed 2 months ago

perazz commented 4 months ago

Compute the multiplicative inverse of a real or complex square matrix: $A \cdot A^{-1} = A^{-1} \cdot A = I_n$ . Based on LAPACK General factorization (*GETRF) and inversion (*GETRI).

Prior art

Proposed implementation

cc: @jalvesz @jvdp1 @loiseaujc @fortran-lang/stdlib

loiseaujc commented 4 months ago

As far as this is my first review for stdlib, it looks pretty good to me. I was jut wondering about the tests. Wouldn't including a test with a known singular matrix be useful to make sure the whole error handling is working correctly?

perazz commented 4 months ago

Great idea @loiseaujc, thanks!

e10e4c4

perazz commented 4 months ago

From Fortran Monthly call:

perazz commented 3 months ago

Thanks a lot @loiseaujc @jvdp1 @jalvesz for the reviews - I believe I've addressed all your comments (see linked commits in each comment). In d0af9be I've introduced a tiny behavior change:

On exceptions raised when the operator interface (.inv.A) is used, instead of error stopping the program, I think it is more reasonable to instead return an array of NaNs (which are initialized from ieee_value(0.0,ieee_quiet_nan). The reason I think this is better is:

So please do let me know if you also like this update.

jalvesz commented 3 months ago

Given your idea to enable the operator interface to run through the computation without stopping in case of singular matrices I just suggested a small addition to the documentation. This change seems reasonable to me as even a scalar division would not stop the program but also simply return a NaN. Otherwise LGTM!!

perazz commented 2 months ago

Ok @jvdp1 @jalvesz I've restored the xdp precision. Please let me know if you have further comments. This is probably ready to be merged sometime next week.

perazz commented 2 months ago

Thank you all, I will merge this now.