Strassen algorithm
≈ 11 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity ( O(n^{\log_2 7}) versus O(n^3) ), although the naive algorithm is often better for smaller matrices. The Strassen algorithm is slower than the fastest known algorithms for extremely large matrices, but such galactic algorithms are not useful in practice, as they are much slower for matrices of practical size. For small matrices, highly optimized implementations of the conventional algorithm are generally faster because Strassen's additional additions and recursion overhead outweigh its reduction in multiplications. Strassen's algorithm works for any ring such as plus/multiply, but not all semirings, particularly combinatorial matrix multiplication such as min-plus or boolean algebra, where the naive algorithm still works.
In linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity ( O(n^{\log_2 7}) versus O(n^3) ), although the naive algorithm is often better for smaller matrices. The Strassen algorithm is slower than the fastest known algorithms for extremely large matrices, but such galactic algorithms are not useful in practice, as they are much slower for matrices of practical size. For small matrices, highly optimized implementations of the conventional algorithm are generally faster because Strassen's additional additions and recursion overhead outweigh its reduction in multiplications. Strassen's algorithm works for any ring such as plus/multiply, but not all semirings, particularly combinatorial matrix multiplication such as min-plus or boolean algebra, where the naive algorithm still works. Let A , B be two square matrices over a ring \mathcal{R} , for example matrices whose entries are integers or the real numbers. The goal of matrix multiplication is to calculate the matrix product C = AB . The following exposition of the algorithm assumes that all of these matrices have sizes that are powers of two (i.e., A, \, B, \, C \in \operatorname{Matr}_{2^n \times 2^n} (\mathcal{R}) ), but this is only conceptually necessary — if the matrices A , B are not of type 2^n \times 2^n , the "missing" rows and columns can be filled with zeros to obtain matrices with sizes of powers of two — though real implementations of the algorithm do not do this in practice. The Strassen algorithm partitions A , B and C into equally sized block matrices A = \begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{bmatrix}, \quad B = \begin{bmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \end{bmatrix}, \quad C = \begin{bmatrix} C_{11} & C_{12} \\ C_{21} & C_{22} \end{bmatrix}, \quad with A_{ij}, B_{ij}, C_{ij} \in \operatorname{Mat}_{2^{n-1} \times 2^{n-1}} (\mathcal{R}) . The naive algorithm would be: \begin{bmatrix} C_{11} & C_{12} \\ C_{21} & C_{22} \end{bmatrix} = \begin{bmatrix} A_{11} {\color{red}\times} B_{11} + A_{12} {\color{red}\times} B_{21} \quad & A_{11} {\color{red}\times} B_{12} + A_{12} {\color{red}\times} B_{22} \\ A_{21} {\color{red}\times} B_{11} + A_{22} {\color{red}\times} B_{21} \quad & A_{21} {\color{red}\times} B_{12} + A_{22} {\color{red}\times} B_{22} \end{bmatrix}. This construction does not reduce the number of multiplications: 8 multiplications of matrix blocks are still needed to calculate the C_{ij} matrices, the same number of multiplications needed when using standard matrix multiplication. The Strassen algorithm defines instead new values: \begin{align} M_1 &= (A_{11} + A_{22}) {\color{red}\times} (B_{11} + B_{22}); \\ M_2 &= (A_{21} + A_{22}) {\color{red}\times} B_{11}; \\ M_3 &= A_{11} {\color{red}\times} (B_{12} - B_{22}); \\ M_4 &= A_{22} {\color{red}\times} (B_{21} - B_{11}); \\ M_5 &= (A_{11} + A_{12}) {\color{red}\times} B_{22}; \\ M_6 &= (A_{21} - A_{11}) {\color{red}\times} (B_{11} + B_{12}); \\ M_7 &= (A_{12} - A_{22}) {\color{red}\times} (B_{21} + B_{22}), \\ \end{align} using only 7 multiplications (one for each M_k ) instead of 8. We may now express the C_{ij} in terms of M_k : \begin{bmatrix} C_{11} & C_{12} \\ C_{21} & C_{22} \end{bmatrix} = \begin{bmatrix} M_1 + M_4 - M_5 + M_7 \quad & M_3 + M_5 \\ M_2 + M_4 \quad & M_1 - M_2 + M_3 + M_6 \end{bmatrix}. We recursively iterate this division process until the submatrices degenerate into numbers (elements of the ring \mathcal{R} ). If, as mentioned above, the original matrix had a size that was not a power of 2, then the resulting product will have zero rows and columns just like A and B , and these will then be stripped at this point to obtain the (smaller) matrix C we really wanted. Practical implementations of Strassen's algorithm switch to standard methods of matrix multiplication for small enough submatrices, for which those algorithms are more efficient. The particular crossover point for which Strassen's algorithm is more efficient depends on the specific implementation and hardware. Earlier authors had estimated that Strassen's algorithm is faster for matrices with widths from 32 to 128 for optimized implementations. However, it has been observed that this crossover point has been increasing in recent years, and a 2010 study found that even a single step of Strassen's algorithm is often not beneficial on current architectures, compared to a highly optimized traditional multiplication, until matrix sizes exceed 1000 or more, and even for matrix sizes of several thousand the benefit is typically marginal at best (around 10% or less). A more recent study (2016) observed benefits for matrices as small as 512 and a benefit around 20%. = \begin{bmatrix} t + b{\color{red}\times}B & w + v + (a + b - c - d){\color{red}\times}D \\ w + u + d{\color{red}\times}(B + C - A - D) & w + u + v \end{bmatrix} where t = a{\color{red}\times}A, \; u = (c - a){\color{red}\times}(C - D), \; v = (c + d){\color{red}\times}(C - A), \; w = t + (c + d - a){\color{red}\times}(A + D - C) . This reduces the number of matrix additions and subtractions from 18 to 15. The number of matrix multiplications is still 7, and the asymptotic complexity is the same. The algorithm was further optimised in 2017 using an alternative basis, reducing the number of matrix additions per bilinear step to 12 while maintaining the number of matrix multiplications, and again in 2023: \begin{align} A_{22} &= A_{12} - A_{21} + A_{22}; \\ B_{22} &= B_{12} - B_{21} + B_{22}, \end{align} \begin{align} t_1 &= A_{21} + A_{22}; \\ t_2 &= A_{22} - A_{12}; \\ t_3 &= A_{22} - A_{11}; \\ t_4 &= B_{22} - B_{11}; \\ t_5 &= B_{21} + B_{22}; \\ t_6 &= B_{22} - B_{12}, \end{align} \begin{align} M_1 &= A_{11} {\color{red}\times} B_{11}; \\ M_2 &= A_{12} {\color{red}\times} B_{21}; \\ M_3 &= A_{21} {\color{red}\times} t_4; \\ M_4 &= A_{22} {\color{red}\times} B_{22}; \\ M_5 &= t_1 {\color{red}\times} t_5; \\ M_6 &= t_2 {\color{red}\times} t_6; \\ M_7 &= t_3 {\color{red}\times} B_{12}, \end{align} \begin{align} C_{11} &= M_1 + M_2; \\ C_{12} &= M_5 - M_7; \\ C_{21} &= M_3 + M_6; \\ C_{22} &= M_5 + M_6-M_2-M_4. \\ \end{align} \begin{align} C_{12} &= C_{12} - C_{22}; \\ C_{21} &= C_{22} - C_{21}, \end{align} The question then is how many operations exactly one needs for Strassen's algorithms, and how this compares with the standard matrix multiplication that takes approximately 2 N^3 (where N = 2^n ) arithmetic operations, i.e. an asymptotic complexity \Theta (N^3) . The number of additions and multiplications required in the Strassen algorithm can be calculated as follows: let f(n) be the number of operations for a 2^n \times 2^n matrix. Then by recursive application of the Strassen algorithm, we see that f(n) = 7 f(n-1) + l 4^n , for some constant l that depends on the number of additions performed at each application of the algorithm. Hence f(n) = (7 + o(1))^n , i.e., the asymptotic complexity for multiplying matrices of size N = 2^n using the Strassen algorithm is O([7+o(1)]^n) = O(N^{\log_{2}7+o(1)}) \approx O(N^{2.8074}) . The reduction in the number of arithmetic operations however comes at the price of a somewhat reduced numerical stability, and the algorithm also requires significantly more memory compared to the naive algorithm. Both initial matrices must have their dimensions expanded to the next power of 2, which results in storing up to four times as many elements, and the seven auxiliary matrices each contain a quarter of the elements in the expanded ones. Strassen's algorithm needs to be compared to the "naive" way of doing the matrix multiplication that would require 8 instead of 7 multiplications of sub-blocks. This would then give rise to the complexity one expects from the standard approach: O(8^n) = O(N^{\log_{2}8}) = O(N^3) . The comparison of these two algorithms shows that asymptotically, Strassen's algorithm is faster: There exists a size N_\text{threshold} so that matrices that are larger are more efficiently multiplied with Strassen's algorithm than the "traditional" way. However, the asymptotic statement does not imply that Strassen's algorithm is always faster even for small matrices, and in practice this is in fact not the case: For small matrices, the cost of the additional additions of matrix blocks outweighs the savings in the number of multiplications. There are also other factors not captured by the analysis above, such as the difference in cost on today's hardware between loading data from memory onto processors vs. the cost of actually doing operations on this data. As a consequence of these sorts of considerations, Strassen's algorithm is typically only used on "large" matrices. This kind of effect is even more pronounced with alternative algorithms such as the one by Coppersmith and Winograd: While asymptotically even faster, the cross-over point N_\text{threshold} is so large that the algorithm is not generally used on matrices one encounters in practice. R(\phi/\mathbf F) = \min \left\{r\left|\exists f_i\in \mathbf A^*,g_i\in\mathbf B^*,w_i\in\mathbf C , \forall \mathbf a\in\mathbf A, \mathbf b\in\mathbf B, \phi(\mathbf a,\mathbf b) = \sum_{i=1}^r f_i(\mathbf a)g_i(\mathbf b)w_i \right.\right\} In other words, the rank of a bilinear map is the length of its shortest bilinear computation. The existence of Strassen's algorithm shows that the rank of 2 \times 2 matrix multiplication is no more than seven. To see this, let us express this algorithm (alongside the standard algorithm) as such a bilinear computation. In the case of matrices, the dual spaces A* and B* consist of maps into the field F induced by a scalar double-dot product, (i.e. in this case the sum of all the entries of a Hadamard product.) It can be shown that the total number of elementary multiplications L required for matrix multiplication is tightly asymptotically bound to the rank R , i.e. L = \Theta(R) , or more specifically, since the constants are known, R / 2 \le L \le R . One useful property of the rank is that it is submultiplicative for tensor products, and this enables one to show that 2^n \times 2^n \times 2^n matrix multiplication can be accomplished with no more than 7^n elementary multiplications for any n . (This n -fold tensor product of the 2 \times 2 \times 2 matrix multiplication map with itself — an n -th tensor power—is realized by the recursive step in the algorithm shown.) ===Cache behavior=== Strassen's algorithm is cache oblivious. Analysis of its cache behavior algorithm has shown it to incur \Theta \left(1 + \frac{n^2}{b} + \frac{n^{\log_2 7}}{b\sqrt{M}} \right) cache misses during its execution, assuming an idealized cache of size M (i.e. with M / b lines of length b ). ==External links== (also includes formulas for fast matrix inversion) Tyler J. Earnest, Strassen's Algorithm on the Cell Broadband Engine
Text derived from https://en.wikipedia.org/w/index.php?oldid=1371474754 under CC BY-SA 4.0; modifications have been made.