Stranssen矩阵乘法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Strassen矩阵乘法

A和B都是n*n的方阵,计算C=A* B。

这个通常做法的乘法计算量是0(exp(n,3)),为了把这个计算数量级从3降到 log2(7),log2(7)略小于2.81。我们使用Strassen乘法,方法如下:

输入方阵的阶,和方阵A,B,输出C。

测试数据:

Sample In (in.txt)

9

1 3

2 5 8 9

3 8 3

5 3 2 7 7 3 2 2 6

5 6 7 8 4 2 1 7 3

2 5 4 7 4 8 1 5 7

8 3 2 6 3 8 6 5 8

2 6 4 8

3 2 1 8 9

3 5

4 7

5 4

6 3 0

2 5 8 5 9

3 1 6 8

3 5 2 1 6 7 5 8 3

11 13 12 25 38 49 33 28 3

35 3 2 7 7 3 2 2 23

52 6 7 8 43 2 1 47 44

24 5 4 7 4 8 1 5 2

81 3 24 6 36 8 36 5 0

2 62 34 8

3 2 1 8 2

3 5 3

4 7

5 45

6 3 21

2 35 8 5 94

3 15 6 1

0 34 33 23 22 1 2 5 3

Sample Out (out.txt)

1031 1038 815 347 1313 346 487 337 268 1015 612 660 439 916 478 489 382 254 1166 653 506 430 1444 446 455 617 518 926 1026 763 447 1108 292 334 441 374 728 1137 992 612 1243 786 521 521 364 898 832 631 468 1391 294 344 423 396 1021 511 586 310 853 553 385 410 451 1483 814 774 483 1553 318 528 615 540 867 930 779 369 1319 488 491 357 350 Code:(strassen.cpp)

#include

#include

#define N 65

using namespace std;

void show(int n,int array[][N])

{

for(int i=0;i

{

for(int j=0;j

{

cout<

}

cout<

}

cout<

}

/*strassen矩?阵¨®计?算?函¡¥数ºy,ê?申¦¨º明¡Â不?定¡§义°?*/ void strassen(int n,int A[][N],int B[][N],int C[][N]);

/*矩?阵¨®加¨®法¤¡§*/

void matMin(int a[][N],int b[][N],int c[][N],int n)

{

for(int i=0;i

{

for(int j=0;j

{

c[i][j]=a[i][j]-b[i][j];

}

}

}

/*方¤?阵¨®减?法¤¡§*/

void matPlu(int a[][N],int b[][N],int c[][N],int n)

{

for(int i=0;i

{

for(int j=0;j

{

c[i][j]=a[i][j]+b[i][j];

}

}

}

/*m函¡¥数ºy的Ì?计?算?*/

void funcM1(int m1[][N],int n,int a11[][N],int a12[][N],int a21[][N],int a22[][N],int b11[][N],int b12[][N],int b21[][N],int b22[][N])

{

int temp1[N][N];

matPlu(a21,a22,temp1,n);

matMin(temp1,a11,temp1,n);

int temp2[N][N];

matMin(b22,b12,temp2,n);

matPlu(temp2,b11,temp2,n);

strassen(n,temp1,temp2,m1);

}

void funcM2(int m2[][N],int n,int a11[][N],int a12[][N],int a21[][N],int a22[][N],int b11[][N],int b12[][N],int b21[][N],int b22[][N])

{

strassen(n,a11,b11,m2);

}

void funcM3(int m3[][N],int n,int a11[][N],int a12[][N],int a21[][N],int a22[][N],int b11[][N],int b12[][N],int b21[][N],int b22[][N])

{

strassen(n,a12,b21,m3);

}

void funcM4(int m4[][N],int n,int a11[][N],int a12[][N],int a21[][N],int a22[][N],int b11[][N],int b12[][N],int b21[][N],int b22[][N])

{

int temp1[N][N];

matMin(a11,a21,temp1,n);

int temp2[N][N];

matMin(b22,b12,temp2,n);

strassen(n,temp1,temp2,m4);

}

void funcM5(int m5[][N],int n,int a11[][N],int a12[][N],int a21[][N],int a22[][N],int b11[][N],int b12[][N],int b21[][N],int b22[][N])

{

int temp1[N][N];

相关文档
最新文档