nalgebra
nalgebra copied to clipboard
Added is_finite check for 4x4 matrix inversion
Fixes #1432
The issue is related to multiplications where the result exceeds the maximum value for the underlying numeric representation. See the added tests for example matrices that pass the determinant check but yield infinite values in the inversion code.
Changes:
- Added a benchmark to compare LU decomposition performance against optimized 4x4 inversion code.
- Added tests to ensure
try_inversecorrectly returnsNonefor singular or near-singular matrices. - Added a convenience function for statically sized matrices in benchmarks (
reproducible_matrix).
Notes:
- The specialized 4x4 inversion routine was approximately 50 times faster than using LU decomposition.
- Adding the
is_finitecheck resulted in a 30% performance penalty (3ns instead of 2ns on my machine). - The check only avoids
InforNaNvalues. - [x] The issue from #1432 ~~probably~~ also affects 2x2 and 3x3 matrices.
- [x] Consider adding the desired behavior for
try_inverseto the method's documentation:- My proposal is to guarantee that the result only contains finite values if
Someis returned. Other definitions would require more involved threshold calculations to define a bound on result precision.
- My proposal is to guarantee that the result only contains finite values if
- I decided to leave the in-place inversion untouched and added context in the documentation.