Sign in to follow this  

Is this incorrect?

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I am starting to test my matrix classes that I made and I was wondering if I am doing my inverse and determinent calculations correctly. I belive they are correct but when I compair the results to that of D3DXMatrix both are off a bit. The results are very close to each other but I dont understand why they are not exactly the same, but this seems only the case on matrices that have large values as I get exact results when I use small values. Here is the code for my determinant calculation:
inline Scalar Matrix4x4::GetDeterminant(void) const
{
	return(	(_11 * _22 * _33 * _44) - (_11 * _22 * _34 * _43) + (_11 * _23 * _34 * _42) - (_11 * _23 * _32 * _44) + 
		(_11 * _24 * _32 * _43) - (_11 * _24 * _33 * _42) - (_12 * _23 * _34 * _41) + (_12 * _23 * _31 * _44) -
		(_12 * _24 * _31 * _43) + (_12 * _24 * _33 * _41) - (_12 * _21 * _33 * _44) + (_12 * _21 * _34 * _43) +
		(_13 * _24 * _31 * _42) - (_13 * _24 * _32 * _41) + (_13 * _21 * _32 * _44) - (_13 * _21 * _34 * _42) +
		(_13 * _22 * _34 * _41) - (_13 * _22 * _31 * _44) - (_14 * _21 * _32 * _43) + (_14 * _21 * _33 * _42) -
		(_14 * _22 * _33 * _41) + (_14 * _22 * _31 * _43) - (_14 * _23 * _31 * _42) + (_14 * _23 * _32 * _41) );
}
and here is my inverse calculation:
inline Matrix4x4 Matrix4x4::GetInverse(void) const
{
	Assert( GetDeterminant() != 0 );
	Scalar determinant = 1.0f / GetDeterminant();

	return Matrix4x4( ((_22 * _33 * _44) + (_23 * _34 * _42) + (_24 * _32 * _43) - (_22 * _34 * _43) - (_23 * _32 * _44) - (_24 * _33 * _42)) * determinant,
			((_12 * _34 * _43) + (_13 * _32 * _44) + (_14 * _33 * _42) - (_12 * _33 * _44) - (_13 * _34 * _42) - (_14 * _32 * _43)) * determinant,
			((_12 * _23 * _44) + (_13 * _24 * _42) + (_14 * _22 * _43) - (_12 * _24 * _43) - (_13 * _22 * _44) - (_14 * _23 * _42)) * determinant,
			((_12 * _24 * _33) + (_13 * _22 * _34) + (_14 * _23 * _32) - (_12 * _23 * _34) - (_13 * _24 * _32) - (_14 * _22 * _33)) * determinant,
			((_21 * _34 * _43) + (_23 * _31 * _44) + (_24 * _33 * _41) - (_21 * _33 * _44) - (_23 * _34 * _41) - (_24 * _31 * _43)) * determinant,
			((_11 * _33 * _44) + (_13 * _34 * _41) + (_14 * _31 * _43) - (_11 * _34 * _43) - (_13 * _31 * _44) - (_14 * _33 * _41)) * determinant,
			((_11 * _24 * _43) + (_13 * _21 * _44) + (_14 * _23 * _41) - (_11 * _23 * _44) - (_13 * _24 * _41) - (_14 * _21 * _43)) * determinant,
			((_11 * _23 * _34) + (_13 * _24 * _31) + (_14 * _21 * _33) - (_11 * _24 * _33) - (_13 * _21 * _34) - (_14 * _23 * _31)) * determinant,
			((_21 * _32 * _44) + (_22 * _34 * _41) + (_24 * _31 * _42) - (_21 * _34 * _42) - (_22 * _31 * _44) - (_24 * _32 * _41)) * determinant,
			((_11 * _34 * _42) + (_12 * _31 * _44) + (_14 * _32 * _41) - (_11 * _32 * _44) - (_12 * _34 * _41) - (_14 * _31 * _42)) * determinant,
			((_11 * _22 * _44) + (_12 * _24 * _41) + (_14 * _21 * _42) - (_11 * _24 * _42) - (_12 * _21 * _44) - (_14 * _22 * _41)) * determinant,
			((_11 * _24 * _32) + (_12 * _21 * _34) + (_14 * _22 * _31) - (_11 * _22 * _34) - (_12 * _24 * _31) - (_14 * _21 * _32)) * determinant,
			((_21 * _33 * _42) + (_22 * _31 * _43) + (_23 * _32 * _41) - (_21 * _32 * _43) - (_22 * _33 * _41) - (_23 * _31 * _42)) * determinant,
			((_11 * _32 * _43) + (_12 * _33 * _41) + (_13 * _31 * _42) - (_11 * _33 * _42) - (_12 * _31 * _43) - (_13 * _32 * _41)) * determinant,
			((_11 * _23 * _42) + (_12 * _21 * _43) + (_13 * _22 * _41) - (_11 * _22 * _43) - (_12 * _23 * _41) - (_13 * _21 * _42)) * determinant,
			((_11 * _22 * _33) + (_12 * _23 * _31) + (_13 * _21 * _32) - (_11 * _23 * _32) - (_12 * _21 * _33) - (_13 * _22 * _31)) * determinant ); 
}
Here are the results of the two when run on a identical matrix which is: [0996.0, 3868.0, 2087.0, 4710.0] [1512.0, 6467.0, 4729.0, 1959.0] [1188.0, 2772.0, 4119.0, 3150.0] [2052.0, 9570.0, 4525.0, 8204.0] My Determinant: 4.0801518e+012 D3DXMatrix Determinant: 4.0801413e+012 My Inverse: [ 0.026646148, 0.0087145399, -0.0076347436, -0.014447306 ] [-0.0019588103, -0.00046090866, 0.00028593006, 0.0011248461 ] [-0.0052189413, -0.0016387368, 0.0018109080, 0.0026922403 ] [-0.0015012644, -0.00073818321, 0.00057725311, 0.00057725311] D3DXMatrix Inverse: [ 0.026646079, 0.0087145176, -0.0076347236, -0.014447271 ] [-0.0019588049, -0.00046090782, 0.00028592919, 0.0011248435 ] [-0.0052189277, -0.0016387325, 0.0018109034, 0.0026922335 ] [-0.0015012607, -0.00073818141, 0.00057725160, 0.00093840674] As you can see the differance in the two is very small but I dont understand the reason for the discrepency if I am doing it correctly. Anyone have any ideas?

Share this post


Link to post
Share on other sites
Well, there are different ways to formulate both the inverse and the determinant. If D3D is doing the math in a different form than you are, the results are likely to be a bit different due to floating point imprecision.

I did notice that your inverse 44 term disagrees more with the D3D result than your other terms, so you might want to double check the code for that term.

Other than that I would say don't worry about the discrepencies, as they look like they are within a reasonable tolerance for limited precision floating point calculations.

Share this post


Link to post
Share on other sites

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this