function ccoef = monomial_to_chebyshev ( n, mcoef )

%*****************************************************************************80
%
%% monomial_to_chebyshev() converts from monomial to Chebyshev form.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 February 2024
%
%  Author:
%
%    Original Fortran77 version by Fred Krogh.
%    This version by John Burkardt.
%
%  Input:
%
%    integer n: the order of the polynomial.
%
%    real mcoef(1:n+1): the monomial coefficients of the polynomial.
% 
%  Output:
%
%    real ccoef(1:n+1): the Chebyshev coefficients of the polynomial.
%
  if ( n < 0 )
    return;
  end
%
%  Force mcoef to be a column vector!.
%
  mcoef = mcoef(:);

  ccoef(1:n+1,1) = mcoef(1:n+1,1);

  tp = 0.5 ^ ( n - 1 );
  ccoef(n+1) = tp * ccoef(n+1);

  if ( n == 0 )
    return;
  end

  ccoef(n) = tp * ccoef(n);
  for j = n - 2 : -1 : 0
    tp = 2.0 * tp;
    ccoef(j+1) = tp * ccoef(j+1);
    ccoef(j+2) = 2.0 * ccoef(j+2);
    for i = j : n - 2
      ccoef(i+1) = ccoef(i+1) + ccoef(i+3);
    end
  end

  return
end
