Skip to content
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

[Question] Matrix4f.determinate() looks wrong? #312

Closed
TheIceCreamBear opened this issue Apr 20, 2022 · 2 comments
Closed

[Question] Matrix4f.determinate() looks wrong? #312

TheIceCreamBear opened this issue Apr 20, 2022 · 2 comments
Labels

Comments

@TheIceCreamBear
Copy link

I was looking around the Matrix4f code and found that the determinate function has a different calculation from what is used in the inverse function.

Specifically, the Matrix4f.determinate() uses plus after each of the calculations but Matrix4f.invertGeneric() does the normal + - + - pattern of determinates.

I'm curious which is correct, or are they both correct for their specific circumstance?

Also I'd like to note that Matrix4f.determinate3x3() and Matrix3.determinate() also keep the sign constant.

My understanding of 3x3 and up matrix determinates is that alternate the sign of each sub determinate based on which row you choose (in row 1 and 3 it would be + - + and row 2 it would be - + -).

Mostly this issue is a question trying to understand the code and the math behind it, and also why the calculation in the determinate method is different from what is in the inverse method.

@httpdigest
Copy link
Member

You are right in that both Matrix4f.determinant() and Matrix4f.invert() would need to effectively do the same computations. And they indeed do. It's just that the calculations done in Matrix4f.invert() have been massively reshuffled and reorganized to make full use of variables to hold common sub-expressions that are computed at least twice. This gave a huge performance boost compared to a naive implementation of Matrix4f.invert() where you'd simply once call determinant() and then do all computations for the individual matrix elements.
However, both the determinant and the inverse matrix elements share many common sub-expressions that need not be computed multiple times, so I spend literally hours refactoring it to have as few arithmetic operations and as many reuses of variables for common sub-expressions as possible.

The fact that you do not see the common alternating + - patterns in the determinant is that this has also been restructured/reshuffled to "look nicer". Note that + (a * b - c * d) is equal to - (c * d - a * b). This is all there is to it, really.

And both methods should do what they are supposed to do.

@TheIceCreamBear
Copy link
Author

Note that + (a * b - c * d) is equal to - (c * d - a * b). This is all there is to it, really.

This makes so much more sense now and I didn't even think about that. Nor did I think about the performance implications. Thank you for clarifying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants