Laplace Expansion
The determinant of any square matrix can be found trough Laplace Expansion. The formula presented in section 7a: 2x2 determinant to find the determinant of a 2x2 matrix is the result of Laplace Expansion.
To find the determinant of a matrix trough Laplace Epxansion, multiply every element in the top row by it's cofactor and sum the result. The formula looks like this:
$$ | M | = \sum_{i=1}^{n} M_{1i}cofactor(M_{1i}) $$
It doesn'thave to be the first row, any row will do. The choice to use the first row is arbitrary.
The implementation provided here is naive. Calling Cofactor
will calculate the entire cofactor matrix, which is not desired. Instead of doing exactly this, try to manually inline the work so the Determinant function does not call any other functions.
float Determinant(mat3 m) { mat3 c = Cofactor(m); float _11 = m.v[0] * c.v[0]; float _12 = m.v[3] * c.v[3]; float _13 = m.v[6] * c.v[6]; return _11 + _12 + _13; } float Determinant(mat4 m) { mat4 c = Cofactor(m); float _11 = m.v[0] * c.v[0]; float _12 = m.v[4] * c.v[4]; float _13 = m.v[8] * c.v[8]; float _14 = m.v[12] * c.v[12]; return _11 + _12 + _13 + _14; }