How many multiplications do small matrix products need?
Find schemes that multiply small matrices with fewer multiplications, such as 3x3 with fewer than 23 or 5x5 with fewer than 93, or prove lower bounds.

| 1 | """The smallest true instance: Strassen's seven multiplications for a 2 x 2 product. |
| 2 | |
| 3 | No data file: the scheme is Strassen's, written out as the three factor matrices of the rank-7 decomposition of the |
| 4 | tensor <2,2,2>. The figure expands the decomposition and compares it with the matrix multiplication tensor, the exact |
| 5 | check the README describes, before drawing it: each column is one multiplication, the combination of A's entries |
| 6 | above the combination of B's entries, filled where the entry is used with a plus, sand with a bar where with a |
| 7 | minus; the dashed eighth slot is the multiplication the schoolbook method needs and the scheme saves. |
| 8 | """ |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import kit |
| 12 | |
| 13 | # Strassen 1969: M1..M7 as (coefficients on A11,A12,A21,A22; coefficients on B11,B12,B21,B22; where each M lands in C) |
| 14 | U = [(1, 0, 0, 1), (0, 0, 1, 1), (1, 0, 0, 0), (0, 0, 0, 1), (1, 1, 0, 0), (-1, 0, 1, 0), (0, 1, 0, -1)] |
| 15 | V = [(1, 0, 0, 1), (1, 0, 0, 0), (0, 1, 0, -1), (-1, 0, 1, 0), (0, 0, 0, 1), (1, 1, 0, 0), (0, 0, 1, 1)] |
| 16 | W = [(1, 0, 0, 1), (0, 0, 1, -1), (0, 1, 0, 1), (1, 0, 1, 0), (-1, 1, 0, 0), (0, 0, 0, 1), (1, 0, 0, 0)] |
| 17 | |
| 18 | # the matrix multiplication tensor <2,2,2>: C[i][k] = sum_j A[i][j] B[j][k] |
| 19 | want = {(i * 2 + j, j * 2 + k, i * 2 + k): 1 for i in range(2) for j in range(2) for k in range(2)} |
| 20 | got = {} |
| 21 | for u, v, w in zip(U, V, W): |
| 22 | for a in range(4): |
| 23 | for b in range(4): |
| 24 | for c in range(4): |
| 25 | if u[a] * v[b] * w[c]: |
| 26 | got[(a, b, c)] = got.get((a, b, c), 0) + u[a] * v[b] * w[c] |
| 27 | assert {k: n for k, n in got.items() if n} == want, 'not the 2 x 2 matrix multiplication tensor' |
| 28 | assert len(U) == 7 |
| 29 | |
| 30 | CELL, GAP, PAIR, COL = 28, 4, 26, 18 |
| 31 | gw = 2 * CELL + GAP |
| 32 | x0 = (kit.W - 8 * gw - 7 * COL) / 2 # seven products and the empty slot of the eighth they save |
| 33 | y0 = 42 |
| 34 | |
| 35 | |
| 36 | def times(cx): # the multiplication sign between a column's two combinations |
| 37 | mx, my = cx + gw / 2, y0 + gw + PAIR / 2 |
| 38 | return kit.line(mx - 5, my - 5, mx + 5, my + 5, 'faint-s', 3) + kit.line(mx - 5, my + 5, mx + 5, my - 5, 'faint-s', 3) |
| 39 | |
| 40 | |
| 41 | body = '' |
| 42 | for r in range(7): |
| 43 | cx = x0 + r * (gw + COL) |
| 44 | for which, coef in ((0, U[r]), (1, V[r])): |
| 45 | cy = y0 + which * (gw + PAIR) |
| 46 | for e, co in enumerate(coef): |
| 47 | x, y = cx + (e % 2) * (CELL + GAP), cy + (e // 2) * (CELL + GAP) |
| 48 | if co: # an entry this product uses, with a plus or a minus; minus also gets a bar, not colour alone |
| 49 | body += (f'<rect class="{"f" if co > 0 else "sand"}" x="{kit.f1(x)}" y="{kit.f1(y)}" ' |
| 50 | f'width="{CELL}" height="{CELL}" rx="5"/>') |
| 51 | if co < 0: |
| 52 | body += kit.line(x + 7, y + CELL / 2, x + CELL - 7, y + CELL / 2, 'ring', 4, ' style="stroke-width:4"') |
| 53 | else: # an entry it leaves out |
| 54 | body += (f'<rect class="faint" x="{kit.f1(x + CELL / 3)}" y="{kit.f1(y + CELL / 3)}" ' |
| 55 | f'width="{kit.f1(CELL / 3)}" height="{kit.f1(CELL / 3)}" rx="2" fill-opacity=".45"/>') |
| 56 | body += times(cx) |
| 57 | # the eighth multiplication the schoolbook method needs and Strassen's scheme does not |
| 58 | cx = x0 + 7 * (gw + COL) |
| 59 | for which in (0, 1): |
| 60 | body += (f'<rect class="faint-s" x="{kit.f1(cx + 1)}" y="{kit.f1(y0 + which * (gw + PAIR) + 1)}" ' |
| 61 | f'width="{gw - 2}" height="{gw - 2}" rx="6" stroke-width="2.5" stroke-dasharray="5 6"/>') |
| 62 | body += times(cx) |
| 63 | label = f'2 x 2 in {len(U)} multiplications, not 8 · Strassen 1969' |
| 64 | kit.write(__file__, 'computer-science', body, label, |
| 65 | "Strassen's seven products for a 2 x 2 matrix product: each column multiplies one combination of the " |
| 66 | "first matrix's entries by one combination of the second's") |
| 67 | print(label) |