没有像算法导论中描述那样 采用下标分解矩阵,仍然是一个一个复制元素
#include
#include
//strassen 矩阵乘法
typedef struct matrix{
int rows;
int cols;
double ** body;
} matrix;
matrix *malloc_matrix(int rows,int cols)
{
int i;
if(rows==0||cols==0) return NULL;
matrix *result=(matrix *)malloc(sizeof(matrix));
result->body=(double **)malloc(sizeof(double*)*rows);
for(i=0;ibody[i]=(double*)malloc(sizeof(double)*cols);
}
result->rows=rows;
result->cols=cols;
return result;
}
void free_matrix(matrix *m)
{
int i;
for(i=0;i<m->rows;i++)
{
free(m->body[i]);
}
free(m->body);
free(m);
}
matrix* copy_matrix(matrix *A)
{
int i,j;
matrix *result=malloc_matrix(A->rows,A->cols);
for(i=0;irows;i++)
{
for(j=0;jcols;j++)
{
result->body[i][j]=A->body[i][j];
}
}
result->cols=A->cols;
result->rows=A->rows;
return result;
}
matrix *matrix_add(matrix *A,matrix*B)
{
int i,j;
if (A==NULL&&B!=NULL)
{
return copy_matrix(B);
}
else if(A!=NULL&&B==NULL)
{
return copy_matrix(A);
}
else if(A==NULL&&B==NULL)
{
return NULL;
}
matrix *result=malloc_matrix(A->rows,A->cols);
for(i=0;irows;i++)
{
for(j=0;jcols;j++)
{
result->body[i][j]=A->body[i][j]+B->body[i][j];
}
}
return result;
}
matrix *matrix_sub(matrix *A,matrix*B)
{
int i,j;
if (A==NULL&&B!=NULL)
{
matrix *result=malloc_matrix(B->rows,B->cols);
for(i=0;irows;i++)
{
for(j=0;jcols;j++)
{
result->body[i][j]=(0-B->body[i][j]);
}
}
result->cols=B->cols;
result->rows=B->rows;
return result;
}
else if(A!=NULL&&B==NULL)
{
return copy_matrix(A);
}
else if(A==NULL&&B==NULL)
{
return NULL;
}
matrix *result=malloc_matrix(A->rows,A->cols);
for(i=0;irows;i++)
{
for(j=0;jcols;j++)
{
result->body[i][j]=A->body[i][j]-B->body[i][j];
}
}
return result;
}
matrix ** matrix_split(matrix *A)
{
int i,j;
matrix **ms=(matrix **)malloc(sizeof(matrix*)*4);
int rows=A->rows/2,cols=A->cols/2;
if(A->rows%2!=0)
rows=(A->rows+1)/2;
if(A->cols%2!=0)
cols=(A->cols+1)/2;
ms[0]=malloc_matrix(rows,cols);
ms[1]=malloc_matrix(rows,A->cols-cols);
ms[2]=malloc_matrix(A->rows-rows,cols);
ms[3]=malloc_matrix(A->rows-rows,A->cols-cols);
for(i=0;ifor(j=0;j0]->body[i][j]=A->body[i][j];
}
for(i=0;ifor(j=0;jcols-cols;j++)
{
ms[1]->body[i][j]=A->body[i][j+cols];
}
for(i=0;irows-rows;i++)
for(j=0;j2]->body[i][j]=A->body[i+rows][j];
}
for(i=0;irows-rows;i++)
for(j=0;jcols-cols;j++)
{
ms[3]->body[i][j]=A->body[i+rows][j+cols];
}
return ms;
}
matrix *matrix_merge(matrix * ms[])
{
matrix *result=malloc_matrix(
(ms[0]!=NULL?ms[0]->rows:0)+(ms[2]!=NULL?ms[2]->rows:0),
(ms[0]!=NULL?ms[0]->cols:0)+(ms[1]!=NULL?ms[1]->cols:0));
int i,j;
if(ms[0]!=NULL)
{
for(i=0;i0]->rows;i++)
for(j=0;j0]->cols;j++)
{
result->body[i][j]=ms[0]->body[i][j];
}
}
if(ms[1]!=NULL)
{
for(i=0;i1]->rows;i++)
for(j=0;j1]->cols;j++)
{
result->body[i][j+ms[0]->cols]=ms[1]->body[i][j];
}
}
if(ms[2]!=NULL)
{
for(i=0;i2]->rows;i++)
for(j=0;j2]->cols;j++)
{
result->body[i+ms[0]->rows][j]=ms[2]->body[i][j];
}
}
if(ms[3]!=NULL)
{
for(i=0;i3]->rows;i++)
for(j=0;j3]->cols;j++)
{
result->body[i+ms[0]->rows][j+ms[0]->cols]=ms[3]->body[i][j];
}
}
return result;
}
//获得矩阵中最大的2次幂子矩阵
matrix **get_max_square_matrix(matrix *A)
{
int i,j;
matrix **ms=(matrix **)malloc(sizeof(matrix*)*4);
int n=A->rows>A->cols?A->cols:A->rows;
if(n%2!=0)
{
n-=1;
}
ms[0]=malloc_matrix(n,n);
ms[1]=malloc_matrix(n,A->cols-n);
ms[2]=malloc_matrix(A->rows-n,n);
ms[3]=malloc_matrix(A->rows-n,A->cols-n);
for(i=0;ifor(j=0;j0]->body[i][j]=A->body[i][j];
}
for(i=0;ifor(j=0;jcols-n;j++)
{
ms[1]->body[i][j]=A->body[i][j+n];
}
for(i=0;irows-n;i++)
for(j=0;j2]->body[i][j]=A->body[i+n][j];
}
for(i=0;irows-n;i++)
for(j=0;jcols-n;j++)
{
ms[3]->body[i][j]=A->body[i+n][j+n];
}
return ms;
}
matrix *matrix_mul(matrix *A,matrix *B)
{
matrix *c;
if(A==NULL||B==NULL)
{
return NULL;
}
if (A->rows<2||A->cols<2||B->cols<2||B->rows<2)
{
c=malloc_matrix(A->rows,B->cols);
int i,j,k;
for(i=0;irows;i++)
{
for(j=0;jcols;j++)
{
c->body[i][j]=0;
for(k=0;kcols;k++)
{
c->body[i][j]+= (A->body[i][k]*B->body[k][j]);
}
}
}
return c;
}
else if (A->rows%2!=0||A->cols%2!=0||B->cols%2!=0||B->rows%2!=0)
{
matrix **sa=get_max_square_matrix(A),**sb=get_max_square_matrix(B);
matrix *a11=sa[0],*a12=sa[1],*a21=sa[2],*a22=sa[3];
matrix *b11=sb[0],*b12=sb[1],*b21=sb[2],*b22=sb[3];
matrix *c11=matrix_add(matrix_mul(a11,b11),matrix_mul(a12,b21)),
*c12=matrix_add(matrix_mul(a11,b12),matrix_mul(a12,b22)),
*c21=matrix_add(matrix_mul(a21,b11),matrix_mul(a22,b21)),
*c22= matrix_add(matrix_mul(a21,b12),matrix_mul(a22,b22));
matrix *cs[4]={c11,c12,c21,c22};
c=matrix_merge(cs);
free(a11);
free(a12);
free(a21);
free(a22);
free(b11);
free(b12);
free(b22);
free(b21);
free(c11);
free(c22);
free(c21);
free(c22);
return c;
}
matrix **sa=matrix_split(A),**sb=matrix_split(B);
matrix *a11=sa[0],*a12=sa[1],*a21=sa[2],*a22=sa[3];
matrix *b11=sb[0],*b12=sb[1],*b21=sb[2],*b22=sb[3];
matrix *s1=matrix_sub(b12,b22),*s2=matrix_add(a11,a12),
*s3=matrix_add(a21,a22),*s4=matrix_sub(b21,b11),
*s5=matrix_add(a11,a22),*s6=matrix_add(b11,b22),
*s7=matrix_sub(a12,a22),*s8=matrix_add(b21,b22),
*s9=matrix_sub(a11,a21),*s10=matrix_add(b11,b12);
matrix *p1=matrix_mul(a11,s1),
*p2=matrix_mul(s2,b22),
*p3=matrix_mul(s3,b11),
*p4=matrix_mul(a22,s4),
*p5=matrix_mul(s5,s6),
*p6=matrix_mul(s7,s8),
*p7=matrix_mul(s9,s10);
matrix *c11=matrix_add(matrix_add(p5,p4),matrix_sub(p6,p2)),
*c12=matrix_add(p1,p2),
*c21=matrix_add(p3,p4),
*c22=matrix_add(matrix_sub(p5,p3),matrix_sub(p1,p7));
matrix *cs[4]={c11,c12,c21,c22};
c=matrix_merge(cs);
free(a11);
free(a12);
free(a21);
free(a22);
free(b11);
free(b12);
free(b21);
free(b22);
free(s1);
free(s2);
free(s3);
free(s4);
free(s5);
free(s6);
free(s7);
free(s8);
free(s9);
free(s10);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);
free(p6);
free(p7);
free(c11);
free(c12);
free(c21);
free(c22);
return c;
}
void print_matrix(matrix *A)
{
int i,j;
for( i=0;irows;i++)
{
for( j=0;jcols;j++)
{
printf("%f ",A->body[i][j]);
}
printf("\n");
}
}
int main(void)
{
matrix *A=malloc_matrix(3,6),*B=malloc_matrix(6,3);
//double w[4][4]={{1 ,2 ,3 ,4 },
// {5 ,6 ,7 ,8 },
// {9 ,10,11,12},
// {13,14,15,16}};
double x[3][6]={
{1 ,2 ,3 ,4 , 5,6},
{5 ,6 ,7 ,8 ,6,8},
{21,32,42,52,33,44}
};
double y[6][3]={
{1 ,2 ,3 },
{5 ,6 ,7 },
{9 ,10,11},
{13,14,15},
{13,14,15},
{13,14,15}
};
int i,j;
for( i=0;irows;i++)
{
for(j=0;jcols;j++)
{
A->body[i][j]=x[i][j];
}
}
for( i=0;irows;i++)
{
for(j=0;jcols;j++)
{
B->body[i][j]=y[i][j];
}
}
matrix *c=matrix_mul(A,B);
print_matrix(c);
}