Matrix Multiplication - Iterative and Recursive Block Product
In questa pagina 3
We work with square matrices with complex entries (the rectangular case is analogous). For block proofs assume , , so every matrix splits into four quadrants down to matrices.
Cost model. Unit cost for every arithmetic operation between two scalars (sum, difference, product, division). Assignments, copies and index computations cost . Complexity is expressed in or in (number of elements): always declare the size metric. is linear in .
Notation: . Input preconditions (here: same dimensions) are static constraints.
Sum
:
SUM(A, B)
n <- rows(A)
for i <- 1 to n do
for j <- 1 to n do
c[i][j] <- a[i][j] + b[i][j]
return CEach for becomes a summation with the same variable: . Any sum algorithm needs distinct sums in the worst case (each depends on its own pair of inputs), so is a lower bound too: the definition-based algorithm is optimal. Same for subtraction.
Iterative row-by-column product
.
MUL(A, B)
n <- rows(A)
for i <- 1 to n do
for j <- 1 to n do
c[i][j] <- a[i][1] * b[1][j]
for k <- 2 to n do
c[i][j] <- c[i][j] + a[i][k] * b[k][j]
return CStart from the first product (not from ) to save one operation. For each : product, then operations for each : That is products and sums; . Example : with products and sums .
Recursive block product
For write
Theorem (fractal property). The quadrants of are given by the same row-by-column formula of matrices with quadrants in place of scalars: .
Proof for (the others are identical). For , . Split the sum at and shift the second part with , :
So an product reduces to 8 products and 4 sums of matrices.
RMUL(A, B)
n <- rows(A)
if n = 1 then return [a11 * b11]
* A = [A11 A12; A21 A22], B = [B11 B12; B21 B22] *
for i <- 1 to 2 do
for j <- 1 to 2 do
Cij <- SUM(RMUL(Ai1, B1j), RMUL(Ai2, B2j))
return CThe quadrant split is a meta-command; in real code one passes indices of the submatrix without copying. Cost: calls on size plus sums of each, i.e. work :
Master Theorem. , , threshold ; , : Case 1 of the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →, . The leaves (, one per triple ) dominate: arity is too high. No improvement over the iterative algorithm.
Exact solution. : RMUL performs exactly the same operations as MUL in a different order.
- Induction: ; .
- Unfolding (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →): level has nodes of work ; leaves of cost :
Example : level : sums , operations; level : calls, each with scalar sums, ; level : products. Total . For : operations ( products, sums).
To beat the number of recursive calls must go below , accepting more work in the internal nodes: Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →. Matrices with extra structure can do even better: Structured Block Matrices - Reducing Recursive CallsFor recursively structured matrix families (ARB, SIR, RCA, supercirculant) the method is: count the free parameters (n, not n^2), write the block product, prove closure under sum, difference and product by induction, count the distinct block products, and reduce them with sums and differences (2 calls for SC and RCA, 3 for ARB and SIR). If the product leaves the family one cannot recurse on it, which is why one asks for a matrix-vector product instead.Structured Block Matrices - Reducing Recursive Calls →.
Versione ripasso
- Cost model: per scalar operation; assignments, copies, indices free. Size or : always declare which. Preconditions = static constraints.
- Sum: , . Optimal: distinct sums needed, so is also a lower bound.
- Iterative product: ; start from , sum for ( operations each): products, sums, . : operations, .
- Fractal property (): . Proof for : split at ; shift ; recognize and .
- RMUL: returns ; else
SUM(RMUL,RMUL): calls, sums of , work . - Master Theorem: , , threshold , : Case 1, (Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →). Leaves dominate: arity too high.
- Exact: (induction: ; unfolding: ). RMUL does exactly the same operations as MUL. : ; : .
- Moral: to beat reduce the calls below (Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →).
- Common mistakes: counting sums (they are : the first product of each is not added); counting quadrant copies; confusing size with ; believing recursive blocks are faster than iterative.