function A = companion_gegenbauer ( p, alpha )

%*****************************************************************************80
%
%% companion_gegenbauer() returns the Gegenbauer basis companion matrix for a polynomial.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 March 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    John Boyd,
%    Solving Transcendental Equations,
%    The gegenbauer 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.
%
%    real alpha: the Gegenbauer parameter.
%
%  Output:
%
%    real A(n,n): the companion matrix.
%
  np1 = length ( p );
  n = np1 - 1;

  A = zeros ( n, n );

  A(1,2) = 0.5 / alpha;

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

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

  return
end

