function pval = lagrange_basis_value ( npol, ipol, xpol, xval )

%*****************************************************************************80
%
%% lagrange_basis_value() evaluates a Lagrange basis polynomial.
%
%  Discussion:
%
%    Given NPOL distinct abscissas, XPOL(1:NPOL), the i-th Lagrange
%    basis polynomial L(i)(x) is the polynomial of degree NPOL - 1 
%    which is 1 at XPOL(i) and 0 at the NPOL - 1 other abscissas.
%
%    A formal representation is:
%
%      L(i)(x) = Product ( 1 <= j <= NPOL, j /= i )
%       ( x - X(j) ) / ( X(i) - X(j) )
%
%  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.
%
%    real XVAL, the evaluation point.
%
%  Output:
%
%    real PVAL, the value of the IPOL-th Lagrange basis polynomial at XVAL.
%

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

  for j = 1 : npol

    if ( j ~= ipol )

      pval = pval * ( xval - xpol(j) ) / ( xpol(ipol) - xpol(j) );

    end

  end

  return
end
