主题:接稀疏矩阵运算器
接稀疏矩阵运算器
Status SubtSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix *Q)
{
int i;
if(M.mu!=N.mu||M.nu!=N.nu)
return ERROR;
for(i=1;i<=N.tu;++i)
N.data[i].e*=-1;
AddSMatrix(M,N,Q);
return OK;
}
Status MultSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix *Q)
{
int arow,brow,p,q,ccol,ctemp[MAXRC+1];
if(M.nu!=N.mu)
return ERROR;
(*Q).mu=M.mu;
(*Q).nu=N.nu;
(*Q).tu=0;
M.rpos[M.mu+1]=M.tu+1;
N.rpos[N.mu+1]=N.tu+1;
if(M.tu*N.tu!=0)
{
for(arow=1;arow<=M.mu;++arow)
{
for(ccol=1;ccol<=(*Q).nu;++ccol)
ctemp[ccol]=0;
(*Q).rpos[arow]=(*Q).tu+1;
for(p=M.rpos[arow];p<M.rpos[arow+1];++p)
{
brow=M.data[p].j;
for(q=N.rpos[brow];q<N.rpos[brow+1];++q)
{
ccol=N.data[q].j;
ctemp[ccol]+=M.data[p].e*N.data[q].e;
}
}
for(ccol=1;ccol<=(*Q).nu;++ccol)
if(ctemp[ccol])
{
if(++(*Q).tu>MAXSIZE)
return ERROR;
(*Q).data[(*Q).tu].i=arow;
(*Q).data[(*Q).tu].j=ccol;
(*Q).data[(*Q).tu].e=ctemp[ccol];
}
}
}
return OK;
}
Status TransposeSMatrix(RLSMatrix M,RLSMatrix *T)
{
int p,q,t,col,*num;
num=(int *)malloc((M.nu+1)*sizeof(int));
(*T).mu=M.nu;
(*T).nu=M.mu;
(*T).tu=M.tu;
if((*T).tu)
{
for(col=1;col<=M.nu;++col)
num[col]=0;
for(t=1;t<=M.tu;++t)
++num[M.data[t].j];
(*T).rpos[1]=1;
for(col=2;col<=M.nu;++col)
(*T).rpos[col]=(*T).rpos[col-1]+num[col-1];
for(col=1;col<=M.nu;++col)
num[col]=(*T).rpos[col];
for(p=1;p<=M.tu;++p)
{
col=M.data[p].j;
q=num[col];
(*T).data[q].i=M.data[p].j;
(*T).data[q].j=M.data[p].i;
(*T).data[q].e=M.data[p].e;
++num[col];
}
}
free(num);
return OK;
}
void main()
{
RLSMatrix A,B,C;
printf("创建矩阵A: ");
CreateSMatrix(&A);
PrintSMatrix(A);
printf("由矩阵A复制矩阵B: ");
CopySMatrix(A,&B);
PrintSMatrix(B);
DestroySMatrix(&B);
printf("销毁矩阵B后:\n");
PrintSMatrix(B);
printf("创建矩阵B2:(与矩阵A的行、列数相同,行、列分别为%d,%d)\n",A.mu,A.nu);
CreateSMatrix(&B);
PrintSMatrix(B);
printf("矩阵C1(A+B): ");
AddSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&C);
printf("矩阵C2(A-B): ");
SubtSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&C);
printf("矩阵C3(A的转置): ");
TransposeSMatrix(A,&C);
PrintSMatrix(C);
DestroySMatrix(&A);
DestroySMatrix(&B);
DestroySMatrix(&C);
printf("创建矩阵A2: ");
CreateSMatrix(&A);
PrintSMatrix(A);
printf("创建矩阵B3:(行数应与矩阵A2的列数相同=%d)\n",A.nu);
CreateSMatrix(&B);
PrintSMatrix(B);
printf("矩阵C5(A*B): ");
MultSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&A);
DestroySMatrix(&B);
DestroySMatrix(&C);
}
Status SubtSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix *Q)
{
int i;
if(M.mu!=N.mu||M.nu!=N.nu)
return ERROR;
for(i=1;i<=N.tu;++i)
N.data[i].e*=-1;
AddSMatrix(M,N,Q);
return OK;
}
Status MultSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix *Q)
{
int arow,brow,p,q,ccol,ctemp[MAXRC+1];
if(M.nu!=N.mu)
return ERROR;
(*Q).mu=M.mu;
(*Q).nu=N.nu;
(*Q).tu=0;
M.rpos[M.mu+1]=M.tu+1;
N.rpos[N.mu+1]=N.tu+1;
if(M.tu*N.tu!=0)
{
for(arow=1;arow<=M.mu;++arow)
{
for(ccol=1;ccol<=(*Q).nu;++ccol)
ctemp[ccol]=0;
(*Q).rpos[arow]=(*Q).tu+1;
for(p=M.rpos[arow];p<M.rpos[arow+1];++p)
{
brow=M.data[p].j;
for(q=N.rpos[brow];q<N.rpos[brow+1];++q)
{
ccol=N.data[q].j;
ctemp[ccol]+=M.data[p].e*N.data[q].e;
}
}
for(ccol=1;ccol<=(*Q).nu;++ccol)
if(ctemp[ccol])
{
if(++(*Q).tu>MAXSIZE)
return ERROR;
(*Q).data[(*Q).tu].i=arow;
(*Q).data[(*Q).tu].j=ccol;
(*Q).data[(*Q).tu].e=ctemp[ccol];
}
}
}
return OK;
}
Status TransposeSMatrix(RLSMatrix M,RLSMatrix *T)
{
int p,q,t,col,*num;
num=(int *)malloc((M.nu+1)*sizeof(int));
(*T).mu=M.nu;
(*T).nu=M.mu;
(*T).tu=M.tu;
if((*T).tu)
{
for(col=1;col<=M.nu;++col)
num[col]=0;
for(t=1;t<=M.tu;++t)
++num[M.data[t].j];
(*T).rpos[1]=1;
for(col=2;col<=M.nu;++col)
(*T).rpos[col]=(*T).rpos[col-1]+num[col-1];
for(col=1;col<=M.nu;++col)
num[col]=(*T).rpos[col];
for(p=1;p<=M.tu;++p)
{
col=M.data[p].j;
q=num[col];
(*T).data[q].i=M.data[p].j;
(*T).data[q].j=M.data[p].i;
(*T).data[q].e=M.data[p].e;
++num[col];
}
}
free(num);
return OK;
}
void main()
{
RLSMatrix A,B,C;
printf("创建矩阵A: ");
CreateSMatrix(&A);
PrintSMatrix(A);
printf("由矩阵A复制矩阵B: ");
CopySMatrix(A,&B);
PrintSMatrix(B);
DestroySMatrix(&B);
printf("销毁矩阵B后:\n");
PrintSMatrix(B);
printf("创建矩阵B2:(与矩阵A的行、列数相同,行、列分别为%d,%d)\n",A.mu,A.nu);
CreateSMatrix(&B);
PrintSMatrix(B);
printf("矩阵C1(A+B): ");
AddSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&C);
printf("矩阵C2(A-B): ");
SubtSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&C);
printf("矩阵C3(A的转置): ");
TransposeSMatrix(A,&C);
PrintSMatrix(C);
DestroySMatrix(&A);
DestroySMatrix(&B);
DestroySMatrix(&C);
printf("创建矩阵A2: ");
CreateSMatrix(&A);
PrintSMatrix(A);
printf("创建矩阵B3:(行数应与矩阵A2的列数相同=%d)\n",A.nu);
CreateSMatrix(&B);
PrintSMatrix(B);
printf("矩阵C5(A*B): ");
MultSMatrix(A,B,&C);
PrintSMatrix(C);
DestroySMatrix(&A);
DestroySMatrix(&B);
DestroySMatrix(&C);
}