function u = pcg(gev,ev,b,ig,tol,it_max)

% The PCG method for solving Toeplitz system Tx=b with
%     circulant preconditioner C.
% gev:    the eigenvalues of the circulant matrix in which
%           the Toeplitz matrix T is imbedded, see A.3;
% ev:     the eigenvalues of the circulant preconditioner C
%           which depend on the choice of preconditioner, see A.3;
% b:      the right-hand side vector b;
% ig:     the initial guess;
% tol:    the tolerance;
% it_max: the maximal number of iterations;
% u:      the output solution.

r    = b-tx(ig,gev);
u    = ig;
aa   = norm(r);
t1   = 1;
d    = zeros(length(b),1);
iter = 1;
e(1) = aa;
fprintf('\n at step %1.0f, residual=%e', iter-1, e(iter));

while iter < it_max & e(iter)/e(1) > tol,
   z       = cinvx(r,ev);
   t1old   = t1;
   t1      = z'*r;
   beta    = t1/t1old;
   d       = z+beta*d;
   s       = tx(d,gev);
   suma    = d'*s;
   tau     = t1/suma;
   u       = u+tau*d;
   r       = r-tau*s;
   iter    = iter+1;
   e(iter) = sqrt(r'*r);
   fprintf('\n at step %1.0f, relative residual = %e',iter-1,e(iter)/e(1));
end
if (iter == it_max),
   fprintf('\n Maximum iterations reached ');
end
