Calculate the inverse of a 4x4 matrix - Why is this code not working?

Oct 15, 2016 at 11:24am
Hello, I am trying to calculate the inverse of a 4x4, I have been thinking about it endlessly yet I can't seem to be able to do it. So I was wondering, how would you calculate the inverse of a 4x4 in C++?

EDIT: I'm trying to make this calculate the determinant of a 4x4, how can I do it? Currently this only calculate the determinant of a 3x3
1
2
3
4
5
 for(i=0;i<3;i++)
 {
  determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*
  a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
 }
Last edited on Oct 15, 2016 at 11:41am
Oct 15, 2016 at 12:47pm
Sorry to be rude, but if you bash people who give help on this forum, you are unlikely to get any help with your question.
Oct 15, 2016 at 8:17pm
Two common ways of evaluating a determinant are:
- recursively (i.e. expand a 4x4 determinant in terms of 3x3 ones etc.)
- sequence of row operations to reduce to upper (or lower) triangular, then just multiply the elements on the diagonal.

I tend to prefer the latter.
Oct 15, 2016 at 9:24pm
Thank you lastchance.

Orange, stop siding with UK Marine.
Oct 15, 2016 at 9:35pm
Although the inverse of a matrix can be written down in terms of determinants, if you are ultimately after that inverse, rather than determinants per se, it is probably quickest to use the Gauss-Jordan method (with sequences of row operations); see:
https://www.mathsisfun.com/algebra/matrix-inverse-row-operations-gauss-jordan.html

It works for any size matrix and for large matrices will be considerably more efficient than working out a lot of determinants and cofactors.
Topic archived. No new replies allowed.