DETERMINANTS AND THE BAREISS ALGORITHM

If you have to calculate determinants, and especially if you have to program an algorithm, investigate the Bareiss algorithm. It's remarkably fast; it limits the divisions so that it doesn't introduce needless rounding errors; and if your matrix elements are all integers, Bareiss is guaranteed to give you an integer result.

I've worked out a way to do Bareiss on pen and paper; here's a link to a PDF showing the technique:

http://www.paprikash.com/lou/bareiss.pdf

#determinant #bareiss

I've been doing some timing on various determinant algorithms. The four I tested were as follows:

- Laplace Expansion
- walking through all the permutations
- Bareiss
- just having a hard-coded formula for all the multiplications for a given sized matrix

First, I compared Laplace, Permutations, and Bareiss on a 10x10 matrix. The box scores:

Permutations: 12.5 seconds
Laplace: 7.5 seconds
Bareiss: 0.004 seconds

Bareiss is the clear winner on big matrices.

I also tested smaller matrices, such as 3x3 and 4x4 and 5x5. Since they can all calculate a determinant on a small matrix in the blink of an eye, I set up loops to rerun the calculations thousands of times to get measurable timing out of them. Here is what I came up with for 4x4 matrices:

Permutations: 8 seconds
Laplace: 5.5 seconds
Bareiss: 1.2 seconds
Hard-Coded Formula: 0.6 seconds

Once again, Bareiss is much faster than Permutations and Laplace. They're not as comparatively bad as before, but even so, Bareiss is still doing much better.

But also, the hard-coded formula is starting to look good. It is in fact the fastest approach up to 4x4, after which Bareiss is the way to go.

So, based on my testing:

- Matrices from 0x0 to 4x4: use the hard-coded formula.

- Matrices 5x5 and larger: use Bareiss.

- Laplace and Permutations shouldn't be used.

I did find a use for the Permutations method though: I modified it to generate the hard-coded formulas. All I really had to do was, instead of multiplying and adding/subtracting terms to calculate a determinant, I concatenated terms into strings that constituted valid source code. For example, the code it generated for a 2x2 matrix was:

mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]

That's how I generated the hard-coded formulas for 4x4 and 5x5.