Inverse of 4x4 matrix

Started by
13 comments, last by Steve132 13 years, 4 months ago
Can someone show me how to do this? I tried to google it but I could not understand anything I read...
Advertisement
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
SUB PhysicsInvertMatrix (Dest AS SINGLE PTR, Source AS SINGLE PTR)	DIM X AS INTEGER	DIM Y AS INTEGER	DIM Index AS INTEGER	DIM Minor(11) AS SINGLE	DIM Adjoint(11) AS SINGLE	DIM AS SINGLE Determinant = Source[0] * (Source[5] * Source[10] - Source[9] * Source[6]) - _					Source[4] * (Source[1] * Source[10] - Source[9] * Source[2]) + _					Source[8] * (Source[1] * Source[6] - Source[5] * Source[2])	DIM AS SINGLE DetRec = 1.0 / Determinant	Minor(0) = Source[5] * Source[10] - Source[9] * Source[6]	Minor(1) = Source[4] * Source[10] - Source[8] * Source[6]	Minor(2) = Source[4] * Source[9] - Source[8] * Source[5]	Minor(4) = Source[1] * Source[10] - Source[9] * Source[2]	Minor(5) = Source[0] * Source[10] - Source[8] * Source[2]	Minor(6) = Source[0] * Source[9] - Source[8] * Source[1]	Minor(8) = Source[1] * Source[6] - Source[5] * Source[2]	Minor(9) = Source[0] * Source[6] - Source[4] * Source[2]	Minor(10) = Source[0] * Source[5] - Source[4] * Source[1]	'Shouldn't I be multiplying each of these by DetRec and...	Adjoint(0) = Minor(0)	Adjoint(1) = -Minor(4)	Adjoint(2) = Minor(8)	Adjoint(4) = -Minor(1)	Adjoint(5) = Minor(5)	Adjoint(6) = -Minor(9)	Adjoint(8) = Minor(2)	Adjoint(9) = -Minor(6)	Adjoint(10) = Minor(10)	'...getting rid of these loops?	FOR Y = 0 TO 2		FOR X = 0 TO 2			Index = Y * 4 + X			Dest[Index] = DetRec * Adjoint(Index)		NEXT X	NEXT Y	Dest[3] = Source[3]	Dest[7] = Source[7]	Dest[11] = Source[11]END SUB
Quit screwin' around! - Brock Samson
Quote:Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


a little confused what is going on in that function...
Try The Laplace Expansion Theorem: Computing the Determinants and Inverses of Matrices by David Eberly. It's a really nice description.
Quote:Original post by SteveDeFacto
Quote:Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


a little confused what is going on in that function...

Yeah, me too. It works though. It takes two pointers to a source and a destination matrix. Here's an example of it's use:

DIM AS SINGLE PTR rotation = dBodyGetRotation (BodyID)
DIM AS SINGLE inverse(12)

PhysicsInvertMatrix (@inverse(0), rotation)

*I'm using SINGLE (float in C++) to match the parameters of the function though this wouldn't be recommended when using ODE functions (use dReal instead) but that's a different topic.

I painstakingly pieced this function together from what information I could glean from information online about inverse matrices. I've read the proof but this was two years ago and I have absolutely no idea how this works anymore. There's plenty of info out there if you really want to get into the specifics. But, for all intents and purposes, this does the job.
Quit screwin' around! - Brock Samson
For such problems there is a very nice book called Numerical Recipes. The older version is available online.

Quote:Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


The indexes go from 0 to 11 in that algorithm, so it can't compute the inverse of a general 4x4 matrix. It probably assumes that the lat row has a know from, maybe 0,0,0,1 .
Quote:Original post by Kambiz
For such problems there is a very nice book called Numerical Recipes. The older version is available online.

Quote:Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


The indexes go from 0 to 11 in that algorithm, so it can't compute the inverse of a general 4x4 matrix. It probably assumes that the lat row has a know from, maybe 0,0,0,1 .

Yes, this is for use with ODE matrices which are 4x3. For most purposes, a set of 0, 0, 0, 1 for the missing row works.

@kloffy: Excellent paper. I hadn't come across that one before but it explains it better than anything I had read.
Quit screwin' around! - Brock Samson
I'm still confused. Is there a simple way to think about it?
mathworld has a really good article on this http://mathworld.wolfram.com/MatrixInverse.html

This can be easily solved using Cramer's rule. Find the determinant of the matrix and invert the result then multiply that by the transposed matrix of cofactors.

Another relevant article: http://en.wikipedia.org/wiki/Cramer's_rule

Hope that helps!
- Blade -
Solving by using the cofactors is actually much harder and laborious than solving it directly.

Inverting a square matrix is the same as solving a system of four equations. This can be easily done by appending the identity matrix and converting the left half to reduced row echelon form.

Example:

Find inverse of:

[ a b c d ]
| e f g h |
| i j k l |
[ m n o p ]

1. So append the identity matrix:

[ a b c d | 1 0 0 0 ]
| e f g h | 0 1 0 0 |
| i j k l | 0 0 1 0 |
[ m n o p | 0 0 0 1 ]

2. Convert the left half to the identity matrix:

[ 1 0 0 0 | a' b' c' d' ]
| 0 1 0 0 | e' f' g' h' |
| 0 0 1 0 | i' j' k' l' |
[ 0 0 0 1 | m' n' o' p' ]

The right hand side is your matrix inverse:

[ a' b' c' d' ]
| e' f' g' h' |
| i' j' k' l' |
[ m' n' o' p' ]

Step 2 is the meat and potatoes of your problem, but is far less work than finding the cofactor matrix.

This topic is closed to new replies.

Advertisement