nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Added is_finite check for 4x4 matrix inversion

Open mat-kie opened this issue 1 year ago • 0 comments

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_inverse correctly returns None for 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_finite check resulted in a 30% performance penalty (3ns instead of 2ns on my machine).
  • The check only avoids Inf or NaN values.
  • [x] The issue from #1432 ~~probably~~ also affects 2x2 and 3x3 matrices.
  • [x] Consider adding the desired behavior for try_inverse to the method's documentation:
    • My proposal is to guarantee that the result only contains finite values if Some is returned. Other definitions would require more involved threshold calculations to define a bound on result precision.
  • I decided to leave the in-place inversion untouched and added context in the documentation.

mat-kie avatar Jan 03 '25 22:01 mat-kie