function value = cheb ( coefs , X )

%*****************************************************************************80
%
% MATLAB version of SUBROUTINE CHEB on page 258 of conte/deboor
%**************   I N P U T   ************
%  coefs  the coefficients in the Chebyshev form of a polynomial of 
%     degree < length(coefs)
%  X, points where the polynomial is to be evaluated.
%**************  O U T P U T  ************
%  value  the value at  X  of the polynomial specified by  coefs.
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
  NTERMS = length(coefs);
  sizeX = size(X);

  if NTERMS == 1
    value = repmat(coefs,sizeXp); 
    return
  end

  TWOX = 2*X;
  PREV2 = 0;
  PREV = repmat(coefs(end),sizeX);
  for k=NTERMS-1:-1:2
    value = coefs(k) + TWOX.*PREV - PREV2;
    PREV2 = PREV;
    PREV = value;
  end
  value = coefs(1) + X.*PREV - PREV2;

  return
end
