I am a person with programming background and need some math help. I am looking at the source code for an implementation of the Conjugate Gradient iterative solver http://en.wikipedia.org/wiki/Conjugate_gradient_method. The stopping condition is calculated using a function called vect_norm_rel (so if the result of vect_norm_rel < tolerance then stop), which takes as parameters the residual vector computed by CG (this is $r_{k+1}$ from the wikipedia article) and a vector (d_) containing the reciprocals of the diagonal values of the A matrix which is initalized as follows:
for(unsigned int i=0; i<_x.size(); ++i) d_[i] = 1.0/_A(i,i);
The code for vect_norm_rel is as follows and it returns the maximum value over the product of each element of the diagonal (_diag[]) with the corresponding element of the residual $r$ (_v[]).
Real res = 0.0; for(unsigned int i=0; i<_v.size(); ++i) { res = std::max(fabs(_v[i]*_diag[i]), res); } return res;
I am aware of how the residual vector ($r = Ax' -b $ where x' is the approximated solution) is used to see how close an iterative method is to a solution, but I do not understand the above method of calculating the stopping condition and why they use the diagonal. Could someone please explain why this method is used to calculate the stopping condition and how does it differ from using just the residual?
Thanks