{"id":261,"date":"2026-11-13T12:36:13","date_gmt":"2026-11-13T04:36:13","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-strassen-multiplies-matrices-with-seven-products\/"},"modified":"2026-11-13T21:53:56","modified_gmt":"2026-11-13T13:53:56","slug":"how-strassen-multiplies-matrices-with-seven-products","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-strassen-multiplies-matrices-with-seven-products\/","title":{"rendered":"How Strassen multiplies matrices with seven products"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Strassen&#8217;s algorithm<\/strong> 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 <strong>O(n^{log\u2082 7})<\/strong>, about <strong>O(n^2.807)<\/strong>, against the classical <strong>O(n\u00b3)<\/strong>. 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The classical product of 2 by 2 block matrices forms eight products of the blocks. Strassen&#8217;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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The result equals the classical product, over any ring in which 2 is not assumed invertible. Strassen&#8217;s identities use only additions and subtractions. Time is \u0398(n^{log\u2082 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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The exponent log\u2082 7 is about 2.807. It is not 2. This algorithm does not make matrix multiplication quadratic.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>If n is below the cutoff, or n is 1, use the classical product and return.<\/li>\n<li>Pad to an even order if needed. Split each matrix into four n\/2 blocks.<\/li>\n<li>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\u2212B22), M4 = A22(B21\u2212B11), M5 = (A11+A12)B22, M6 = (A21\u2212A11)(B11+B12), M7 = (A12\u2212A22)(B21+B22).<\/li>\n<li>The output blocks are C11 = M1+M4\u2212M5+M7, C12 = M3+M5, C21 = M2+M4, C22 = M1\u2212M2+M3+M6.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when an eighth recursive product is kept for simplicity<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Form all eight block products and quote the Strassen exponent<\/td>\n<td>Eight half-size products are the classical recurrence, \u0398(n\u00b3). The saving is specifically the reduction to seven.<\/td>\n<\/tr>\n<tr>\n<td>Pad with zeros and return the padded matrix<\/td>\n<td>The caller asked for the original shape. The extra row and column are zeros only in the pad, and the dimensions are wrong.<\/td>\n<\/tr>\n<tr>\n<td>Ignore cancellation and use the algorithm for a poorly conditioned floating-point product<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Claim O(n\u00b2) matrix multiplication<\/td>\n<td>log\u2082 7 is greater than 2. No known practical algorithm multiplies dense matrices in quadratic time.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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\u2082 7.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment multiplies 2\u00d72 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 <code>19 22<\/code> \/ <code>43 50<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def add(A, B, sign=1):\n    return [[A[i][j] + sign * B[i][j] for j in range(len(A))] for i in range(len(A))]\n\ndef strassen(A, B):\n    n = len(A)\n    if n == 1:\n        return [[A[0][0] * B[0][0]]]\n    m = n \/\/ 2\n    def q(M, r, c):\n        return [row[c * m:(c + 1) * m] for row in M[r * m:(r + 1) * m]]\n    A11, A12, A21, A22 = q(A, 0, 0), q(A, 0, 1), q(A, 1, 0), q(A, 1, 1)\n    B11, B12, B21, B22 = q(B, 0, 0), q(B, 0, 1), q(B, 1, 0), q(B, 1, 1)\n    M1 = strassen(add(A11, A22), add(B11, B22))\n    M2 = strassen(add(A21, A22), B11)\n    M3 = strassen(A11, add(B12, B22, -1))\n    M4 = strassen(A22, add(B21, B11, -1))\n    M5 = strassen(add(A11, A12), B22)\n    M6 = strassen(add(A21, A11, -1), add(B11, B12))\n    M7 = strassen(add(A12, A22, -1), add(B21, B22))\n    C11 = add(add(add(M1, M4), M5, -1), M7)\n    C12 = add(M3, M5)\n    C21 = add(M2, M4)\n    C22 = add(add(add(M1, M3), M2, -1), M6)\n    out = [[0] * n for _ in range(n)]\n    for i in range(m):\n        for j in range(m):\n            out[i][j] = C11[i][j]\n            out[i][j + m] = C12[i][j]\n            out[i + m][j] = C21[i][j]\n            out[i + m][j + m] = C22[i][j]\n    return out\n\nA = [[1, 2], [3, 4]]\nB = [[5, 6], [7, 8]]\nC = strassen(A, B)\nassert C == [[19, 22], [43, 50]]\nprint(*C[0])\nprint(*C[1])\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Strassen&#8217;s algorithm returns the seven block products and the exponent log base 2 of 7. <strong>Strassen&#8217;s algorithm multiplies n by n matrices with seven recursive half-size products, in O(n^2.807) arithmetic operations.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Strassen&#8217;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\u2082 7}), about O(n^2.807), against the classical&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,56],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-arithmetic"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":660,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/261\/revisions\/660"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}