Game Development Reference
In-Depth Information
void preCGM(CSparseMatrix &S, //Coefficient matrix (in)
CVector &b, //Constant terms vector(in)
CVector &x, //System solution and
//initial solution (in out)
CPreconditioner &P, //Preconditioner (in)
int iter, //Number of iterations (in)
float epsilon) //Maximum error tolerance (in)
{ CVector r,d,q,s;
double sigmaOld,sigmaNew,alfa,beta,tol;
r.resize(b.size()); d.resize(b.size());
q.resize(b.size()); s.resize(b.size());
//Initialization.
r=b S x;
// The preconditioner class solves: Pd=r
P.solve(r,d);
sigmaNew = dot(r,d);
for ( int i=0; i
<
iter && epsilon
<
sigmaNew; i++)
{ q=S
d;
alfa = sigmaNew / dot (d,q);
//Solution refinement
x+=afla d;
r =alfa q;
//The preconditioner class solves: Ps=r
P.solve(r,s);
sigmaOld = sigmaNew;
sigmaNew = dot(r,s);
beta = sigmaNew/sigmaOld;
d=s+beta d;
}
}
Listing 10.6. Preconditioned conjugate-gradient method implementation.
The convergency of the method can be improved by using a good initial solu-
tion. Generally, the position computed in the last simulation step is used.
The condition number κ ( S ) of S also influences the convergency. The smaller
it is, the faster the convergency. If κ ( S )=1, the solution is achieved in only one
Search Nedrilad ::




Custom Search