bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / special / _orthogonal / roots_legendre
function
scipy.special._orthogonal:roots_legendre
source: build-install/usr/lib/python3.14/site-packages/scipy/special/_orthogonal.py:3333
Signature
def roots_legendre(n, mu = False)Summary
Gauss-Legendre quadrature.
Extended Summary
Compute the sample points and weights for Gauss-Legendre quadrature [GL]. The sample points are the roots of the nth degree Legendre polynomial . These sample points and weights correctly integrate polynomials of degree or less over the interval with weight function . See 2.2.10 in [AS] for more details.
Parameters
n: intquadrature order.
mu: bool, optionalIf True, return the sum of the weights, optional.
Returns
x: ndarraySample points.
w: ndarrayWeights.
mu: floatSum of the weights.
Examples
import numpy as np from scipy.special import roots_legendre, eval_legendre roots, weights = roots_legendre(9)✓
roots weights✗
array([0.08127, 0.18065, 0.26061, 0.31235, 0.33024, 0.31235, 0.26061,
0.18065, 0.08127])
Verify that we have the roots by evaluating the degree 9 Legendre
polynomial at ``roots``. All the values are approximately zero:
eval_legendre(9, roots)
✗array([-0., -0., 0., 0., 0., -0., -0., 0., -0.])Here we'll show how the above values can be used to estimate the integral from 1 to 2 of f(t) = t + 1/t with Gauss-Legendre quadrature [GL]_. First define the function and the integration limits.
def f(t): return t + 1/t a = 1 b = 2✓
t = (b - a)/2 * roots + (a + b)/2
✓(b - a)/2 * f(t).dot(weights)
✗np.float64(2.1931471805599276)Compare that to the exact result, which is 3/2 + log(2):
1.5 + np.log(2)
✗np.float64(2.1931471805599454)
See also
- numpy.polynomial.legendre.leggauss
- scipy.integrate.fixed_quad
Aliases
scipy.special.p_roots