Calculating the Determinant of a 3 x 3 Matrix

04 Aug 2018

C#, Maths


Calculating the determinant of a 3 x 3 matrix is relatively trivial; the Laplace expansion is the method used here.

Consider the following 3 x 3 matrix:

`[[a,b,c],[d,e,f],[g,h,i]]`

The formula for calculating the determinant of this matrix follows this form:

`d = (a * (ei - fh)) - (b * (di - fg)) + (c * (dh - eg))`

And that's it.

Example

`[[6,1,1],[4,-2,5],[2,8,7]]`

`d = (6 * ((-2 * 7) - (5 * 8))) - (1 * ((4 * 7) - (5 * 2))) + (1 * ((4 * 8) - (-2 * 2)))`

`=> (6 * ((-14) - (40))) - (1 * ((28) - (10))) + (1 * ((32) - (-4)))`

`=> (6 * (-14 - 40)) - (1 * (28 - 10)) + (1 * (32 - -4))`

`=> (6 * (-54)) - (1 * (18)) + (1 * (36))`

`=> (6 * -54) - (1 * 18) + (1 * 36)`

`=> (-324) - (18) + (36)`

`=> -324 - 18 + 36`

`=> -306`

Note that the above calculations have been arranged so they can be ported directly to code.


decimal a = 6; 
decimal b = 1; 
decimal c = 1; 

decimal d = 4; 
decimal e = -2; 
decimal f = 5; 

decimal g = 2; 
decimal h = 8; 
decimal i = 7; 

decimal ei = e * i; 
decimal fh = f * h; 

decimal di = d * i; 
decimal fg = f * g; 

decimal dh = d * h; 
decimal eg = e * g; 

decimal determinant = (a * (ei - fh)) - (b * (di - fg)) + (c * (dh - eg)); 

 

Copyright © 2024 carlbelle.com