# Determinant of a 4x4 matrix help needed

This topic is 2680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there,

I'm working on a design document of a maths library for my future projects as part of hopefully a whole game engine. I'm having problems finding a decent documentation of working out the determinant of a 4x4 matrix before calculating the inverse of the matrix, I've found a few videos on YouTube that in some way helps but not from the perspective of calculating in code but rather on paper (I know that if we can calculate it on paper the coding is made a lot easier from our understanding of the calculation) but from one video that I found 'finding the determinant of a 4x4 matrix' I'm slightly confused by if this is the best method in perspective of coding (have it function search the column and row for any which have a larger zero count of elements before calculation the rest) as the video shows or if there is a better and simpler way to do this?

Dave

##### Share on other sites
If you have generic 4x4 matrix (not 3d transformations only, for example), then don't worry about zeroes when implementing determinant calculation on computer. Just take first row and use that to to construct four 3x3 submatrices. Searching for most zeroes in column or row is when you want calculate determinant on paper - it is easier because then you need to do less calculations.

Dave

##### Share on other sites
Computing an NxN determinant by expanding a row or column and computing N determinants of size (N-1)x(N-1) will require O(N!) operations. You can obtain the determinant as a side-product of Gaussian elimination, which runs asymptotically much faster. However, N=4 might be small enough that the O(N!) algorithm is still faster.

##### Share on other sites
You almost certainly do not need a generic matrix inversion routine for game applications.

If you're inverting a well behaved 4x4 transformation matrix, decompose it into two matricies -- one containing the 3x3 rotation part padded out to 4x4. This is invertible by transposing it. The other matrix contains the translation component. This is also then easily inverted by transposing it.

Then multiply them back together in the reverse order.

This is much faster than the application of generic inversion.

##### Share on other sites

You almost certainly do not need a generic matrix inversion routine for game applications.

If you're inverting a well behaved 4x4 transformation matrix, decompose it into two matricies -- one containing the 3x3 rotation part padded out to 4x4. This is invertible by transposing it. The other matrix contains the translation component. This is also then easily inverted by transposing it.

Then multiply them back together in the reverse order.

This is much faster than the application of generic inversion.

This is what I am doing at now - but a word of warning:

Better enforce the invariant, that 3x3 rotation part of your matrix is orthonormal every time you store matrix for the usage in later frames/timesteps. You will be surprised, how fast calculating errors can accumulate and completely mess up your matrix if you are using multiplication + inversion by transposition.

##### Share on other sites

[...] but a word of warning:

Better enforce the invariant, that 3x3 rotation part of your matrix is orthonormal every time you store matrix for the usage in later frames/timesteps. You will be surprised, how fast calculating errors can accumulate and completely mess up your matrix if you are using multiplication + inversion by transposition.

I just wanted to mention that one of the advantages of using quaternions is that they are much simpler to normalize. Instead of transposition, you would use conjugation.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015564
×