-
-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inverting a 4x4 matrix with try_inverse_mut
doesn't leave self
unchanged if the inversion fails.
#1384
Conversation
try_inverse_mut
doesn't leave self
unchanged if the inversion fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
I guess we don't have any unit tests validating this. Want to add one? |
@Ralith, I realized that I had mistakenly omitted the cofactor |
Oh, nice, looks good then. |
@Andlon oh yes, added the test now |
tests/core/matrix.rs
Outdated
#[test] | ||
fn test_inversion_failure_leaves_matrix_unchanged(m in matrix4()) { | ||
let original_matrix = m.clone(); | ||
if m.try_inverse().is_none() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this branch actually get hit? I'm not sure how often proptest generates non-invertible matrices. Would a hardcoded case work better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @Ralith, I think the likelihood of matrix4()
producing singular matrices is likely (very) small (though I haven't checked), so a test with a fixed input is better in this case.
I was actually just asking about a test to make sure this isn't causing a regression, but that was already covered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the test. We're almost there, I'm unfortunately going to have to be a little bit picky still :-) See comments!
tests/core/matrix.rs
Outdated
4.0, 8.0, 12.0, 16.0 | ||
); | ||
let expected = mat.clone(); | ||
if !mat.try_inverse_mut() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is currently very brittle. Imagine that some change in an implementation detail somewhere caused mat.determinant()
to return something not exactly 0.0
. Then mat.try_inverse_mut()
might return true
and the test would succeed, although it's no longer testing what we want to test it.
Perhaps replace the if by an assert? E.g. assert!(!mat.try_inverse_mut())
followed by the assert_eq
that you already have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Andlon fixed it
tests/core/matrix.rs
Outdated
@@ -1263,6 +1263,20 @@ fn column_iterator_double_ended_mut() { | |||
assert_eq!(col_iter_mut.next(), None); | |||
} | |||
|
|||
#[test] | |||
fn test_inversion_failure_leaves_matrix_unchanged() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to _matrix4_unchanged
to indicate that this was specifically an issue with 4x4 inversion and what we're testing here
@jnyfah: thanks for fixing the last remaining points! We're all good to go now, except CI is failing due to formatting. I think you just need to run |
Thanks for your help with this @jnyfah! The CI failed due to an unrelated issue (a brittle test). |
Fixes #1380
Modified the
do_inverse4
function to calculate the determinant of 4x4 matrix before proceeding with the computation of the adjugate matrix to ensure thatout
is not modified if the determinant is zero.