Short answer: Strassen’s algorithm multiplies two n by n matrices with seven recursive multiplications of n/2 by n/2 matrices instead of eight. A constant number of additions recombines those seven products into the four blocks of the result. The recurrence solves to O(n^{log₂ 7}), about O(n^2.807), against the classical O(n³). A cutoff to classical multiplication is required for any practical speedup. The algorithm is not asymptotically optimal. Later algorithms have smaller exponents and larger constants.
What the problem actually asks
The classical product of 2 by 2 block matrices forms eight products of the blocks. Strassen’s identities form seven. The algebra is rigid: each of the seven linear combinations of blocks has to match the formula, and the four output blocks are specific sums and differences of those products. Dropping a sign is enough to return a matrix that is not the product.
The split needs even order, or a pad of one row and column of zeros. The pad must not be left in the returned matrix.
What the algorithm guarantees
The result equals the classical product, over any ring in which 2 is not assumed invertible. Strassen’s identities use only additions and subtractions. Time is Θ(n^{log₂ 7}) arithmetic operations if the recursion runs to 1 by 1, and less wall time if it stops at a cutoff where the classical triple loop, with good cache behavior, wins. Extra memory is the temporary block sums. Numerical stability is weaker than a classical product of well-conditioned matrices, because subtractions cancel. For floating-point work that cancellation is a reason to prefer classical multiplication unless the size justifies the exponent.
The exponent log₂ 7 is about 2.807. It is not 2. This algorithm does not make matrix multiplication quadratic.
The working steps
- If n is below the cutoff, or n is 1, use the classical product and return.
- Pad to an even order if needed. Split each matrix into four n/2 blocks.
- Compute the seven products M1 through M7 from the standard linear combinations of blocks. One common set is M1 = (A11+A22)(B11+B22), M2 = (A21+A22)B11, M3 = A11(B12−B22), M4 = A22(B21−B11), M5 = (A11+A12)B22, M6 = (A21−A11)(B11+B12), M7 = (A12−A22)(B21+B22).
- The output blocks are C11 = M1+M4−M5+M7, C12 = M3+M5, C21 = M2+M4, C22 = M1−M2+M3+M6.
- Strip any padding. A sign error in this recombination is a wrong product, so test the cutoff case against the classical product on random matrices.
The seven-product identity is exact arithmetic. Floating-point cancellation is a separate defect and does not mean the identity is wrong. The practical crossover is often thousands of rows, and a library GEMM with a classical algorithm and good blocking can beat a naive Strassen well past the asymptotic prediction. Later exponent improvements are theoretical algorithms with still larger constants.
Approaches that look simpler and fail
| Approach | Why it fails when an eighth recursive product is kept for simplicity |
|---|---|
| Form all eight block products and quote the Strassen exponent | Eight half-size products are the classical recurrence, Θ(n³). The saving is specifically the reduction to seven. |
| Pad with zeros and return the padded matrix | The caller asked for the original shape. The extra row and column are zeros only in the pad, and the dimensions are wrong. |
| Ignore cancellation and use the algorithm for a poorly conditioned floating-point product | The mathematical product is right and the rounding error can be worse than the classical product. That is a reason to stay classical for numerical work. |
| Claim O(n²) matrix multiplication | log₂ 7 is greater than 2. No known practical algorithm multiplies dense matrices in quadratic time. |
Integer and polynomial products in the middle range use Karatsuba. Dense numerical products usually use a blocked classical GEMM. Strassen is the seven-product matrix algorithm with exponent log₂ 7.
A working example
This Python fragment multiplies 2×2 blocks with seven products instead of eight. The padding of an odd order is not needed here. The product of [[1, 2], [3, 4]] and [[5, 6], [7, 8]] is 19 22 / 43 50.
def add(A, B, sign=1):
return [[A[i][j] + sign * B[i][j] for j in range(len(A))] for i in range(len(A))]
def strassen(A, B):
n = len(A)
if n == 1:
return [[A[0][0] * B[0][0]]]
m = n // 2
def q(M, r, c):
return [row[c * m:(c + 1) * m] for row in M[r * m:(r + 1) * m]]
A11, A12, A21, A22 = q(A, 0, 0), q(A, 0, 1), q(A, 1, 0), q(A, 1, 1)
B11, B12, B21, B22 = q(B, 0, 0), q(B, 0, 1), q(B, 1, 0), q(B, 1, 1)
M1 = strassen(add(A11, A22), add(B11, B22))
M2 = strassen(add(A21, A22), B11)
M3 = strassen(A11, add(B12, B22, -1))
M4 = strassen(A22, add(B21, B11, -1))
M5 = strassen(add(A11, A12), B22)
M6 = strassen(add(A21, A11, -1), add(B11, B12))
M7 = strassen(add(A12, A22, -1), add(B21, B22))
C11 = add(add(add(M1, M4), M5, -1), M7)
C12 = add(M3, M5)
C21 = add(M2, M4)
C22 = add(add(add(M1, M3), M2, -1), M6)
out = [[0] * n for _ in range(n)]
for i in range(m):
for j in range(m):
out[i][j] = C11[i][j]
out[i][j + m] = C12[i][j]
out[i + m][j] = C21[i][j]
out[i + m][j + m] = C22[i][j]
return out
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = strassen(A, B)
assert C == [[19, 22], [43, 50]]
print(*C[0])
print(*C[1])
Where it is used
Strassen multiplication is used for large dense matrix products when the extra rounding is acceptable. Many GEMM kernels still use a tiled conventional product because the constant and the numerics win at practical sizes. The seven multiplies are the asymptotic improvement.
When you can drop this algorithm
Drop Strassen for small or floating-point matrices where a tuned classical kernel wins and cancellation matters. Keep it for exact arithmetic on large matrices, with a measured cutoff and a test against the classical product.
Why this stays published
We publish this so a search for Strassen’s algorithm returns the seven block products and the exponent log base 2 of 7. Strassen’s algorithm multiplies n by n matrices with seven recursive half-size products, in O(n^2.807) arithmetic operations.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
