24.13 Fast Matrix Multiplication
Every algorithm in this chapter is built out of
matrix products, and every cost we have counted has been
counted against them. Section 12 broke one such count:
the Fourier matrix is applied in about
24.13.1. What a fast algorithm would be
Count the operations that Definition 0.9.9
prescribes. The product
Before asking whether it can be, we need to say what
“it” is. A claim that some procedure multiplies matrices
in fewer than
A program is a list of arithmetic instructions, each using the inputs or earlier results, and its cost is the length of the list.
Fix a field
- (i) one of the
indeterminates, or a constant of (an input step), or - (ii)
, , or for some (an arithmetic step).
Each
In words: input steps are free, because they only
name the data; the three arithmetic operations each cost
Two examples fix the definition. For
The exponent of matrix multiplication is the smallest growth rate a family of such programs can have.
The exponent of matrix
multiplication over
The infimum is over all real
Proof. Let
Now let
So
Warning. The exponent is an
asymptotic statement and says nothing about any
particular size.
24.13.2. Seven products instead of eight
The classical program for
Let
Idea. There is no derivation to
reproduce: the seven products were found by Strassen,
and what a proof can do is check them. What deserves
watching during the check is which rules it
uses. Every step expands a product of sums by
distributivity and then cancels equal terms. No two
blocks are ever interchanged, and no block is ever
inverted or divided by — only the ring operations of
Proof. Expand the seven products by
distributivity, keeping every factor in its place:
Two features of that proof matter more than the
result. First, it used only addition, subtraction,
multiplication, and their laws in
Multiply
Solution. The seven products are
Warning. The seven products
are not seven of the eight classical ones. Not
one of
Quick check. Does Theorem 24.13.2 still hold
over
Solution. Yes. The field
24.13.3. The recursion, and its exact cost
Apply Theorem 24.13.2 with
Let
Idea. Two counts and one
recurrence. Count the block additions in Theorem 24.13.2: ten to
build the operands of the seven products, eight to
assemble the four blocks of the answer, eighteen in all,
each of them
Proof. Step 1: the
recurrence. Let
Step 2: the solution. We show
Step 3: reading it in
Finally, the recursion is a straight-line program in
the sense of Definition 24.13.1: it
uses only additions, subtractions, and multiplications,
and by Theorem 24.13.2 it
produces every entry of
The number
Remark. The recursion is not a way
to make
24.13.4. What is known about the exponent, and what is not
Theorem 24.13.3 gives
Strassen’s bound was the first below
Two honest caveats belong with those numbers.
- The bounds below
are obtained by analyzing families of recursive constructions, not by exhibiting seven-product identities; the algorithms they yield carry constants so large that at every size that arises in practice they are slower than the recipe. This is a known feature of the constructions, and it is recorded here, not proved. may depend on the field , and the definition above was made field by field for that reason. The bounds just quoted, and Strassen’s, hold over every field.
Remark. The name bilinear
complexity belongs to a related quantity: the
least number of multiplications (as opposed to
all arithmetic operations) needed to compute a bilinear
map, which for the
24.13.5. Accuracy: a norm bound, but not an entrywise one
Everything so far was exact algebra. In the floating-point model of Definition 24.1.1 the two algorithms are not interchangeable, and the difference is sharper than “one is less accurate”.
Start with the classical recipe, which inherits
Section 1’s analysis directly. Write
Let
Proof. Fix
The bound is entrywise, and that is
its strength: the error in the
Strassen’s algorithm satisfies a weaker bound, in
which the whole of
Let
Idea. Each
Proof. Sizes. Each
factor of
Rounding. Fix
Assembly. Take
A bound of this shape survives the recursion, with a
constant that grows with
Work in a model as in Definition 24.1.1, and
suppose the set
Idea. Choose the data so that the
Proof. Write
Compute the four quantities that Theorem 24.13.2 combines
into the
Assemble
Finally
The constant
They are not vacuous either, and the fourth one fixes
the scale of
Work in binary arithmetic with
Solution. The hypotheses.
A number is representable when its significand fits in
The two answers. By Theorem 24.13.6,
Strassen’s algorithm returns
The other three entries of the Strassen product also
come out exactly right here, and the error
Warning. “No entrywise
bound” is not “unstable”. Proposition 24.13.5 is a
genuine bound, and for a matrix whose entries are all of
comparable size it says as much as Proposition 24.13.4 does.
The two part company only when the entries of
24.13.6. Whether to use it
The operation count and the error analysis pull in
opposite directions, and the honest summary is a list of
exact numbers rather than a verdict. Suppose the
recursion is stopped at a base size
So on operation count alone the trade is favorable at sizes that are not large. Three things keep the classical algorithm the one in ordinary use at small and moderate sizes, and none of them is visible in an operation count.
- Additions are not free in the way the count
assumes. The seven products must be stored, and
the eighteen block sums read and write whole blocks that
the classical recipe never touches. An operation count
charges
for an addition and for a multiplication and charges nothing for moving a number; on a real machine the movement is often the expensive part, and the recursion moves much more data than it saves in arithmetic. - The error bound is weaker, and it is weaker in a way that matters exactly when it is not expected — see Example (A small entry lost entirely).
- The sizes have to cooperate. The recursion wants even dimensions at every level; an odd size must be padded or handled specially, and the bookkeeping erodes the saving at precisely the moderate sizes where it is thinnest.
The conclusion is not that Theorem 24.13.2 is a
curiosity. It is used, at large sizes, with a classical
base case and with the accuracy caveat understood. What
it is not is a replacement for a definition:
24.13.7. Summary and transfer
Chapter 21 closed Part V with a complaint and a promise: “every proof here is an existence or a comparison, and not one of them is an algorithm. That is the subject of Chapter 24.” Thirteen sections later, the promise is paid, and what “compute” turned out to mean can be said in one sentence: a computed answer is the exact answer to a nearby question, and how near depends on the algorithm, while how much that nearness costs depends on the problem.
The chapter’s one new assumption was the floating-point model of Definition 24.1.1, a description of a machine stated as a definition. Everything else was proved from it and from the linear algebra of Chapters 0 to 21.
- What a computed answer means. Section 1 stated the model, proved Lemma 24.1.1 and Theorem 24.1.2, and gave the words the rest of the chapter needs: Definition 24.1.3, Definition 24.1.2, and the rule Theorem 24.1.3, that the forward error is at most the condition number times the backward error.
- Solving a system. Section 2 read elimination as a sequence of Schur complements (Theorem 24.2.1), counted it (Proposition 24.2.3), and proved backward error analyses in the cases it could reach: Theorem 24.2.5 outright, and Theorem 24.2.6 for the ordering it displays, the same bound for Chapter 3’s ordering being stated with credit. With it came the growth factor Definition 24.2.1, the bound Theorem 24.2.8 — attained, by Example (A matrix that really grows) — and the pivot-free safety of Cholesky (Theorem 24.2.9).
- Least squares. Section 3 proved
that the sensitivity of the least-squares solution
involves the residual as well as
(Theorem 24.3.1), that the normal equations lose twice the digits (Theorem 24.3.2), and that a reflection does not (Theorem 24.3.3) — that last for one reflection applied to a matrix, the accumulation over the whole factorization being stated with credit. - Eigenvalues. Section 4 built the power method (Theorem 24.4.1), inverse iteration (Theorem 24.4.3), and Rayleigh quotient iteration (Theorem 24.4.4); Section 5 identified the QR algorithm with orthogonal iteration (Theorem 24.5.2), proved its convergence (Theorem 24.5.3), and explained the Hessenberg reduction (Proposition 24.5.4) and the shifts.
- Iterating in a subspace. Section 6 built the Krylov space (Definition 24.6.1), the Arnoldi process (Theorem 24.6.2), and its Hermitian three-term form (Theorem 24.6.3), and showed that the Ritz values (Definition 24.6.2) interlace (Theorem 24.6.4) and carry Chapter 20’s residual bounds. Section 7 minimized over the same space: Theorem 24.7.4, the Chebyshev bound Lemma 24.7.6, and the square root in Theorem 24.7.7.
- One piece of pure mathematics. Section 8 showed that an algorithm and a differential equation can be the same object: the Lax pair Definition 24.8.1, the isospectral theorem Theorem 24.8.1, and the Toda flow’s relation to the QR algorithm (Theorem 24.8.5).
- Applications. Section 9 turned a mass-and-spring system into a generalized eigenproblem (Theorem 24.9.2); Section 10 proved that the best-fitting affine subspace is spanned by the leading singular vectors of the centered data matrix (Theorem 24.10.2) and turned “variance explained” into a theorem (Theorem 24.10.4); Section 11 built the graph Laplacian (Definition 24.11.1), identified its kernel (Theorem 24.11.2), introduced the Fiedler value (Definition 24.11.2) with the minimum it solves (Proposition 24.11.4) and the partition the signs of a Fiedler vector give, and proved the matrix-tree theorem (Theorem 24.11.8).
- Speed. Section 12 factored the
Fourier matrix (Theorem 24.12.1) and
solved the resulting recurrence (Theorem 24.12.2); this
section factored the two-by-two product (Theorem 24.13.2), solved
the resulting recurrence (Theorem 24.13.3), proved
the one lower bound on
this book has (Proposition 24.13.1), and showed what the speed costs (Theorem 24.13.6).
Four moves carried the chapter.
- Conditioning belongs to the problem, stability to the algorithm, and the two multiply. Chapter 16 §08 stated the distinction and could not use it; Theorem 24.1.3 makes it a theorem, and every accuracy claim in the chapter is an instance. Transfer: when an answer is wrong, ask first whether any method could have done better. If the condition number says no, do not change the algorithm — change the problem.
- Measure an answer by a change in the data that makes it exact. A forward error is usually unknowable and a backward error is usually computable, so prove the backward statement and let Theorem 24.1.3 supply the rest (§§01, 02, 03). Transfer: when a quantity you want to bound is out of reach, look for a perturbation of the input that the computed output answers exactly; bounding that is often elementary.
- Every eigenvalue algorithm iterates, because no finite one can exist. Chapter 11 §08 gave the reason — a finite procedure using arithmetic and radicals would solve the general quintic — and Sections 4 to 7 are what is left once that door is shut: a sequence of subspaces, each cheap to extend, each carrying a certified error bar from Chapter 20. Transfer: when exact solution is impossible in principle, the design question becomes what to iterate in; the Krylov space is the answer whenever the only affordable operation is one matrix–vector product.
- A fast algorithm is a factorization. The Fourier matrix factors into sparse pieces and the two-by-two product factors into seven multiplications; in both cases the cost follows by solving a recurrence exactly, and in both cases the underlying object is unchanged (§§12, 13). Transfer: to make an operation fast, do not look for a shortcut in the arithmetic. Look for a factorization of the operation itself, and count the recursive calls against the size reduction.
The chapter paid the following promises, in the words in which they were made.
| The promise | Made in | Paid by |
|---|---|---|
| “whether a given method keeps that promise is stability, and it is Chapter 24’s subject” | Chapter 16 §08 | §01 |
| “Chapter 24 draws the distinction properly, defines backward stability, and proves which of the algorithms in this book have it” | Chapter 16 §08 | §01, §02, §03 |
| “An error analysis of this kind, which measures an answer by a change in the data that makes it exact, is a backward error analysis. Chapter 24 builds on it.” | Chapter 20 §04 | §01 |
| “Chapter 24 makes this precise” (the |
Chapter 3 §06 | §02 |
| “Chapter 24 reads Gaussian elimination itself as a
sequence of Schur complements of |
Chapter 8 §03 | §02 |
| “Cholesky does about half the arithmetic of a general LU factorization … it needs no pivoting” | Chapter 13 §02 | §02 |
| “Three things are not proved here, and all three belong to Chapter 24” (the second of them being that nothing in that chapter is about arithmetic) | Chapter 16 §08 | §01, §02, §03 |
| “The conditioning of the least-squares problem
itself is not |
Chapter 16 §08 | §03 |
| “Chapter 24 makes ‘sensitivity’ precise and proves the comparison” | Chapter 11 §08, quoted again in Chapter 16 §08 | §03 |
| “a stable implementation of the QR route loses about
|
Chapter 16 §08 | §03 |
| “Chapter 24 quantifies the loss” (the Householder sign choice) | Chapter 11 §08 | §03 |
| “Chapter 24 explains why it is the stable one” (reflections against classical Gram–Schmidt) | Chapter 11 §02 | §03 |
| “Chapter 24 builds an iteration on it” (the Rayleigh quotient) | Chapter 17 §01 and Chapter 20 §06 | §04 |
| “the iteration |
Chapter 19 §04 | §04 |
| “Chapter 24 studies it for general matrices, where the rate is governed by the ratio of the two largest eigenvalue moduli” | Chapter 19 §08 | §04 |
| “the power method, the algorithm that computes a Perron vector at the size of the web, belongs to Chapter 24” | Chapter 19 §09 | §04 |
| “Chapter 24 develops that iteration and explains why every implementation starts with the reduction proved here” | Chapter 11 §08 | §05 |
| “algorithms that compute eigenvalues, is the subject of Chapter 24” | Chapter 20 §11 | §04, §05, §06 |
| “Those steps need a probabilistic model, a reason to prefer squared error, and an argument that the sample answer says something about the population. Chapter 24 supplies them” | Chapter 13 §11 | §10, for the first two; the third is refused, below |
| “the graph Laplacian, returns in Chapter 24” | Chapter 3 §07 | §11 |
| the matrix-tree theorem, “which Chapter 24 completes” | Chapter 7 §07 | §11 |
| “Chapter 24 develops it, together with the cost accounting that makes ‘quickly’ precise” (the fast Fourier transform) | Chapter 12 §09 | §12 |
Some things the chapter did not do, and the chapter’s
index collects them in one list. Section 2 proved the
backward error of a factorization for the ordering it
displays and stated with credit that Chapter 3’s
ordering satisfies the same bound; it also showed that
the growth bound for partial pivoting is far too large
to prove stability, and said so rather than tidying it.
Section 3 proved the backward error of a single
reflection and stated with credit both the accumulation
over a whole factorization and the rates at which
classical and modified Gram–Schmidt lose orthogonality.
Section 4 stated, and did not prove, that Rayleigh
quotient iteration converges at a quadratic rate, not a
cubic one, for a general diagonalizable matrix. Section
5 quoted the theorem of Abel and Ruffini, which is why
every eigenvalue method here iterates, and proved the
first column of the convergence theorem of the QR
algorithm while stating the general form with credit.
Section 6 stated the classical convergence estimate for
the extreme Ritz values and proved the clean special
case alone, and described the loss of orthogonality in
Lanczos without analyzing it. Section 8 derived the
continuous Toda statement only after granting that
Nothing in this book is proved from any of them. Each is quoted exactly where a reader would otherwise expect a proof, and every theorem in the chapter either proves its own case or carries what it needs as an explicit hypothesis; since all of them enter the book here, no earlier chapter can rest on them either. The one place a quoted result is used at all is Exercise (C1: Would twenty-three products for three-by-three beat Strassen?) (b), where Laderman’s identity is handed to the reader as a hypothesis and the deduction from it is the reader’s own. Chapter 21’s quoted fact (A7) was not used anywhere in this chapter, and no analysis beyond Chapter 16’s (A1)–(A6) was imported.
The book’s account of computation ends here. Twenty chapters built objects and proved things about them; this one asked what it costs to produce those objects from data of finite accuracy, and found that the answer splits cleanly in two. How much accuracy the data deserve is a question about the problem, answered by a condition number. How much of that the algorithm delivers is a question about the algorithm, answered by a backward error. Everything in this chapter — the pivoting, the reflections, the shifts, the Krylov spaces, the seven products — is an attempt to keep the second small when the first is out of our hands.
24.13.8. Exercises
A. Check your understanding
- (a) State how many
arithmetic operations Definition 0.9.9 uses
for the product of two
matrices, separating multiplications from additions. - (b) Define the
exponent
, and state what is proved about it in this section. - (c) True or false:
Theorem 24.13.2 shows that
. Justify your answer. - (d) Which properties
of
matrices does the proof of Theorem 24.13.2 use? Say what would go wrong if the identity had needed two blocks to commute. - (e) State the difference between the error bound of Proposition 24.13.4 and that of Proposition 24.13.5, and say for which matrices the difference matters.
Solution.
(a)
multiplications and additions, operations in all: each of the entries is a sum of products, needing multiplications and additions.(b)
, with the arithmetic cost of Definition 24.13.1. Proved here: (Proposition 24.13.1) and (Theorem 24.13.3). Everything else quoted about is stated with credit and not proved.(c) False, and for two reasons. The cost of Definition 24.13.1 counts additions as well as multiplications, and Theorem 24.13.2 uses
of them, so its cost at is , not ; indeed by the classical program, which is better. The number counts multiplications only.(d) Only the associativity and distributivity of matrix addition and multiplication and the existence of additive inverses, together with Theorem 8.1.1, which says that the blocks of
are the eight products combined as for a matrix. No block is ever inverted and no two are ever interchanged. If some cancellation had required, say, , it would fail for general blocks, which do not commute, and the identity would be true for numbers and false for matrices — so the recursion of Theorem 24.13.3 would be unavailable, which is the only reason the identity is of any interest.(e) Proposition 24.13.4 bounds the
error by , a quantity attached to that entry; Proposition 24.13.5 bounds every error by a multiple of , a quantity attached to the whole matrices. They agree in strength when all entries of are of comparable size, and differ when some entry of the product is far smaller than others, as in Example (A small entry lost entirely).
B. Practice
Let
Solution. With
The count: seven multiplications, and ten additions to form the operands together with eight to assemble the answer, so eighteen additions, twenty-five operations in all. The classical route uses eight multiplications and four additions, twelve operations. At this size the exchange is a loss.
Let
Solution. The blocks are
Let
- (a) How many
multiplications does the recursion of Theorem 24.13.3 perform,
as a function of
? How many does the classical recipe perform? - (b) Using
, compute and for and . - (c) Hence say, for these two sizes, which recipe uses fewer operations.
Solution.
(a) Each level replaces one product of size
by seven of size , and a product of size is one multiplication, so the recursion performs multiplications. The classical recipe performs .(b) For
, that is : , while . For , that is : , while .(c) At both sizes the classical recipe wins, by a factor of about
at and about at . The ratio is shrinking, and it crosses between and ; recursing all the way to scalars is a losing strategy at any size a person would compute by hand.
C. Going deeper
Suppose a ring identity multiplies two
- (a) Show that the
recursion performs
multiplications on matrices of size , and deduce that it gives . - (b) You are given
that such an identity exists with
; this is a result of Laderman, which is not proved here. Determine whether recursing on it beats Theorem 24.13.3. Justify your answer. - (c) Determine the
largest
for which such an identity would beat Theorem 24.13.3.
Hint: compare logarithms to a common base.
Solution.
(a) One level replaces a product of size
by products of size , so by induction on the recursion performs multiplications at size , the case being one multiplication. The additions at one level cost for a constant depending only on the identity, so the operation count satisfies with . Unrolling, where the last step uses and drops the . So with , since . Padding to a power of three as in Theorem 24.13.3 extends this to all , so .(b) No.
, while . Since , the exponent obtained from products at size is worse than Strassen’s.(c) We need
, that is . Now , so the condition is . Indeed , while . So an identity with multiplications would beat Strassen and one with would not.
Let
- (a) Compute
and exactly, and identify the entry of that is smallest as . - (b) Compute
exactly, and find which of them is largest as . - (c) Suppose
and are representable, and that the model rounds to and to , each time losing a trailing . Determine , and the least constant for which can hold at the position.
Hint: with
Solution.
(a)
, and since every entry of and is positive, . The smallest entry as is the entry .(b) With
and all other entries , As the largest in modulus is , with beside it; the other five all tend to .(c) The
entry is assembled as , and by (b) this is , as it must be. Two of the four terms have modulus close to , and the two occurrences of that survive the cancellation are exactly the two the model throws away: every operand that enters the entry — , , , and — are all representable, so each is formed exactly, and then by hypothesis and , while and are exact. Assembling left to right, so and the error is . Since by (a), an entrywise bound there reads , so the least admissible constant is , exactly as in Theorem 24.13.6. The mechanism is the same: a quantity of size is carried inside numbers of size , where the model cannot see it.
Suppose someone proposes an identity that multiplies
- (a) Write down the
recurrence for its operation count with
, and solve it exactly for . - (b) Hence determine the exponent it gives, and compare with Theorem 24.13.3.
- (c) Explain in one sentence what part of an identity the exponent depends on.
Solution.
(a) Two block additions of size
cost , so with . We claim . For this is . Assuming it for and taking , so , since and . (Check: , , .)(b) With
this is , so the exponent is . Saving additions changed the constant from to and nothing else; Theorem 24.13.3 changed the exponent from to .(c) Only the number of multiplications recurses, so only that number enters the exponent; the additions contribute to the constant.