function mcoef = chebyshev_to_monomial ( n, ccoef )

%*****************************************************************************80
%
%% chebyshev_to_monomial() converts from Chebyshev to monomial 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 ccoef(1:n+1): the Chebyshev coefficients of the polynomial.
% 
%  Output:
%
%    real mcoef(1:n+1): the monomial coefficients of the polynomial.
%
  if ( n < 0 )
    return
  end
%
%  Force ccoef to be a column vector!
%
  ccoef = ccoef(:);

  tp = 1.0;

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

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

  mcoef(n+1) = tp * mcoef(n+1);
  if ( 0 < n )
    mcoef(n) = tp * mcoef(n);
  end

  return
end
