Algorithm Search⌘K

Proofs: Laderman's algorithm

These are the human proofs of Laderman's algorithm, at full length.

Laderman's 23 products give the 3×3 matrix product full

This proof shows: correctness, termination.

Let RR be a ring: an additive abelian group with an associative multiplication that distributes over addition on both sides. Commutativity is not used. Every product below is written with an entry of AA on the left and an entry of BB on the right, and the only rules used are distributivity, x(−y)=−(xy)=(−x)yx(-y) = -(xy) = (-x)y, and the laws of the additive group. So every identity holds when the entries are blocks of matrices over RR of matching shapes.

The nine identities

Write cik=ai1b1k+ai2b2k+ai3b3kc_{ik} = a_{i1}b_{1k} + a_{i2}b_{2k} + a_{i3}b_{3k} for the target, and M1,…,M23M_1, \ldots, M_{23} for the products of the table on the main page.

Four groups. By distributivity,

M7+M8=(−a11+a31)((b11−b13+b23)+(b13−b23))+a32(b11−b13+b23)=(−a11+a31)b11+a32(b11−b13+b23),M_7 + M_8 = (-a_{11}+a_{31})\big((b_{11}-b_{13}+b_{23}) + (b_{13}-b_{23})\big) + a_{32}(b_{11}-b_{13}+b_{23}) = (-a_{11}+a_{31})b_{11} + a_{32}(b_{11}-b_{13}+b_{23}),

so with M6=a11b11M_6 = a_{11}b_{11},

G1:=M6+M7+M8=a31b11+a32b11−a32b13+a32b23.G_1 := M_6+M_7+M_8 = a_{31}b_{11} + a_{32}b_{11} - a_{32}b_{13} + a_{32}b_{23}.

In the same way, splitting the left factor of M4M_4 as (−a11+a21)+a22(-a_{11}+a_{21}) + a_{22} and of M16M_{16} and M12M_{12} as (−a13+a23)+a22(-a_{13}+a_{23}) + a_{22} and (−a13+a33)+a32(-a_{13}+a_{33}) + a_{32},

G2:=M14+M16+M17=a23b31+a22b23+a22b31−a22b33,G3:=M12+M13+M14=a33b31+a32b22+a32b31−a32b32,G4:=M2+M4+M6=a21b11+a22b11−a22b12+a22b22.\begin{aligned} G_2 &:= M_{14}+M_{16}+M_{17} = a_{23}b_{31} + a_{22}b_{23} + a_{22}b_{31} - a_{22}b_{33}, \\ G_3 &:= M_{12}+M_{13}+M_{14} = a_{33}b_{31} + a_{32}b_{22} + a_{32}b_{31} - a_{32}b_{32}, \\ G_4 &:= M_2+M_4+M_6 = a_{21}b_{11} + a_{22}b_{11} - a_{22}b_{12} + a_{22}b_{22}. \end{aligned}

For G4G_4: M2+M4=(a11−a21)((−b12+b22)−(b11−b12+b22))+a22(b11−b12+b22)=−(a11−a21)b11+a22(b11−b12+b22)M_2 + M_4 = (a_{11}-a_{21})\big((-b_{12}+b_{22}) - (b_{11}-b_{12}+b_{22})\big) + a_{22}(b_{11}-b_{12}+b_{22}) = -(a_{11}-a_{21})b_{11} + a_{22}(b_{11}-b_{12}+b_{22}), and adding M6M_6 gives the line. G2G_2 and G3G_3 are the same computation with the indices renamed.

Completed groups. Expanding M9=(a31+a32)(−b11+b13)M_9 = (a_{31}+a_{32})(-b_{11}+b_{13}), M18=(a22+a23)(−b31+b33)M_{18} = (a_{22}+a_{23})(-b_{31}+b_{33}), M15=(a32+a33)(−b31+b32)M_{15} = (a_{32}+a_{33})(-b_{31}+b_{32}) and M5=(a21+a22)(−b11+b12)M_5 = (a_{21}+a_{22})(-b_{11}+b_{12}) and adding,

H1:=G1+M9=a31b13+a32b23,H2:=G2+M18=a22b23+a23b33,H3:=G3+M15=a32b22+a33b32,H4:=G4+M5=a21b12+a22b22.\begin{aligned} H_1 &:= G_1 + M_9 = a_{31}b_{13} + a_{32}b_{23}, & H_2 &:= G_2 + M_{18} = a_{22}b_{23} + a_{23}b_{33}, \\ H_3 &:= G_3 + M_{15} = a_{32}b_{22} + a_{33}b_{32}, & H_4 &:= G_4 + M_5 = a_{21}b_{12} + a_{22}b_{22}. \end{aligned}

Entries with five products. Using M23=a33b33M_{23} = a_{33}b_{33}, M21=a21b13M_{21} = a_{21}b_{13}, M22=a31b12M_{22} = a_{31}b_{12} and M20=a23b32M_{20} = a_{23}b_{32}:

c33=M6+M7+M8+M9+M23=H1+a33b33=a31b13+a32b23+a33b33,c23=M14+M16+M17+M18+M21=H2+a21b13,c32=M12+M13+M14+M15+M22=H3+a31b12,c22=M2+M4+M5+M6+M20=H4+a23b32.\begin{aligned} c_{33} &= M_6+M_7+M_8+M_9+M_{23} = H_1 + a_{33}b_{33} = a_{31}b_{13}+a_{32}b_{23}+a_{33}b_{33}, \\ c_{23} &= M_{14}+M_{16}+M_{17}+M_{18}+M_{21} = H_2 + a_{21}b_{13}, \\ c_{32} &= M_{12}+M_{13}+M_{14}+M_{15}+M_{22} = H_3 + a_{31}b_{12}, \\ c_{22} &= M_2+M_4+M_5+M_6+M_{20} = H_4 + a_{23}b_{32}. \end{aligned}

Each right side is the wanted cikc_{ik}.

c21c_{21} and c31c_{31}. The long product is

M3=−a22b11+a22b12+a22b21−a22b22−a22b23−a22b31+a22b33.M_3 = -a_{22}b_{11} + a_{22}b_{12} + a_{22}b_{21} - a_{22}b_{22} - a_{22}b_{23} - a_{22}b_{31} + a_{22}b_{33}.

The table gives c21=M2+M3+M4+M6+M14+M16+M17=G4+M3+G2c_{21} = M_2+M_3+M_4+M_6+M_{14}+M_{16}+M_{17} = G_4 + M_3 + G_2. In this sum the terms a22b11a_{22}b_{11}, a22b12a_{22}b_{12}, a22b22a_{22}b_{22} from G4G_4 and a22b23a_{22}b_{23}, a22b31a_{22}b_{31}, a22b33a_{22}b_{33} from G2G_2 cancel with the six matching terms of M3M_3, and what remains is a21b11+a22b21+a23b31a_{21}b_{11} + a_{22}b_{21} + a_{23}b_{31}. In the same way, with

M11=−a32b11+a32b13+a32b21−a32b22−a32b23−a32b31+a32b32,M_{11} = -a_{32}b_{11} + a_{32}b_{13} + a_{32}b_{21} - a_{32}b_{22} - a_{32}b_{23} - a_{32}b_{31} + a_{32}b_{32},

c31=G1+M11+G3=a31b11+a32b21+a33b31c_{31} = G_1 + M_{11} + G_3 = a_{31}b_{11} + a_{32}b_{21} + a_{33}b_{31}.

c12c_{12} and c13c_{13}. The table gives c12=M1+M4+M5+M6+M12+M14+M15=H4−M2+H3−M13+M1c_{12} = M_1 + M_4 + M_5 + M_6 + M_{12} + M_{14} + M_{15} = H_4 - M_2 + H_3 - M_{13} + M_1. Here

−M2=a11b12−a11b22−a21b12+a21b22,−M13=−a13b22+a13b32+a33b22−a33b32,M1=a11b22+a12b22+a13b22−a21b22−a22b22−a32b22−a33b22.\begin{aligned} -M_2 &= a_{11}b_{12} - a_{11}b_{22} - a_{21}b_{12} + a_{21}b_{22}, \\ -M_{13} &= -a_{13}b_{22} + a_{13}b_{32} + a_{33}b_{22} - a_{33}b_{32}, \\ M_1 &= a_{11}b_{22} + a_{12}b_{22} + a_{13}b_{22} - a_{21}b_{22} - a_{22}b_{22} - a_{32}b_{22} - a_{33}b_{22}. \end{aligned}

Adding these to H4+H3=a21b12+a22b22+a32b22+a33b32H_4 + H_3 = a_{21}b_{12} + a_{22}b_{22} + a_{32}b_{22} + a_{33}b_{32}, every monomial cancels except a11b12+a12b22+a13b32=c12a_{11}b_{12} + a_{12}b_{22} + a_{13}b_{32} = c_{12}. In the same way c13=M6+M7+M9+M10+M14+M16+M18=H1−M8+H2−M17+M10c_{13} = M_6+M_7+M_9+M_{10}+M_{14}+M_{16}+M_{18} = H_1 - M_8 + H_2 - M_{17} + M_{10}, with

−M8=a11b13−a11b23−a31b13+a31b23,−M17=−a13b23+a13b33+a23b23−a23b33,M10=a11b23+a12b23+a13b23−a22b23−a23b23−a31b23−a32b23,\begin{aligned} -M_8 &= a_{11}b_{13} - a_{11}b_{23} - a_{31}b_{13} + a_{31}b_{23}, \\ -M_{17} &= -a_{13}b_{23} + a_{13}b_{33} + a_{23}b_{23} - a_{23}b_{33}, \\ M_{10} &= a_{11}b_{23} + a_{12}b_{23} + a_{13}b_{23} - a_{22}b_{23} - a_{23}b_{23} - a_{31}b_{23} - a_{32}b_{23}, \end{aligned}

and the sum with H1+H2=a31b13+a32b23+a22b23+a23b33H_1 + H_2 = a_{31}b_{13} + a_{32}b_{23} + a_{22}b_{23} + a_{23}b_{33} is a11b13+a12b23+a13b33=c13a_{11}b_{13} + a_{12}b_{23} + a_{13}b_{33} = c_{13}.

c11c_{11}. M6+M14+M19=a11b11+a13b31+a12b21M_6 + M_{14} + M_{19} = a_{11}b_{11} + a_{13}b_{31} + a_{12}b_{21}.

So all nine identities hold in every ring.

The recursion

Claim. For all n,m,p≥0n, m, p \ge 0 and n0≥1n_0 \ge 1, LADERMAN(A,B,n0)\mathrm{LADERMAN}(A, B, n_0) returns ABAB for A∈Rn×mA \in R^{n \times m} and B∈Rm×pB \in R^{m \times p}.

Proof by strong induction on s=n+m+ps = n + m + p. If min⁡(n,m,p)≤n0\min(n, m, p) \le n_0, line L1 returns the classical product, which is ABAB.

Otherwise n,m,p≥2n, m, p \ge 2. If some side is not a multiple of 33, line L2 pads AA to A′=(A000)A' = \begin{pmatrix} A & 0 \\ 0 & 0 \end{pmatrix} of size n′×m′n' \times m' and BB to B′=(B000)B' = \begin{pmatrix} B & 0 \\ 0 & 0 \end{pmatrix} of size m′×p′m' \times p', with each side rounded up to a multiple of 33. Then the top-left n×pn \times p block of A′B′A'B' is AB+0⋅0=ABAB + 0 \cdot 0 = AB. The padded call has all sides multiples of 33 and still has min⁡(n′,m′,p′)>n0\min(n', m', p') > n_0, so it does not reach line L2 again. It reaches line L3, and its correctness is the case below, which only uses the induction hypothesis on calls with smaller side sums.

If all sides are multiples of 33, line L3 splits AA and BB into 3×33 \times 3 grids of blocks of sizes h×kh \times k and k×qk \times q, with h=n/3h = n/3, k=m/3k = m/3, q=p/3q = p/3. By block multiplication, the block (i,k)(i, k) of ABAB is ∑jAijBjk\sum_j A_{ij} B_{jk}. The factors at line L4 are h×kh \times k and k×qk \times q, and h+k+q≤⌈n/3⌉+⌈m/3⌉+⌈p/3⌉<sh + k + q \le \lceil n/3 \rceil + \lceil m/3 \rceil + \lceil p/3 \rceil < s for the original sides (each ⌈x/3⌉<x\lceil x/3 \rceil < x for x≥2x \ge 2), so by the induction hypothesis each call returns the true block product MrM_r. Lines L5 and L6 then form the nine sums of the table. Block matrices of these shapes satisfy all the ring laws used in the first part (with h×kh \times k times k×qk \times q products), so the nine identities give Cik=∑jAijBjkC_{ik} = \sum_j A_{ij} B_{jk}, which is the block (i,k)(i, k) of ABAB. □\square

Termination

Each recursive call at line L4 has a smaller side sum than the call that made it, measured against the unpadded sides, and the padded call of line L2 goes to line L3 without padding again. So the side sum strictly decreases along every chain of calls, except for one padding step per level, and every chain ends at line L1. Since each side shrinks from xx to at most ⌈x/3⌉\lceil x/3 \rceil per level, the depth is O(log⁡max⁡(n,m,p))O(\log \max(n, m, p)).

Back to Laderman's algorithm