There are a large number of programming problems that involve the use of matrices and linear algebra. And when you have a matrix, there may be times where you need to know its determinant. For calculating the determinant of a 2x2, or a 3x3 matrix, there’s a fairly straightforward formula. On the other hand, if you need a generic solution for a matrix of any size, you have to get a little more complicated.

One of **Jarrod**’s co-workers had their own “optimized” solution for this. It’s optimized in the sense that it doesn’t always return the right answer. This particular sample is for 3x3
matrices, but don’t worry: it’s been copy/pasted into all the spots that need to work with 4x4 matrices too.

N6& = 3 P6& = 3 DE1: For Q& = 2 To P6& For S& = 1 To Q& - 1 If Z6& <> 0 Then Let Q& = N6&: Let S& = Q& - 1: Let Z6& = 0: GoTo DE1 If A#(S&, S&) <> 0 Then GoTo DE3 For U& = S& + 1 To P6&: If A#(U&, S&) <> 0 Then Let T& = -T&: GoTo DE2 Next U&: GoTo DE4 DE2: For Z6& = 1 To P6&: A6# = A#(U&, Z6&): A#(U&, Z6&) = A#(S&, Z6&): A#(S&, Z6&) = A6# Next Z6& DE3: V# = A#(Q&, S&) / A#(S&, S&) For R6& = 1 To P6&: A#(Q&, R6&) = A#(Q&, R6&) - V# * A#(S&, R6&): Next R6& Next S& Next Q& w# = 1 For X6& = 1 To P6&: w# = w# * A#(X6&, X6&): Next X6& DE4: Det# = (w# * T&)

Don’t let the unusual characters fool you. This was found in a Visual Basic program, although it started life in some other dialect.