Salta al contenuto
Note per Studenti Matrix Multiplication - Iterative and Recursive Block Product Report

Matrix Multiplication - Iterative and Recursive Block Product

In questa pagina 3

We work with square n×nn\times n matrices with complex entries (the rectangular case is analogous). For block proofs assume n=2kn=2^k, k≥0k\ge0, so every matrix splits into four n2×n2\frac n2\times\frac n2 quadrants down to 1×11\times1 matrices.

Cost model. Unit cost for every arithmetic operation between two scalars (sum, difference, product, division). Assignments, copies and index computations cost 00. Complexity is expressed in nn or in N=n2N=n^2 (number of elements): always declare the size metric. T(n)=Θ(n2)T(n)=\Theta(n^2) is linear in NN.

Notation: A=[aij]1≤i,j≤nA=[a_{ij}]_{1\le i,j\le n}. Input preconditions (here: same dimensions) are static constraints.

Sum

cij=aij+bijc_{ij}=a_{ij}+b_{ij}:

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 C

Each for becomes a summation with the same variable: TSUM(n)=∑i=1n∑j=1n1=n2=Θ(n2)T_{SUM}(n)=\sum_{i=1}^n\sum_{j=1}^n1=n^2=\Theta(n^2). Any sum algorithm needs n2n^2 distinct sums in the worst case (each cijc_{ij} depends on its own pair of inputs), so n2n^2 is a lower bound too: the definition-based algorithm is optimal. Same for subtraction.

Iterative row-by-column product

cij=∑k=1naikbkjc_{ij}=\sum_{k=1}^na_{ik}b_{kj}.

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 C

Start from the first product (not from 00) to save one operation. For each (i,j)(i,j): 11 product, then 22 operations for each k=2..nk=2..n: T(n)=∑i=1n∑j=1n(1+2(n−1))=n2(2n−1)=2n3−n2.T(n)=\sum_{i=1}^n\sum_{j=1}^n\bigl(1+2(n-1)\bigr)=n^2(2n-1)=2n^3-n^2. That is n3n^3 products and n3−n2n^3-n^2 sums; TMUL=Θ(n3)=Θ(NN)T_{MUL}=\Theta(n^3)=\Theta(N\sqrt N). Example n=2n=2: (1234)(5678)=(19224350)\begin{pmatrix}1&2\\3&4\end{pmatrix}\begin{pmatrix}5&6\\7&8\end{pmatrix}=\begin{pmatrix}19&22\\43&50\end{pmatrix} with 88 products and 44 sums =12=2⋅23−22=12=2\cdot2^3-2^2.

Recursive block product

For n=2kn=2^k write A=(A11A12A21A22),B=(B11B12B21B22),Ahk,Bhk∈Cn2×n2.A=\begin{pmatrix}A_{11}&A_{12}\\A_{21}&A_{22}\end{pmatrix},\qquad B=\begin{pmatrix}B_{11}&B_{12}\\B_{21}&B_{22}\end{pmatrix},\qquad A_{hk},B_{hk}\in\mathbb C^{\frac n2\times\frac n2}.

Theorem (fractal property). The quadrants of C=ABC=AB are given by the same row-by-column formula of 2×22\times2 matrices with quadrants in place of scalars: Cij=Ai1B1j+Ai2B2jC_{ij}=A_{i1}B_{1j}+A_{i2}B_{2j}.

Proof for C11C_{11} (the others are identical). For 1≤i,j≤n21\le i,j\le\frac n2, (C11)ij=cij(C_{11})_{ij}=c_{ij}. Split the sum at k=n2k=\frac n2 and shift the second part with k=h+n2k=h+\frac n2, 1≤h≤n21\le h\le\frac n2: cij=∑k=1n/2aikbkj+∑h=1n/2ai,h+n/2 bh+n/2, j=∑k=1n/2(A11)ik(B11)kj+∑h=1n/2(A12)ih(B21)hj=(A11B11+A12B21)ij. ■c_{ij}=\sum_{k=1}^{n/2}a_{ik}b_{kj}+\sum_{h=1}^{n/2}a_{i,h+n/2}\,b_{h+n/2,\,j}=\sum_{k=1}^{n/2}(A_{11})_{ik}(B_{11})_{kj}+\sum_{h=1}^{n/2}(A_{12})_{ih}(B_{21})_{hj}=(A_{11}B_{11}+A_{12}B_{21})_{ij}.\ \blacksquare

So an n×nn\times n product reduces to 8 products and 4 sums of n2×n2\frac n2\times\frac n2 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 C

The quadrant split is a meta-command; in real code one passes indices of the submatrix without copying. Cost: 88 calls on size n2\frac n2 plus 44 sums of (n2)2(\frac n2)^2 each, i.e. work n2n^2: T(n)={1n=18 T(n2)+n2n>1.T(n)=\begin{cases}1&n=1\\8\,T(\frac n2)+n^2&n>1.\end{cases}

Master Theorem. a=8a=8, b=2b=2, threshold nlog⁡28=n3n^{\log_28}=n^3; w=n2=O(n3−ε)w=n^2=O(n^{3-\varepsilon}), ε=1\varepsilon=1: 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 →, T=Θ(n3)T=\Theta(n^3). The leaves (n3n^3, one per triple (i,j,k)(i,j,k)) dominate: arity 88 is too high. No improvement over the iterative algorithm.

Exact solution. T(n)=2n3−n2T(n)=2n^3-n^2: RMUL performs exactly the same operations as MUL in a different order.

Example n=4n=4: level 00: 44 sums 2×22\times2, 1616 operations; level 11: 88 calls, each with 44 scalar sums, 3232; level 22: 6464 products. Total 112=2⋅43−42112=2\cdot4^3-4^2. For n=8n=8: 960960 operations (512512 products, 448448 sums).

To beat n3n^3 the number of recursive calls must go below 88, 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

Teoria collegata