function pcof = lagrange_basis_coef ( npol, ipol, xpol )

%*****************************************************************************80
%
%% lagrange_basis_coef() returns the coefficients of a Lagrange basis polynomial.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 July 2025
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer NPOL, the number of abscissas.
%    NPOL must be at least 1.
%
%    integer IPOL, the index of the polynomial to evaluate.
%    IPOL must be between 1 and NPOL.
%
%    real XPOL(NPOL), the abscissas of the
%    Lagrange polynomials.  The entries in XPOL must be distinct.
%
%  Output:
%
%    real PCOF(1:NPOL), the standard polynomial
%    coefficients of the IPOL-th Lagrange polynomial:
%      L(IPOL)(X) = SUM ( 0 <= I <= NPOL-1 ) PCOF(I+1) * X^I
%

%
%  Make sure IPOL is legal.
%
  if ( ipol < 1 || npol < ipol )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lagrange_basis_coef(): Fatal error!\n' );
    fprintf ( 1, '  1 <= IPOL <= NPOL is required.\n' );
    error ( 'lagrange_basis_coef(): Fatal error!' );
  end
%
%  Check that the abscissas are distinct.
%
  if ( ~ r8vec_is_distinct ( npol, xpol ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lagrange_basis_coef(): Fatal error!\n' );
    fprintf ( 1, '  Two or more entries of XPOL are equal:\n' );
    error ( 'lagrange_basis_coef(): Fatal error!' );
  end

  pcof(1) = 1.0;
  pcof(2:npol) = 0.0;

  indx = 0;

  for i = 1 : npol

    if ( i ~= ipol )

      indx = indx + 1;

      for j = indx : -1 : 0

        pcof(j+1) = - xpol(i) * pcof(j+1) / ( xpol(ipol) - xpol(i) );

        if ( 0 < j )
          pcof(j+1) = pcof(j+1) + pcof(j) / ( xpol(ipol) - xpol(i) );
        end

      end

    end

  end

  return
end
