Matrix Inversion

Started by
11 comments, last by Bobby-D 20 years, 3 months ago
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]
Advertisement
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?
do unto others... and then run like hell.
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.
One by one, the peguins steal my sanity.

This topic is closed to new replies.

Advertisement