Very handy matrix functions - part 1
I finished reading my 3D math book and let me tell you it was full of information that I've been searching for...for a very long time.
Granted the D3DX library of functions can do most anything with matrices but it is nice to know the math behind it.
For instance...here is how to find the determinant of a 2D matrix.
M=
[m11 m12]
[m21 m22]
(:<X>: denotes the determinant of matrix X)
:M:=((m11m22)-(m12m21))
So for a 3x3 would be:
M=
[m11 m12 m13]
[m21 m22 m23]
[m31 m32 m33]
((m11m22m33)+(m12m23m31)+(m13m21m32))-
((m13m22m31)+(m12m21m33)+(m11m23m32))
=
m11(m22m33-m23m32)+m12(m23m31-m21m33)+m13(m21m32-m22m31)
But this changes for an n x n matrix.
Essentially you must divide the matrix up into submatrixes called cofactors. A cofactor is obtained by simply discarding the rows stated. So C11 would mean to discard the 1st column and 1st row - the matrix you have left is the cofactor.
Take the sum of all the determinants of the cofactors and you have the determinant of the matrix.
Here is an example of a cofactor:
M=
[m11 m12 m13]
[m21 m22 m23]
[m31 m32 m33]
C11=
[m22 m23]
[m32 m33]
:C11:=(m22m33)-(m23m32)
So:
:C11:+
:C12:+
:C13:+
:C21:+
:C22:+
:C23:+
:C31:+
:C32:+
:C33:=
:M:
Now using this you can compute the inverse of a matrix...if it has one.
Once you compute the cofactors you can then find the adjoint of M
So our matrix of cofactors now looks like this (w/o having actually computed the determinants of the cofactors):
A=
[c11 c12 c13]
[c21 c22 c23]
[c31 c32 c33]
adj A=
[c11 c21 c31]
[c12 c22 c32]
[c13 c23 c33]
Find the inverse by now dividing by the determinant of M
[c11 c21 c31]
[c12 c22 c32]
[c13 c23 c33]
-----------------
:M:
or
(X^-1 denotes X to the -1 or the inverse of X)
M^-1=
[c11/:M: c21/:M: c31/:M:]
[c12/:M: c22/:M: c32/:M:]
[c13/:M: c23/:M: c33/:M:]
so:
M^-1=adj M/:M:
To test whether or not a matrix even has an inverse - its determinant cannot be zero. So if the determinant of a matrix is non-zero then it does have an inverse else it does not.
I've been looking for this info for a long time and finally found a good explanation of it.
Final XYZ rotation matrix
cosx=cos(x)
sinx=sin(x)
cosy=cos(y)
siny=sin(y)
cosz=cos(z)
sinz=sin(z)
R=
[m11 m12 m13]
[m21 m22 m23]
[m31 m32 m33]
m11=(cosy)(cosz)+(siny)(sinx)(siny)
m12=(-cosy)(sinz)+(siny)(sinx)(cosz)
m13=(siny)(cosx)
m21=(sinz)(cosx)
m22=(cosz)(cosx)
m23=(-sinx)
m31=(-siny)(cosz)+(cosy)(sinx)(sinz)
m32=(sinz)(siny)+(cosy)(sinx)(cosz)
m33=(cosy)(cosx)