Does anyone know how to get the inverse of a 4*4 matrix? If matrix M =
 a b c d 
 e f g h 
 i j k l 
 m n o p 
what would M^1 be?
Inverse Matrix
Started by masonium, Oct 11 2001 11:51 AM
10 replies to this topic
Sponsor:
#2 Members  Reputation: 495
Posted 11 October 2001  02:50 PM
There is no formula because it is found procedurally. If you have a matrix with actual values then you can take the cross product of that matrix with the one you listed. That produces a set of 16 equations with 16 unknowns. The cross product gives the left hand side of the equation and the identity matrix gives the right hand side. You then have to solve that system of equations for the sixteen unknowns. I think the easiest way to do that is to put it in a 17 by 16 matrix and transform it to reduced row echelon form.
Using a simple example say x+y=1 and 2x3y=1.
The process is pretty straight forward so hopefully that is enough to get you going. If not then I would suggest a book on linear algebra or trying to find a tutorial on solving systems of linear equations online.
Using a simple example say x+y=1 and 2x3y=1.

The process is pretty straight forward so hopefully that is enough to get you going. If not then I would suggest a book on linear algebra or trying to find a tutorial on solving systems of linear equations online.
#4 Members  Reputation: 133
Posted 12 October 2001  05:13 AM
The Inverse of a n*n matrix can easily be computed by using the Gaussalgorithm. Transform your matrix to A and I I=Identity matrix:
a11 a12 a13 ..a1j 1 0 0 0 0...n
a21 a22 a23 ..a2j 0 1 0 0 0...n
a31 a32 a33 ..a3j 0 0 1 0 0...n and so on
Now apply the Gaussalgorithm on this n*n equation system. It will give you a matrix of the form:
a11 a12 a13...a1j 1 0 0 0 0...n
0 a22 a23...a2j val 1 val val val...n
0 0 a33...a3j val val 1 ....
The matrix on the right (former identity) are now used as
right handside vectors (y) of the equationsystem Ax=y1..yn, where
Ax is the matrix on the left. You can now calculate recursivly the solution of this equation system, starting at aij up to a11 and the first column of the right matrix as y, then proceeding, using y2 for the same leftside and so on.
For ex. a 4*4 matrix would give you 4 4dimensional solutions.
These 4 vectors put in a matrix are the transposed inverted matrix. To get the regular inverse just swap the lines with
the colums. The algorithm also calculates the determinant as a
positive sideeffect. detA=product of aii. This algorithm seems complicated at first, but is very effective if bigger matrices are involved.
There exist many variation of the Gaussalgorithm. I won''t explain ít here, but you should find enough information on the net.
good luck cruz
a11 a12 a13 ..a1j 1 0 0 0 0...n
a21 a22 a23 ..a2j 0 1 0 0 0...n
a31 a32 a33 ..a3j 0 0 1 0 0...n and so on
Now apply the Gaussalgorithm on this n*n equation system. It will give you a matrix of the form:
a11 a12 a13...a1j 1 0 0 0 0...n
0 a22 a23...a2j val 1 val val val...n
0 0 a33...a3j val val 1 ....
The matrix on the right (former identity) are now used as
right handside vectors (y) of the equationsystem Ax=y1..yn, where
Ax is the matrix on the left. You can now calculate recursivly the solution of this equation system, starting at aij up to a11 and the first column of the right matrix as y, then proceeding, using y2 for the same leftside and so on.
For ex. a 4*4 matrix would give you 4 4dimensional solutions.
These 4 vectors put in a matrix are the transposed inverted matrix. To get the regular inverse just swap the lines with
the colums. The algorithm also calculates the determinant as a
positive sideeffect. detA=product of aii. This algorithm seems complicated at first, but is very effective if bigger matrices are involved.
There exist many variation of the Gaussalgorithm. I won''t explain ít here, but you should find enough information on the net.
good luck cruz
#5 Anonymous Poster_Anonymous Poster_* Guests  Reputation:
Posted 12 October 2001  05:51 AM
Or if you are using the D3DX stuff, you can simply just call a D3DXMatrixInverse!
Jeff
Jeff
#6 Members  Reputation: 495
Posted 12 October 2001  06:08 AM
Hehe, that''s a bit easier isn''t it. I''m a bit lost on the last part about the transpose.
I don''t see where the transpose comes into play.

I don''t see where the transpose comes into play.
#7 Members  Reputation: 133
Posted 12 October 2001  07:06 AM
The problem with LilBudyWizer's algorithm is that it presumes that all pivotelements aii (the diagonalelements used to eliminate the lines below) are different from 0. Otherwise a divide by 0 would occur. The algorithm I described uses 1dimensional pivotselection. (Sorry I forgot to mention that) This variation of Gauss can deal with the occurance of 0 as a pivot element. (It just selects another one, different from 0). If the algorithm can't find an pivot element different from 0 throughout the whole column, it terminates, which mathematically means the determinant is 0. That means the matrix is singular and cannot be inverted.
LilBudyWizer reduces the matrix at first to an upper triangular matrix and then to its canonical form. You can also do so, but for large matrices, the version with gausspivot is far more effective (with large I mean 4 and greater). To reduce the upper triangular matrix to ist canonical form you need to do the Gauss reduction one more time. This take actually more time
greets cruz
Edited by  cruz on October 12, 2001 2:17:48 PM
Edited by  cruz on October 12, 2001 2:22:15 PM
LilBudyWizer reduces the matrix at first to an upper triangular matrix and then to its canonical form. You can also do so, but for large matrices, the version with gausspivot is far more effective (with large I mean 4 and greater). To reduce the upper triangular matrix to ist canonical form you need to do the Gauss reduction one more time. This take actually more time
greets cruz
Edited by  cruz on October 12, 2001 2:17:48 PM
Edited by  cruz on October 12, 2001 2:22:15 PM
#11 Anonymous Poster_Anonymous Poster_* Guests  Reputation:
Posted 14 October 2001  10:08 AM
Try this for general matrix/quaternion questions and answers:
[link]
http://skal.planetd.net/demo/matrixfaq.htm
[/link]
You can find a very optimal way of performing a 4x4 inverse here:
[link]
http://developer.intel.com/design/pentiumiii/sml/24504301.pdf
[/link]
According to Intel, performing a matrix inverse yields faster results for Cramer''s rule than Gaussian elimination with dimensions <= 6. All the information and source code is available at the above link.
Bear in mind also that if your 4x4 matrices are *only* a combination of any of the following: translation, rotation, or scale, you can perform an "affine matrix" inverse much quicker than the above methods. Information available here:
[link]
http://www.cs.unc.edu/~gotz/code/affinverse.html
[/link]
HTH
[link]
http://skal.planetd.net/demo/matrixfaq.htm
[/link]
You can find a very optimal way of performing a 4x4 inverse here:
[link]
http://developer.intel.com/design/pentiumiii/sml/24504301.pdf
[/link]
According to Intel, performing a matrix inverse yields faster results for Cramer''s rule than Gaussian elimination with dimensions <= 6. All the information and source code is available at the above link.
Bear in mind also that if your 4x4 matrices are *only* a combination of any of the following: translation, rotation, or scale, you can perform an "affine matrix" inverse much quicker than the above methods. Information available here:
[link]
http://www.cs.unc.edu/~gotz/code/affinverse.html
[/link]
HTH