Strassen algorithm(O(n^lg7))

Let AB be two square matrices over a ring R. We want to calculate the matrix product C as

{displaystyle mathbf {C} =mathbf {A} mathbf {B} qquad mathbf {A} ,mathbf {B} ,mathbf {C} in R^{2^{n} imes 2^{n}}}mathbf {C} =mathbf {A} mathbf {B} qquad mathbf {A} ,mathbf {B} ,mathbf {C} in R^{2^{n}	imes 2^{n}}

If the matrices AB are not of type 2n × 2n we fill the missing rows and columns with zeros.

We partition AB and C into equally sized block matrices

{displaystyle mathbf {A} ={egin{bmatrix}mathbf {A} _{1,1}&mathbf {A} _{1,2}\mathbf {A} _{2,1}&mathbf {A} _{2,2}end{bmatrix}}{mbox{ , }}mathbf {B} ={egin{bmatrix}mathbf {B} _{1,1}&mathbf {B} _{1,2}\mathbf {B} _{2,1}&mathbf {B} _{2,2}end{bmatrix}}{mbox{ , }}mathbf {C} ={egin{bmatrix}mathbf {C} _{1,1}&mathbf {C} _{1,2}\mathbf {C} _{2,1}&mathbf {C} _{2,2}end{bmatrix}}}mathbf {A} ={egin{bmatrix}mathbf {A} _{1,1}&mathbf {A} _{1,2}\mathbf {A} _{2,1}&mathbf {A} _{2,2}end{bmatrix}}{mbox{ , }}mathbf {B} ={egin{bmatrix}mathbf {B} _{1,1}&mathbf {B} _{1,2}\mathbf {B} _{2,1}&mathbf {B} _{2,2}end{bmatrix}}{mbox{ , }}mathbf {C} ={egin{bmatrix}mathbf {C} _{1,1}&mathbf {C} _{1,2}\mathbf {C} _{2,1}&mathbf {C} _{2,2}end{bmatrix}}

with

{displaystyle mathbf {A} _{i,j},mathbf {B} _{i,j},mathbf {C} _{i,j}in R^{2^{n-1} imes 2^{n-1}}}mathbf {A} _{i,j},mathbf {B} _{i,j},mathbf {C} _{i,j}in R^{2^{n-1}	imes 2^{n-1}}

then

{displaystyle mathbf {C} _{1,1}=mathbf {A} _{1,1}mathbf {B} _{1,1}+mathbf {A} _{1,2}mathbf {B} _{2,1}}mathbf {C} _{1,1}=mathbf {A} _{1,1}mathbf {B} _{1,1}+mathbf {A} _{1,2}mathbf {B} _{2,1}
{displaystyle mathbf {C} _{1,2}=mathbf {A} _{1,1}mathbf {B} _{1,2}+mathbf {A} _{1,2}mathbf {B} _{2,2}}mathbf {C} _{1,2}=mathbf {A} _{1,1}mathbf {B} _{1,2}+mathbf {A} _{1,2}mathbf {B} _{2,2}
{displaystyle mathbf {C} _{2,1}=mathbf {A} _{2,1}mathbf {B} _{1,1}+mathbf {A} _{2,2}mathbf {B} _{2,1}}mathbf {C} _{2,1}=mathbf {A} _{2,1}mathbf {B} _{1,1}+mathbf {A} _{2,2}mathbf {B} _{2,1}
{displaystyle mathbf {C} _{2,2}=mathbf {A} _{2,1}mathbf {B} _{1,2}+mathbf {A} _{2,2}mathbf {B} _{2,2}}mathbf {C} _{2,2}=mathbf {A} _{2,1}mathbf {B} _{1,2}+mathbf {A} _{2,2}mathbf {B} _{2,2}

With this construction we have not reduced the number of multiplications. We still need 8 multiplications to calculate the Ci,j matrices, the same number of multiplications we need when using standard matrix multiplication.

Now comes the important part. We define new matrices

{displaystyle mathbf {M} _{1}:=(mathbf {A} _{1,1}+mathbf {A} _{2,2})(mathbf {B} _{1,1}+mathbf {B} _{2,2})}mathbf {M} _{1}:=(mathbf {A} _{1,1}+mathbf {A} _{2,2})(mathbf {B} _{1,1}+mathbf {B} _{2,2})
{displaystyle mathbf {M} _{2}:=(mathbf {A} _{2,1}+mathbf {A} _{2,2})mathbf {B} _{1,1}}mathbf {M} _{2}:=(mathbf {A} _{2,1}+mathbf {A} _{2,2})mathbf {B} _{1,1}
{displaystyle mathbf {M} _{3}:=mathbf {A} _{1,1}(mathbf {B} _{1,2}-mathbf {B} _{2,2})}mathbf {M} _{3}:=mathbf {A} _{1,1}(mathbf {B} _{1,2}-mathbf {B} _{2,2})
{displaystyle mathbf {M} _{4}:=mathbf {A} _{2,2}(mathbf {B} _{2,1}-mathbf {B} _{1,1})}mathbf {M} _{4}:=mathbf {A} _{2,2}(mathbf {B} _{2,1}-mathbf {B} _{1,1})
{displaystyle mathbf {M} _{5}:=(mathbf {A} _{1,1}+mathbf {A} _{1,2})mathbf {B} _{2,2}}mathbf {M} _{5}:=(mathbf {A} _{1,1}+mathbf {A} _{1,2})mathbf {B} _{2,2}
{displaystyle mathbf {M} _{6}:=(mathbf {A} _{2,1}-mathbf {A} _{1,1})(mathbf {B} _{1,1}+mathbf {B} _{1,2})}mathbf {M} _{6}:=(mathbf {A} _{2,1}-mathbf {A} _{1,1})(mathbf {B} _{1,1}+mathbf {B} _{1,2})
{displaystyle mathbf {M} _{7}:=(mathbf {A} _{1,2}-mathbf {A} _{2,2})(mathbf {B} _{2,1}+mathbf {B} _{2,2})}mathbf {M} _{7}:=(mathbf {A} _{1,2}-mathbf {A} _{2,2})(mathbf {B} _{2,1}+mathbf {B} _{2,2})

only using 7 multiplications (one for each Mk) instead of 8. We may now express the Ci,j in terms of Mk, like this:

{displaystyle mathbf {C} _{1,1}=mathbf {M} _{1}+mathbf {M} _{4}-mathbf {M} _{5}+mathbf {M} _{7}}mathbf {C} _{1,1}=mathbf {M} _{1}+mathbf {M} _{4}-mathbf {M} _{5}+mathbf {M} _{7}
{displaystyle mathbf {C} _{1,2}=mathbf {M} _{3}+mathbf {M} _{5}}mathbf {C} _{1,2}=mathbf {M} _{3}+mathbf {M} _{5}
{displaystyle mathbf {C} _{2,1}=mathbf {M} _{2}+mathbf {M} _{4}}mathbf {C} _{2,1}=mathbf {M} _{2}+mathbf {M} _{4}
{displaystyle mathbf {C} _{2,2}=mathbf {M} _{1}-mathbf {M} _{2}+mathbf {M} _{3}+mathbf {M} _{6}}mathbf {C} _{2,2}=mathbf {M} _{1}-mathbf {M} _{2}+mathbf {M} _{3}+mathbf {M} _{6}

We iterate this division process n times (recursively) until the submatrices degenerate into numbers (elements of the ring R). The resulting product will be padded with zeroes just like A and B, and should be stripped of the corresponding rows and columns.

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).

 from Wikipedia

--------------------------------------------------------------------------------------------------------------------------------------------------------------

it substitude the 8th recursive invocation(multiplication) by the liner combination of the submatrices above(cause A4,4 and B4,4 has been used before).
like a*(b+c) can have less steps than a*b+a*c,it uses liner combination to simplify the tranditional multiplicate way.
原文地址:https://www.cnblogs.com/wujunde/p/6935348.html