Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 152: Intro to Mathematical Software
2017-02-06
Kiran Kedlaya; University of California, San Diego
adapted from lectures by William Stein, University of Washington
Lecture 12: Linear algebra (part 3)
As usual, HW due Tuesday at 8pm. Peer evaluations due Thursday at 8pm.
Multimodular echelon form: a peek behind the scenes
How does a computer algebra system compute the determinant of a large matrix?
Definitely not by expansion in minors, nor by using the explicit formula as a sum over permutations. The most reasonable generic method is Gaussian elimination, which for a matrix of size n requires arithmetic operations.
For very large matrices (say n > 50000), there are some more advanced methods that do a bit better, e.g., Strassen's algorithm: https://en.wikipedia.org/wiki/Strassen_algorithm. We won't be encountering such methods here.
However, there is a big difference between doing linear algebra with floating-point real numbers (as is typical in MATLAB) versus doing it with exact numbers like integers or rational numbers. Let me illustrate this now.
First, though, an example to demonstrate what Sage is capable of.
The idea behind this calculation is multimodular echelon form. This uses some ideas from elementary number theory in a way you might not have anticipated.
Imagine with me what it would be like to do Gaussian elimination to put the following matrix $$ into reduced row Echelon form:
Then clear the second column:
And it gets bad...
There is a better way, which seems way more complicated but:
avoids "coefficient explosion"
enables many other nice optimizations: parallelization, working mod p, asymptotically fast matrix multiplication, etc.
The basic idea:
Choose some random prime .
Reduce our matrix modulo a prime number to get .
Compute the reduced row echelon form of .
(*) Lift the result back to the rational numbers to get the right answer.
This is potentially better, since the numbers all stay small, instead of getting massive/painful.
Notice -- the numbers aren't exploding. (Please imagine doing all this with a 200x201 matrix, say!)
But... how do you get from B.rref() to A.rref()???
Obviously, there isn't enough information: but 236 mod 389 doesn't tell you ...
There is an algorithm called rational reconstruction, which is based on Euclidean GCD:
Still doesn't work. But if we replace 389 by a bigger prime, it works:
Multimodular!
However, if the matrix is large, the entries are large, and we have to work modulo an enormous prime with thousands of digits. NO.
Instead, work modulo several small primes (possibly at once, in parallel). Small primes are easier and faster -- they fit in a machine word.
Use the Chinese Remainder Theorem to combine the rref modulo a bunch of small primes to get a matrix with entries modulo .
Use rational reconstruction on that (it works modulo for any integer ).
And... realize that echelon modulo can be done via several matrix multiplications! (Permute the matrix and work with blocks...)
And... that matrix multiplication is way faster that for big ... https://en.wikipedia.org/wiki/Strassen_algorithm again.