C++ 中的反斜杠
backslash in C++
在 Matlab 中 x=A\b
;求解线性系统 Ax=b。是否可以在 C++ 中模仿相同的内容?可以使用运算符重载,但如何在以下程序中使用 \ 而不是 /?
friend Vector operator/(const Vector &b, const Matrix &A)
{ //BiCG Algorithm is used here.
int row=A.getrowdim();
int col=A.getcoldim();
Vector x(row,0.0);
Vector r(row,0.0);
Vector s(row,0.0);
Vector u(row,0.0);
Vector v(row,0.0);
Vector w(row,0.0);
Vector p(row,0.0);
double rho0,beta,alpha,rho;
Vector t(row,0.0);
double norm;
r=b-A*x;
s=r;
rho0=1;
for(int k=1;k<M;k++)
{
rho=s*r;
beta=rho/rho0;
u=r+beta*u;
w=s+beta*w;
v=A*u;
alpha=rho/(w*v);
x=x+alpha*u;
r=r-alpha*v;
s=s-alpha*(Transpose(A)*w);
norm=r.norm(2);
cout<<"Step Number "<<k<<" xnorm "<<norm<<endl;
if(norm<eps)
break;
rho0=rho;
}
return x;
}
没有
你不能重载operator\
(主要是因为它不存在)。
像任何其他函数一样,为操作起一个漂亮、清晰、描述性的名称。
在 Matlab 中 x=A\b
;求解线性系统 Ax=b。是否可以在 C++ 中模仿相同的内容?可以使用运算符重载,但如何在以下程序中使用 \ 而不是 /?
friend Vector operator/(const Vector &b, const Matrix &A)
{ //BiCG Algorithm is used here.
int row=A.getrowdim();
int col=A.getcoldim();
Vector x(row,0.0);
Vector r(row,0.0);
Vector s(row,0.0);
Vector u(row,0.0);
Vector v(row,0.0);
Vector w(row,0.0);
Vector p(row,0.0);
double rho0,beta,alpha,rho;
Vector t(row,0.0);
double norm;
r=b-A*x;
s=r;
rho0=1;
for(int k=1;k<M;k++)
{
rho=s*r;
beta=rho/rho0;
u=r+beta*u;
w=s+beta*w;
v=A*u;
alpha=rho/(w*v);
x=x+alpha*u;
r=r-alpha*v;
s=s-alpha*(Transpose(A)*w);
norm=r.norm(2);
cout<<"Step Number "<<k<<" xnorm "<<norm<<endl;
if(norm<eps)
break;
rho0=rho;
}
return x;
}
没有
你不能重载operator\
(主要是因为它不存在)。
像任何其他函数一样,为操作起一个漂亮、清晰、描述性的名称。