"""The smallest true instance: Strassen's seven multiplications for a 2 x 2 product. No data file: the scheme is Strassen's, written out as the three factor matrices of the rank-7 decomposition of the tensor <2,2,2>. The figure expands the decomposition and compares it with the matrix multiplication tensor, the exact check the README describes, before drawing it: each column is one multiplication, the combination of A's entries above the combination of B's entries, filled where the entry is used with a plus, sand with a bar where with a minus; the dashed eighth slot is the multiplication the schoolbook method needs and the scheme saves. """ from pathlib import Path import kit # Strassen 1969: M1..M7 as (coefficients on A11,A12,A21,A22; coefficients on B11,B12,B21,B22; where each M lands in C) 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)] 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)] 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)] # the matrix multiplication tensor <2,2,2>: C[i][k] = sum_j A[i][j] B[j][k] 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)} got = {} for u, v, w in zip(U, V, W): for a in range(4): for b in range(4): for c in range(4): if u[a] * v[b] * w[c]: got[(a, b, c)] = got.get((a, b, c), 0) + u[a] * v[b] * w[c] assert {k: n for k, n in got.items() if n} == want, 'not the 2 x 2 matrix multiplication tensor' assert len(U) == 7 CELL, GAP, PAIR, COL = 28, 4, 26, 18 gw = 2 * CELL + GAP x0 = (kit.W - 8 * gw - 7 * COL) / 2 # seven products and the empty slot of the eighth they save y0 = 42 def times(cx): # the multiplication sign between a column's two combinations mx, my = cx + gw / 2, y0 + gw + PAIR / 2 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) body = '' for r in range(7): cx = x0 + r * (gw + COL) for which, coef in ((0, U[r]), (1, V[r])): cy = y0 + which * (gw + PAIR) for e, co in enumerate(coef): x, y = cx + (e % 2) * (CELL + GAP), cy + (e // 2) * (CELL + GAP) if co: # an entry this product uses, with a plus or a minus; minus also gets a bar, not colour alone body += (f' 0 else "sand"}" x="{kit.f1(x)}" y="{kit.f1(y)}" ' f'width="{CELL}" height="{CELL}" rx="5"/>') if co < 0: body += kit.line(x + 7, y + CELL / 2, x + CELL - 7, y + CELL / 2, 'ring', 4, ' style="stroke-width:4"') else: # an entry it leaves out body += (f'') body += times(cx) # the eighth multiplication the schoolbook method needs and Strassen's scheme does not cx = x0 + 7 * (gw + COL) for which in (0, 1): body += (f'') body += times(cx) label = f'2 x 2 in {len(U)} multiplications, not 8 ยท Strassen 1969' kit.write(__file__, 'computer-science', body, label, "Strassen's seven products for a 2 x 2 matrix product: each column multiplies one combination of the " "first matrix's entries by one combination of the second's") print(label)