**Story:**

For my seminar work, I had to calculate the determinant of a large integer matrix.

In this case, large meant n>=500. You might say that this isn’t very large and I would agree. However, it is large enough to overflow a float and reach +inf. Reaching +inf is bad: once you do, you lose all the accuracy of the computation. Also, since determinant calculations use addition as well as subtraction, if +inf is reached in the middle of the calculation the value might never drop back, even if the determinant is a “small” number.

(Note: Even if the calculation hadn’t reached +inf, but just some very large floating point number, the same phenomenon could have occurred.)

As I said, my matrix was composed of integers. Since a determinant is a sum of multiples of matrix elements, you would expect such a determinant to be an integer as well. It would be nice to take advantage of that fact.

How should one compute such a determinant? Well, luckily Python has support for arbitrarily large integers. Let’s compute the determinant with them!

Unfortunately, Python’s numpy computes determinants using LU decomposition, which uses division – hence, floating point values.

Well, never mind, I know how to calculate a determinant, right?

Ten minutes later the naive determinant calculation by minors was implemented, with one “minor” setback – it takes O(n!) time.

Googling for integer determinants yielded some articles about division free algorithms, which weren’t really easy to implement. There was one suggestion about using Rational numbers and Gauss Elimination, with pivots chosen to tame the growth of the nominator and denominator.

So, I hitched up a rational number class, using Python’s arbitrarily large integers as nominator and denominator. Then, I got some code to do Gauss Elimination, although my pivoting wasn’t the most fancy. This seemed to work, and I got my desired large determinants.

**Epilogue:**

Not too long ago, I ran across mpmath, a Python library for arbitrarily large floating point numbers. It is possible I could have used it instead of my own rational numbers. Next time I run into a relevant problem I’ll keep this library in mind.

Also, an even shorter while ago, I became aware that since 2.6 Python boasts a fractions module. This will sure come in handy in the future.

**Code:**

Available here. If you intend to use it in Python 2.6 and above, I suggest replacing my Rational class with Python’s Fraction.

(Note that I adapted this module to my needs. Therefore it has a timer that prints time estimations for computations, and it uses Psyco.)

## 2 replies on “Computing Large Determinants in Python”

Or you could fire up good ol’ Sage* and just write down

sage: Matrix([[1,2,3],[4,5,7],[9,8,9]])

[1 2 3]

[4 5 7]

[9 8 9]

sage: _.det()

4

* http://sagemath.org/

That’s excellent!

I looked at sage a long time ago, but I didn’t have it mind.

I checked out their matrix determinant, and they use some really specialized algorithms. It’s really fast!

Thanks a bunch!