function [ x, error, total_iters ] = gmres ( x0, b, atv, params )

%*****************************************************************************80
%
%% gmres() implements the Saad-Schultz GMRES solver.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 April 2021
%
%  Author:
%
%    Tim Kelley
%
%  Reference:
%
%    Tim Kelley,
%    Iterative Methods for Linear and Nonlinear Equations,
%    SIAM, 2004,
%    ISBN: 0898713528,
%    LC: QA297.8.K45.
%
%  Input:
%
%    real X0(N): the initial iterate.
%
%    real B(N): the right hand side.
%
%    function ax = ATV ( X ): returns A * X.
%
%    real PARAMS(3): iteration control parameters.
%    params(1) = relative residual reduction factor
%    params(2) = max number of iterations
%    params(3) = reorthogonalization method
%      1 -- Brown/Hindmarsh condition (default)
%      2 -- Never reorthogonalize (not recommended)
%      3 -- Always reorthogonalize (not cheap!)
%
%  Output:
%
%    real X(N): the estimated solution.
%
%    real ERROR(total_iters): residual norms over the iteration.
%
%    integer TOTAL_ITERS: the number of iterations taken.
%
  n = length ( b );
  errtol = params(1);
  kmax = params(2);
  reorth = 1;
  if ( length ( params ) == 3 )
    reorth = params(3);
  end
  x = x0;

  h = zeros(kmax,kmax);
  v = zeros(n,kmax);
  c = zeros(kmax+1,1);
  s = zeros(kmax+1,1);
  if ( norm ( x ) ~= 0.0 )
    r = b - feval ( atv, x );
  else
    r = b;
  end
  rho = norm ( r );
  g = rho * eye ( kmax+1, 1 );
  errtol = errtol * norm ( b );
  error = [];
%
%  test for termination on entry
%
  error = [ error, rho ];
  total_iters = 0;
  if ( rho < errtol ) 
    return
  end

  v(:,1) = r / rho;
  beta = rho;
  k = 0;
%
% GMRES iteration
%
  while ( ( errtol < rho ) && ( k < kmax ) )

    k = k + 1;
    v(:,k+1) = feval ( atv, v(:,k) );
    normav = norm ( v(:,k+1) );
%
% Modified Gram-Schmidt
%
    for j = 1 : k
      h(j,k) = v(:,j)' * v(:,k+1);
      v(:,k+1) = v(:,k+1) - h(j,k) * v(:,j);
    end
    h(k+1,k) = norm ( v(:,k+1) );
    normav2 = h(k+1,k);
%
% Reorthogonalize?
%
    if ( ( reorth == 1 && normav + 0.001 * normav2 == normav) || reorth ==  3)
      for j = 1 : k
        hr = v(:,j)' * v(:,k+1);
        h(j,k) = h(j,k) + hr;
        v(:,k+1) = v(:,k+1) - hr * v(:,j);
      end
      h(k+1,k) = norm ( v(:,k+1) );
    end
%
%  watch out for happy breakdown 
%
    if ( h(k+1,k) ~= 0.0 )
      v(:,k+1) = v(:,k+1) / h(k+1,k);
    end
%
%  Form and store the information for the new Givens rotation
%
    if ( 1 < k )
      h(1:k,k) = givapp ( c(1:k-1), s(1:k-1), h(1:k,k), k-1 );
    end

    nu = norm ( h(k:k+1,k) );

    if ( nu ~= 0 )
      c(k) = h(k,k) / nu;
      s(k) = - h(k+1,k) / nu;
      h(k,k) = c(k) * h(k,k) - s(k) * h(k+1,k);
      h(k+1,k) = 0.0;
      g(k:k+1) = givapp ( c(k), s(k), g(k:k+1), 1 );
    end
%
%  Update the residual norm
%
    rho = abs ( g(k+1) );
    error = [ error, rho ];
  end
%
%  At this point either k > kmax or rho < errtol.
%  It's time to compute x and leave.
%
  y = h(1:k,1:k) \ g(1:k);
  total_iters = k;
  x = x0 + v(1:n,1:k) * y;

  return
end

