Introduction
介绍
Strassen in 1969 which gives an overview that how we can find the multiplication of two 2*2 dimension matrix by the brute-force algorithm. But by using divide and conquer technique the overall complexity for multiplication two matrices is reduced. This happens by decreasing the total number if multiplication performed at the expenses of a slight increase in the number of addition.
Strassen在1969年概述了如何用蛮力算法找到两个2 * 2维矩阵的乘法。 但是通过使用分而治之技术,两个矩阵相乘的整体复杂度降低了。 如果以乘法数略有增加为代价执行乘法,则会通过减少总数来实现。
For multiplying the two 2*2 dimension matrices Strassen's used some formulas in which there are seven multiplication and eighteen addition, subtraction, and in brute force algorithm, there is eight multiplication and four addition. The utility of Strassen's formula is shown by its asymptotic superiority when order n of matrix reaches infinity. Let us consider two matrices A and B, n*n dimension, where n is a power of two. It can be observed that we can contain four n/2*n/2 submatrices from A, B and their product C. C is the resultant matrix of A and B.
为了将两个2 * 2维矩阵相乘, 斯特拉森使用了一些公式,其中有七个乘法和十八个加法,减法,在蛮力算法中,有八个乘法和四个加法。 Strassen公式的效用由矩阵阶数n达到无穷大时的渐近优越性表示。 让我们考虑两个矩阵A和B , n * n维,其中n是2的幂。 可以观察到,我们可以包含来自A , B及其乘积C的四个n / 2 * n / 2个子矩阵。 C是A和B的合成矩阵。
Strassen矩阵乘法的过程 (Procedure of Strassen matrix multiplication)
There are some procedures:
有一些过程:
Divide a matrix of order of 2*2 recursively till we get the matrix of 2*2.
递归除以2 * 2的矩阵,直到得到2 * 2的矩阵。
Use the previous set of formulas to carry out 2*2 matrix multiplication.
使用前面的一组公式进行2 * 2矩阵乘法。
In this eight multiplication and four additions, subtraction are performed.
在这八个乘法和四个加法中,执行减法。
Combine the result of two matrixes to find the final product or final matrix.
合并两个矩阵的结果以找到最终乘积或最终矩阵。
Stassen矩阵乘法的公式 (Formulas for Stassen’s matrix multiplication)
In Strassen’s matrix multiplication there are seven multiplication and four addition, subtraction in total.
在Strassen的矩阵乘法中,总共有七个乘法和四个加减法。
1. D1 = (a11 + a22) (b11 + b22)
2. D2 = (a21 + a22).b11
3. D3 = (b12 – b22).a11
4. D4 = (b21 – b11).a22
5. D5 = (a11 + a12).b22
6. D6 = (a21 – a11) . (b11 + b12)
7. D7 = (a12 – a22) . (b21 + b22)
C11 = d1 + d4 – d5 + d7
C12 = d3 + d5
C21 = d2 + d4
C22 = d1 + d3 – d2 – d6
Strassen矩阵乘法的算法 (Algorithm for Strassen’s matrix multiplication)
Algorithm Strassen(n, a, b, d)
算法Strassen(n,a,b,d)
begin
If n = threshold then compute
C = a * b is a conventional matrix.
Else
Partition a into four sub matrices a11, a12, a21, a22.
Partition b into four sub matrices b11, b12, b21, b22.
Strassen ( n/2, a11 + a22, b11 + b22, d1)
Strassen ( n/2, a21 + a22, b11, d2)
Strassen ( n/2, a11, b12 – b22, d3)
Strassen ( n/2, a22, b21 – b11, d4)
Strassen ( n/2, a11 + a12, b22, d5)
Strassen (n/2, a21 – a11, b11 + b22, d6)
Strassen (n/2, a12 – a22, b21 + b22, d7)
C = d1+d4-d5+d7 d3+d5
d2+d4 d1+d3-d2-d6
end if
return (C)
end.
Strassen矩阵乘法代码 (Code for Strassen matrix multiplication)
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements of second matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Output:
输出:
Enter the 4 elements of first matrix:
5 6 1 7
Enter the 4 element of second matrix:
6 2 8 7
The first matrix is
5 6
1 7
The second matrix is
6 2
8 7
After multiplication
78 52
62 51
Complexity: The time complexity is O(N2.8074).
复杂度:时间复杂度为O(N 2.8074 ) 。
翻译自: https://www.includehelp.com/algorithms/strassen-matrix-multiplication.aspx