Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculating Inverse Matrices

Started by jollyjeffers May 14, 2003 at 5:40 PM 9 replies 5.5k views
Original Post
jollyjeffers
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 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 |
sQuid
sQuid
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.
Burning_Ice
Burning_Ice
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:


  
|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...


Way Walker
Way Walker
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.
kdogg
kdogg
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.
malpass
malpass
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
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 with

cofactor 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 matrix
A = [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 matrix
A = [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 by
determinant of 3x3 matrix
A = [3 4 -1] = 113
[2 0 7]
[1 -3 -2]

gives...

[21 11 28]
[11 -5 -23]
[-6 13 -8]
___________
133

Which 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
jollyjeffers
jollyjeffers
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.
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.