
Calculating Inverse Matrices
hi,
I''ve been brushing up on my matrix maths (from a pure maths/theoretical pov first - not programming it yet), and I''ve found Eric Lengyel''s ''Mathematics for 3D game programming and computer graphics extremely useful.
But I''m a little stuck with the algorithm presented on p32 (for those with a copy). It basically uses a form of gaussian elimination to calculate the inverse of a 3x3 matrix. However, the example in the book has lots of "convenient" numbers - such that the author can skip a few steps, my own examples/tests aren''t so simple...
can someone point me towards a fairly clear explanation of the maths for calculating inverses?
I''ve found a few chock-full of complex maths-speak, but I really need an idiots guide to get this straight in my head
cheers,
Jack
DirectX 4 VB: All you need for multimedia programming in Visual Basic
Formula 1 Championship Manager, My Game Project.

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>
May 14, 2003 05:48 PM
I''m sure there''s an article right here on gamedev.net about inverses of 3x3 matrices, but I can''t remember where. It''ll come up in a google search.
You could use the cofactor method, that''s probably as simple as it gets for 3x3 matrices.
The "cofactor" of a matrix element aij in the matrix is the determinant of the 2x2 matrix formed by removing the ith row and jth column.
The "cofactor matrix" is formed by replacing each element with its cofactor. Take its adjoint, divide by the determinant of the original matrix and you have the inverse.
If that''s not clear google will prob. have lots to say about it.
The "cofactor" of a matrix element aij in the matrix is the determinant of the 2x2 matrix formed by removing the ith row and jth column.
The "cofactor matrix" is formed by replacing each element with its cofactor. Take its adjoint, divide by the determinant of the original matrix and you have the inverse.
If that''s not clear google will prob. have lots to say about it.
well the method to do it by hand on paper is really simple:
write the matrix down.
below your matrix write the identity matrix of the same dimension.
now reduce your matrix stepwise to the identity with only the basic allowed operations (swapping lines, swapping columns, adding/subtracting a multiple of one line to another).
at each step you do the same transformation to the identity matrix. in the end (when your matrix has become identity) the identity will be the inverse of the original matrix.
short example to make clear what i mean:
first step was line2 - 2*line1
second step line1 - line2
but doing it computationally is a bit more complex and you will need to use A^-1 = (1/det A)* (adjoint A)^t (^t = transposed)
or some other way...
write the matrix down.
below your matrix write the identity matrix of the same dimension.
now reduce your matrix stepwise to the identity with only the basic allowed operations (swapping lines, swapping columns, adding/subtracting a multiple of one line to another).
at each step you do the same transformation to the identity matrix. in the end (when your matrix has become identity) the identity will be the inverse of the original matrix.
short example to make clear what i mean:
|1 1| |1 1| |1 0|A = |2 3| -> |0 1| -> |0 1| = Id |1 0| |1 0| |3 -1|Id = |0 1| -> |-2 1| -> |-2 1| = A^-1
first step was line2 - 2*line1
second step line1 - line2
but doing it computationally is a bit more complex and you will need to use A^-1 = (1/det A)* (adjoint A)^t (^t = transposed)
or some other way...
quote:
Original post by jollyjeffers
hi,
I''ve been brushing up on my matrix maths (from a pure maths/theoretical pov first - not programming it yet), and I''ve found Eric Lengyel''s ''Mathematics for 3D game programming and computer graphics extremely useful.
But I''m a little stuck with the algorithm presented on p32 (for those with a copy). It basically uses a form of gaussian elimination to calculate the inverse of a 3x3 matrix. However, the example in the book has lots of "convenient" numbers - such that the author can skip a few steps, my own examples/tests aren''t so simple...
can someone point me towards a fairly clear explanation of the maths for calculating inverses?
I''ve found a few chock-full of complex maths-speak, but I really need an idiots guide to get this straight in my head
I''m guessing you''re familiar with Gaussian Elimination? So, you have a matrix A that you want to find the inverse of. Let I be the identity matrix (you know, 0''s everywhere except the main diagonal, which is all 1''s). Now, set up something that looks like
[A|I]
Then you perform Gaussian Elimination on this. If you end up with something of the form
[I|B]
Then B is the inverse of A. If you don''t get the identity matrix on the left, there is no inverse. I personally prefer this to the adjoint method, there''s fewer calculations and the calculations are simpler. The adjoint method might be better if you''re writing a program to compute the inverse, there''s less logic involved. Oh, and in your own examples, make sure that the determinant is non-zero. If the determinant is 0, there''s no inverse.
May 14, 2003 08:55 PM
if this matrix happens to be a rotation matrix then you can simply transpose it. you didn''t mention what this was for, so i don''t know if that''s useful or not.
james
james
Way Walker''s approach is the most versatile and easily implemented. Most advanced calculators have a function to put a matrix in reduced row echelon form. Just input the matrix as walker said and hit the rref button. If you don''t have such a calculator, it''s easy to implement that function on your own or do it by hand.
May 15, 2003 01:14 AM
Calculating the inverse of a matrix is usually a waste of time. The only reason you would need to do so is to solve an equation of type Ax = b where you would solve for x by solving the equation x = bAinverse. A much faster way to do this is to compute the LU factorization of the matrix A so that LUx = b and L is a lower triangular matrix and U is an upper triangular matrix. Then you solve the equation Ly = b for y (which is trivial with a lower triangular matrix) and then solve Ux = y for x which again is trivial. This is demonstrably much faster than computing the inverse of matrix A.
Here''s more information at this link:
http://www.cs.ut.ee/~toomas_l/linalg/lin2/node4.html
Here''s more information at this link:
http://www.cs.ut.ee/~toomas_l/linalg/lin2/node4.html
ok, lets start with a 2x2
(I don''t know how to make the superscript fonts
, so the part to be superscript i will put in {}, and incase u didn''t know, one inside |, ie |A| is the determinant of the matrix A)
To find the inverse of
of the square matrix
we use the expression:
OK, thats a 2x2, the inverse (or reciprocal) of a 3x3 is...
First you need to find the cofactor inside a 3x3, so
The + and -''s come from a simple matrix grid
Then you fill in the grid by doing the above for each element, and you end up with
The transpose of the matrix of cofactors is from writing the rows as columns...
Now you have to find the determinant of the origional matrix
To get the determinant of the 3x3 matrix, you find the sum of the product of each element and its cofactor
You add up the determinants of all the elements in the 3x3 to give you the determinant of the 3x3 matrix
So, the inverse of the 3x3 matrix is...
The transpose of the cofactors is known as the adjoint, or adj.
So the inverse of a 3x3 matrix is:
Inverse = Adjoint/Determinant
Hope this helps
(I don''t know how to make the superscript fonts

To find the inverse of
A{-1}
of the square matrix
A = [a b] [c d]
we use the expression:
A{-1} = (1/|A|) [d -b] which = (1/(ad-bc)) [d -b] [-c a] [-c a]
OK, thats a 2x2, the inverse (or reciprocal) of a 3x3 is...
First you need to find the cofactor inside a 3x3, so
V-----------------------A = [3 4 -1] | [2 0 7] | [1 -3 -2] | |And the cofactor of the element 3 is, you basically cross out the row and colomn that that element passes through, so your left with a 2x2, ie. the cofactor of the element 3 leaves you withcofactor 3 = +[0 7] [-3 -2]ie. 21 (remember ad-bc)cofactor 4 = -[2 7] [1 -2]ie. 11
The + and -''s come from a simple matrix grid
[+ - +][- + -][+ - +]
Then you fill in the grid by doing the above for each element, and you end up with
Matrix of the cofactors is[21 11 -6][11 -5 13][28 -23 -8]
The transpose of the matrix of cofactors is from writing the rows as columns...
Transpose of the cofactor matrix[21 11 28][11 -5 -23][-6 13 -8]
Now you have to find the determinant of the origional matrix
Origional matrixA = [3 4 -1] [2 0 7] [1 -3 -2]
To get the determinant of the 3x3 matrix, you find the sum of the product of each element and its cofactor
cofactor 3 = +[0 7] [-3 -2]ie. 21, so the element (3)x the cofactor(21)3x21 + ...cofactor 4 = -[2 7] [1 -2]ie. 11, so 4x11 + ...
You add up the determinants of all the elements in the 3x3 to give you the determinant of the 3x3 matrix
determinant of 3x3 matrixA = [3 4 -1] = 113 [2 0 7] [1 -3 -2]
So, the inverse of the 3x3 matrix is...
The inverse of a 3x3 matrix...A = [3 4 -1] [2 0 7] [1 -3 -2]is (Transpose of cofactors)/Determinant, so...Transpose of the cofactor matrix[21 11 28][11 -5 -23][-6 13 -8]divide bydeterminant of 3x3 matrixA = [3 4 -1] = 113 [2 0 7] [1 -3 -2]gives...[21 11 28][11 -5 -23][-6 13 -8]___________ 133Which easily turns into 1 [21 11 28] ___ [11 -5 -23] 113 [-6 13 -8]
The transpose of the cofactors is known as the adjoint, or adj.
So the inverse of a 3x3 matrix is:
Inverse = Adjoint/Determinant
Hope this helps

as usual - Gamedev comes through with good answers
thanks everyone... think I''ve got a lot of possibilities to go on here 
I had originally tried doing it with gaussian elmination, but I got a little stuck near the end - had an odd number I couldn''t get rid of (the determinant was non-zero, so an inverse had to exist)...
Jack
DirectX 4 VB: All you need for multimedia programming in Visual Basic
Formula 1 Championship Manager, My Game Project.


I had originally tried doing it with gaussian elmination, but I got a little stuck near the end - had an odd number I couldn''t get rid of (the determinant was non-zero, so an inverse had to exist)...
Jack
DirectX 4 VB: All you need for multimedia programming in Visual Basic
Formula 1 Championship Manager, My Game Project.
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement