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
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.
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.