function b = r85_mv ( n, a, x )

%*****************************************************************************80
%
%% R85_MV multiplies a R85 matrix times a vector.
%
%  Discussion:
%
%    The R85 storage format represents a pentadiagonal matrix as a 5 
%    by N array, in which each row corresponds to a diagonal, and 
%    column locations are preserved.  Thus, the original matrix is 
%    "collapsed" vertically into the array.
%
%  Example:
%
%    Here is how a R85 matrix of order 6 would be stored:
%
%       *   *  A13 A24 A35 A46
%       *  A12 A23 A34 A45 A56
%      A11 A22 A33 A44 A55 A66
%      A21 A32 A43 A54 A65  *
%      A31 A42 A53 A64  *   *
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 August 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the linear system.
%
%    Input, real A(5,N), the R85 matrix.
%
%    Input, real X(N,1), the vector to be multiplied by A.
%
%    Output, real B(N,1), the product A * x.
%
  x = x(:);
  b = zeros ( n, 1 );
%
%  MATLAB's indexing rules can be maddening.
%
  for i = 1 : n
    b(i) = a(3,i) * x(i);
  end

  for i = 3 : n
    b(i) = b(i) + a(1,i) * x(i-2);
  end

  for i = 2 : n
    b(i) = b(i) + a(2,i) * x(i-1);
  end

  for i = 1 : n - 1
    b(i) = b(i) + a(4,i) * x(i+1);
  end 

  for i = 1 : n - 2
    b(i) = b(i) + a(5,i) * x(i+2);
  end

  return
end

