function dpdx = lagrange_basis_deriv ( npol, ipol, xpol, xval )

%*****************************************************************************80
%
%% lagrange_basis_deriv(): derivative of the IPOL-th 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.
%
%    real XVAL, the evaluation point.
%
%  Output:
%
%    real DPDX, the derivative of the IPOL-th 
%    Lagrange polynomial at XVAL.
%

%
%  Make sure IPOL is legal.
%
  if ( ipol < 1 || npol < ipol )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lagrange_basis_deriv(): Fatal error!\n' );
    fprintf ( 1, '  1 <= IPOL <= NPOL is required.\n' );
    error ( 'lagrange_basis_deriv(): Fatal error!\n' );
  end
%
%  Check that the abscissas are distinct.
%
  if ( ~ r8vec_is_distinct ( npol, xpol ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lagrange_basis_deriv(): Fatal error!\n' );
    fprintf ( 1, '  Two or more entries of XPOL are equal:\n' );
    error ( 'lagrange_basis_deriv(): Fatal error!\n' );
  end
%
%  Evaluate the derivative, which can be found by summing up the result
%  of differentiating one factor at a time, successively.
%
  dpdx = 0.0;

  for i = 1 : npol

    if ( i ~= ipol )

      p = 1.0;
      for j = 1 : npol

        if ( j == i )
          p = p                      / ( xpol(ipol) - xpol(j) );
        elseif ( j ~= ipol )
          p = p * ( xval - xpol(j) ) / ( xpol(ipol) - xpol(j) );
        end

      end

      dpdx = dpdx + p;

    end

  end

  return
end
