function A = companion_chebyshev ( p )

%*****************************************************************************80
%
%% companion_chebyshev() returns the Chebyshev basis companion matrix for a polynomial.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 March 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    John Boyd,
%    Solving Transcendental Equations,
%    The Chebyshev Polynomial Proxy and other Numerical Rootfinders,
%    Perturbation Series, and Oracles,
%    SIAM, 2014,
%    ISBN: 978-1-611973-51-8,
%    LC: QA:353.T7B69
%
%  Input:
%
%    real p(n+1): the polynomial coefficients, in order of increasing degree.
%
%  Output:
%
%    real A(n,n): the companion matrix.
%
  np1 = length ( p );
  n = np1 - 1;

  A = zeros ( n, n );

  A(1,2) = 1.0;

  for i = 2 : n - 1
    A(i,i-1) = 0.5;
    A(i,i+1) = 0.5;
  end
  for j = 1 : n
    A(n,j) = - 0.5 * p(j) / p(n+1);
  end
  A(n,n-1) = A(n,n-1) + 0.5;

  return
end

