大型稀疏矩阵非零系数的确定?
大型稀疏矩阵的形式:程序代码运行带这一段就终止了,检查好几遍,看不不出错误,求指点!for(i=0;i<(P+1)*(N+1);i++)
coefficients.push_back(Tri(i,i,1.0)); //第一个i表示A矩阵中的行数,第二个i表示A矩阵中的列数,1.0表示非零元素的系数
//矩阵A中的中间行
for(i=(P+1)*(N+1);i<SMADi-(P+1)*(N+1);i++)
{
k=i%(P+1)*(N+1);
//矩阵A中每一个B矩阵中的首行
for(k=0;k<P+1;k++)
{
//矩阵B中首行的每一个a矩阵中的首行
if(k==0)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的末行
else if(k==P)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的中间行
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
}
//矩阵A中每一个B矩阵中的中间行
for(k=P+1;k<N*(P+1);k++)
{
for(int m=1;m<N;m++)
{
//矩阵B中首行的每一个a矩阵中的首行
if(k==m*(P+1))
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的末行
else if(k==m*(P+1)+P)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的中间行
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
}
}
//矩阵A中每一个B矩阵中的末行
for(k=N*(P+1);k<(N+1)*(P+1);k++)
{
//矩阵B中首行的每一个a矩阵中的首行
if(k==N*(P+1))
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的末行
else if(k==(N+1)*(P+1)-1)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
//矩阵B中首行的每一个a矩阵中的中间行
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),1.0));
coefficients.push_back(Tri(i,i+(P+1)*(N+1),1.0));
}
}
}
//矩阵A中的末行
for(i=SMADi-(P+1)*(N+1);i<SMADi;i++)
{
//B矩阵中的首行
for(i=SMADi-(P+1)*(N+1);i<SMADi-(P+1)*N;i++)
{
if(i==SMADi-(P+1)*(N+1))
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else if(i==SMADi-(P+1)*N-1)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i+(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
}
//B矩阵的中间行
for(i=SMADi-N*(P+1);i<SMADi-(P+1);i++)
{
for(int k=0;k<N-1;k++)
{
if(i==SMADi-(N-k)*(P+1))
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else if(i==SMADi-(N-k)*(P+1)+P)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i-(P+1),1.0));
coefficients.push_back(Tri(i,i+(P+1),1.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
}
}
//B矩阵中的末行
for(i=SMADi-(P+1);i<SMADi;i++)
{
if(i==SMADi-(P+1))
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i+1,2.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else if(i==SMADi-1)
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,2.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
else
{
coefficients.push_back(Tri(i,i,-6.0));
coefficients.push_back(Tri(i,i-1,1.0));
coefficients.push_back(Tri(i,i+1,1.0));
coefficients.push_back(Tri(i,i-(P+1),2.0));
coefficients.push_back(Tri(i,i-(P+1)*(N+1),2.0));
}
}
}