function [xnew,gradnew,alpha,pnew] = prpluscg(fnhess,fngrad,fn,gradold,xold,p)
% usage: [xnew,gradnew,alpha,pnew] = prpluscg(fnhess,fngrad,fn,gradold,xold,p)
% description: performs one step of Polak-Ribiere plus method with restart after 
% cgrestart steps. Input parameters:
% fngrad: function grad name
% fn: function name
% gradold: gradient at starting point
% xold: starting point
% p: current search direction
% parms: [rho,c1,c2,alpha1,alphamax,restart]

% global variables:
global parms;
% global countiter; % needed if restarting is periodic

% no bulletproofing or safeguarding
cgrestart = parms.cgrestart;

alpha = wolfelinesrch(fngrad,fn,xold,p);
xnew = xold + alpha*p;
gradnew = feval(fngrad,xnew);
% here is a periodic restarting test:
%if ((countiter > 0) & (rem(countiter, cgrestart) == 0))
%  pnew = -gradnew;
%  return;
%end
% here is a orthogonality test restart:
if (abs(gradnew'*gradold) >= cgrestart)
  pnew = -gradnew;
  return;
end
%beta = max((gradnew'*(gradnew))/(gradold'*gradold),0); % fletcher-reeves
beta = max((gradnew'*(gradnew-gradold))/(gradold'*gradold),0); % polak-ribiere
pnew = -gradnew + beta*p;

