function vrot = givapp ( c, s, vin, k )
 
%*****************************************************************************80
%
%% givapp() applies K Givens rotations for the GMRES codes.
%
%  Discussion:
%
%    A Givens rotation matrix has the form
%      ( c -s )
%      ( s  c )
%    where C and S are the cosine and sine of some angle.
%
%    K Givens matrices and a vector V of length K+1 are defined.
%    The first matrix is applied to V(1:2).
%    The second matrix is applied to V(2:3), and so on.
%
%  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 C(K), S(K): a sequence defining K Givens rotation matrices.
%
%    real VIN(K+1): a vector to be transformed.
%
%    integer K: the number of Givens matrices.
%
%  Output:
%
%    VROT(K+1): the transformed vector.
%
  vrot = vin;

  for i = 1 : k
    w1 = c(i) * vrot(i) -        s(i)   * vrot(i+1);
    w2 = s(i) * vrot(i) + conj ( c(i) ) * vrot(i+1);
    vrot(i:i+1) = [ w1, w2 ];
  end

  return
end

