#### Archived

This topic is now archived and is closed to further replies.

# Matrix Inversion

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

## Recommended Posts

Can anyone tell me where I can find help on the maths behind a function for Inverting a 4x4 Matrix? I need to make a function to invert a matrix, but I don''t know how. Thanks. One by one, the peguins steal my sanity.

##### Share on other sites
Look for some tutorials on matrix math or linear algebra. They''re all over the web.

I like pie.

##### Share on other sites
first transpose the matrix( rows become the columns )
then divide every element by the determinant of the original matrix.

i''m not sure how to go about coding it but that should help get you started.

i got this off the net somewhere, but it was a couple months ago sorry i can''t give you an address.

##### Share on other sites
Almost, you need to calculate the cofactor for each position of the transposed matrix, and divide that by the determinant.
The cofactor of position i,j is the determinant of the 3x3 matrix obtained by removing row i and column j from the original 4x4.

##### Share on other sites
Most computer implementations would use elemtary row operations, rather than calculating cofactors, for finding the inverse of a matrix. This is much less computationally expensive, especially on big matrices.

##### Share on other sites
quote:
Original post by sjelkjd
Almost, you need to calculate the cofactor for each position of the transposed matrix, and divide that by the determinant.
The cofactor of position i,j is the determinant of the 3x3 matrix obtained by removing row i and column j from the original 4x4.

Almost, you multiply the cofactor by (-1) to the power of (i+j).

This is called the adjoint of a matrix.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
quote:
Original post by Bobby-D
Can anyone tell me where I can find help on the maths behind a function for Inverting a 4x4 Matrix? I need to make a function to invert a matrix, but I don't know how. Thanks.

If your 4x4 matrix is a combination of a 3x3 matrix M and a 3 vector V:
T = | M V |    | 0 1 |

(hope the terminology makes sense...)

then
T_inv = | M_inv (-M_inv * V) |        | 0         1        |

And if the 3x3 matrix is just a rotation matrix, then M_inv = transpose(M), which makes things very simple indeed.

[edited by - MrRowl on January 14, 2004 10:10:32 AM]

##### Share on other sites
If you actually want to code a matrix inversion function and understand why it works, I''d recomend checking out the matrix algebra section at SOS Math. It covers gaussian elimination (which can be used to invert a matrix), and personally I found it to be explained in a very clear manner, so when I actually wrote my matrix inverse function I knew what it was actually doing.

##### Share on other sites
Knowing Gaussian elimination is helpful for understanding matrix inversion, but I would only recommend it for large matrices. I''d use the

inv (M) = ( 1/det (M) ) adjoint (M)

method for 3x3 or 4x4 matrices.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
quote:
Original post by Paradigm Shifter
quote:
Original post by sjelkjd
Almost, you need to calculate the cofactor for each position of the transposed matrix, and divide that by the determinant.
The cofactor of position i,j is the determinant of the 3x3 matrix obtained by removing row i and column j from the original 4x4.

Almost, you multiply the cofactor by (-1) to the power of (i+j).

This is called the adjoint of a matrix.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This method is known as Cramer''s rule. It happens to be EXTREMELY expensive as the matrix gets larger. NOT recommended in practice for large matrices. See the interesting tables in the link below. A computer with teraflops of computing power would require 4 *YEARS* to compute the inverse of a 20x20 matrix using Cramer''s rule!

http://www2.math.uic.edu/~hanson/mcs471/cramers.html

Fortunately, for very small matrices, its not much different from Gaussian Elimination or other methods. As Paradigm Shifter pointed out, the technique is quite fine and good for 3x3 or 4x4 matrices.

Note that for transformation matrices, there are smarter ways.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
float determinant4f(const float m[16]){  return    m[12]*m[9]*m[6]*m[3]-    m[8]*m[13]*m[6]*m[3]-    m[12]*m[5]*m[10]*m[3]+    m[4]*m[13]*m[10]*m[3]+    m[8]*m[5]*m[14]*m[3]-    m[4]*m[9]*m[14]*m[3]-    m[12]*m[9]*m[2]*m[7]+    m[8]*m[13]*m[2]*m[7]+    m[12]*m[1]*m[10]*m[7]-    m[0]*m[13]*m[10]*m[7]-    m[8]*m[1]*m[14]*m[7]+    m[0]*m[9]*m[14]*m[7]+    m[12]*m[5]*m[2]*m[11]-    m[4]*m[13]*m[2]*m[11]-    m[12]*m[1]*m[6]*m[11]+    m[0]*m[13]*m[6]*m[11]+    m[4]*m[1]*m[14]*m[11]-    m[0]*m[5]*m[14]*m[11]-    m[8]*m[5]*m[2]*m[15]+    m[4]*m[9]*m[2]*m[15]+    m[8]*m[1]*m[6]*m[15]-    m[0]*m[9]*m[6]*m[15]-    m[4]*m[1]*m[10]*m[15]+    m[0]*m[5]*m[10]*m[15];}bool generateInverseMatrix4f(float i[16], const float m[16]){  float x=determinant4f(m);  if (x==0) return false;  i[0]= (-m[13]*m[10]*m[7] +m[9]*m[14]*m[7] +m[13]*m[6]*m[11]         -m[5]*m[14]*m[11] -m[9]*m[6]*m[15] +m[5]*m[10]*m[15])/x;  i[4]= ( m[12]*m[10]*m[7] -m[8]*m[14]*m[7] -m[12]*m[6]*m[11]         +m[4]*m[14]*m[11] +m[8]*m[6]*m[15] -m[4]*m[10]*m[15])/x;  i[8]= (-m[12]*m[9]* m[7] +m[8]*m[13]*m[7] +m[12]*m[5]*m[11]         -m[4]*m[13]*m[11] -m[8]*m[5]*m[15] +m[4]*m[9]* m[15])/x;  i[12]=( m[12]*m[9]* m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10]         +m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]* m[14])/x;  i[1]= ( m[13]*m[10]*m[3] -m[9]*m[14]*m[3] -m[13]*m[2]*m[11]         +m[1]*m[14]*m[11] +m[9]*m[2]*m[15] -m[1]*m[10]*m[15])/x;  i[5]= (-m[12]*m[10]*m[3] +m[8]*m[14]*m[3] +m[12]*m[2]*m[11]         -m[0]*m[14]*m[11] -m[8]*m[2]*m[15] +m[0]*m[10]*m[15])/x;  i[9]= ( m[12]*m[9]* m[3] -m[8]*m[13]*m[3] -m[12]*m[1]*m[11]         +m[0]*m[13]*m[11] +m[8]*m[1]*m[15] -m[0]*m[9]* m[15])/x;  i[13]=(-m[12]*m[9]* m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10]         -m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]* m[14])/x;  i[2]= (-m[13]*m[6]* m[3] +m[5]*m[14]*m[3] +m[13]*m[2]*m[7]         -m[1]*m[14]*m[7]  -m[5]*m[2]*m[15] +m[1]*m[6]* m[15])/x;  i[6]= ( m[12]*m[6]* m[3] -m[4]*m[14]*m[3] -m[12]*m[2]*m[7]         +m[0]*m[14]*m[7]  +m[4]*m[2]*m[15] -m[0]*m[6]* m[15])/x;  i[10]=(-m[12]*m[5]* m[3] +m[4]*m[13]*m[3] +m[12]*m[1]*m[7]         -m[0]*m[13]*m[7]  -m[4]*m[1]*m[15] +m[0]*m[5]* m[15])/x;  i[14]=( m[12]*m[5]* m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6]         +m[0]*m[13]*m[6]  +m[4]*m[1]*m[14] -m[0]*m[5]* m[14])/x;  i[3]= ( m[9]* m[6]* m[3] -m[5]*m[10]*m[3] -m[9]* m[2]*m[7]         +m[1]*m[10]*m[7]  +m[5]*m[2]*m[11] -m[1]*m[6]* m[11])/x;  i[7]= (-m[8]* m[6]* m[3] +m[4]*m[10]*m[3] +m[8]* m[2]*m[7]         -m[0]*m[10]*m[7]  -m[4]*m[2]*m[11] +m[0]*m[6]* m[11])/x;  i[11]=( m[8]* m[5]* m[3] -m[4]*m[9]* m[3] -m[8]* m[1]*m[7]         +m[0]*m[9]* m[7]  +m[4]*m[1]*m[11] -m[0]*m[5]* m[11])/x;  i[15]=(-m[8]* m[5]* m[2] +m[4]*m[9]* m[2] +m[8]* m[1]*m[6]         -m[0]*m[9]* m[6]  -m[4]*m[1]*m[10] +m[0]*m[5]* m[10])/x;  return true;}

[edited by - circuit on January 14, 2004 1:49:47 PM]

##### Share on other sites
Okay, in order to do it by Gauss reduction (I assume you know how to do Gauss reduction?!?):

Assume you want to invert matrix A, set matrix B to the identity matrix.

Reduce A to the identity matrix (using Gauss reduction) but use exactly the same operations on matrix B that you used on matrix A.

When eventually matrix A becomes the identity matrix, matrix B will be your original matrix A''s inverse.

Simple no?

##### Share on other sites
Wow, thanks guys for all the replies. It''s all been very helpful. My knowledge of matricies is fairly limited, but I think i''m starting to understand. At least now I know what to look for. Much Appreciated.