scipy.special.eval_legendre#

scipy.special.eval_legendre(n, x, out=None) = <ufunc 'eval_legendre'>#

Evaluate Legendre polynomial at a point.

The Legendre polynomials can be defined via the Gauss hypergeometric function \({}_2F_1\) as

\[P_n(x) = {}_2F_1(-n, n + 1; 1; (1 - x)/2).\]

When \(n\) is an integer the result is a polynomial of degree \(n\). See 22.5.49 in [AS] (or equivalently [DLMF]) for details.

Parameters:
narray_like

Degree of the polynomial. If not an integer, the result is determined via the relation to the Gauss hypergeometric function.

xarray_like

Points at which to evaluate the Legendre polynomial

outndarray, optional

Optional output array for the function values

Returns:
Pscalar or ndarray

Values of the Legendre polynomial

See also

roots_legendre

roots and quadrature weights of Legendre polynomials

legendre

Legendre polynomial object

hyp2f1

Gauss hypergeometric function

numpy.polynomial.legendre.Legendre

Legendre series

References

[AS]

Milton Abramowitz and Irene A. Stegun, eds. Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables. New York: Dover, 1972.

[DLMF]

NIST Digital Library of Mathematical Functions, https://dlmf.nist.gov/15.9.E7

Examples

>>> import numpy as np
>>> from scipy.special import eval_legendre
>>> import matplotlib.pyplot as plt

Evaluate the zero-order Legendre polynomial at x = 0

>>> eval_legendre(0, 0)
1.0

Evaluate the first-order Legendre polynomial between -1 and 1

>>> x = np.linspace(-1, 1, 5)  # Domain of Legendre polynomials
>>> eval_legendre(1, x)
array([-1. , -0.5,  0. ,  0.5,  1. ])

Evaluate Legendre polynomials of order 0 through 4 at x = 0

>>> eval_legendre(range(5), 0)
array([ 1.   ,  0.   , -0.5  ,  0.   ,  0.375])

Plot Legendre polynomials of order 0 through 4

>>> x = np.linspace(-1, 1, 800)
>>> fig, ax = plt.subplots(1, 1, constrained_layout=True)
>>> for n in range(5):
...     y = eval_legendre(n, x)
...     ax.plot(x, y, label=f'{n = }')
>>> ax.grid()
>>> ax.set_title("Legendre Polynomials")
>>> ax.set_xlabel("$x$")
>>> ax.set_ylabel("$P_n(x)$")
>>> ax.legend(loc='upper left', bbox_to_anchor=(1, 1), shadow=True)
>>> plt.show()
../../_images/scipy-special-eval_legendre-1.png