torch.special.legendre_polynomial_p
function legendre_polynomial_p<S extends Shape>(x: Tensor<S, 'float32'>, n: number | Tensor, _options?: SpecialPolynomialOptions<S>): Tensor<S, 'float32'>Computes Legendre polynomial P_n(x).
The Legendre polynomials P_n(x) are orthogonal on [-1, 1] with constant weight (orthogonality with respect to Lebesgue measure). Fundamental to spherical harmonics and angular momentum analysis. Essential for:
- Quantum mechanics: spherical harmonics Y_ℓ^m ∝ P_ℓ^m (associated Legendre); angular momentum eigenfunctions; atomic/molecular orbitals
- Multipole expansions: gravitational/electrostatic potentials expanded in Legendre series (natural expansion for spherical symmetry)
- Geophysics: spherical harmonics for Earth's magnetic field, gravitational field, seismic analysis
- Numerical integration: Gauss-Legendre quadrature (optimal for polynomial integration on [-1, 1]; no weight function needed)
- Approximation on [-1, 1]: natural basis for problems on symmetric interval with no weight function
- Machine learning: spherical CNNs, graph neural networks on manifolds, spherical data (faces, molecules)
Spherical Symmetry: Legendre polynomials are "azimuthally symmetric" part (m=0) of associated Legendre polynomials. Angular momentum eigenfunctions require associated Legendre P_n^m, but standard P_n is fundamental building block.
Gauss-Legendre Quadrature: Roots of P_n are optimal quadrature nodes for polynomial integration on [-1, 1]. Most widely used quadrature rule in numerical analysis.
- Interval [-1, 1]: Natural domain; no weight function (constant weight = 1)
- Gauss-Legendre quadrature: Roots are optimal for numerical integration; most widely used quadrature rule
- Boundary behavior: P_n(1) = 1, P_n(-1) = (-1)^n; bounded and simple boundary values
- Parity: P_n(-x) = (-1)^n P_n(x) (even if n even, odd if n odd)
- Orthogonality weight: Constant weight 1 (unlike others with nontrivial weights)
- Recurrence: Simple three-term with coefficients (n+1), (2n+1), n; numerically stable
- Spherical harmonics: Associated Legendre P_n^m forms basis for angular part of solutions in spherical coords
- Multipole expansion: Natural basis for problems with spherical symmetry (Laplace, Poisson equations)
- Domain [-1, 1]: Extrapolation outside [−1, 1] reasonable (polynomial growth, unlike Chebyshev oscillations)
- Associated Legendre for quantum: Use associated Legendre P_n^m(x) for non-azimuthal spherical harmonics
- Quadrature nodes: Gauss-Legendre nodes are roots of P_n; must be computed separately (not evaluation)
Parameters
xTensor<S, 'float32'>- Input tensor with values in [-1, 1] (natural domain; symmetric interval)
nnumber | Tensor- Polynomial degree (non-negative integer). Can be scalar or Tensor
_optionsSpecialPolynomialOptions<S>optional
Returns
Tensor<S, 'float32'>– Tensor with P_n(x) valuesExamples
// Basic evaluation
const x = torch.linspace(-1, 1, 5);
const P_0 = torch.special.legendre_polynomial_p(x, 0); // [1, 1, 1, 1, 1]
const P_1 = torch.special.legendre_polynomial_p(x, 1); // x
const P_2 = torch.special.legendre_polynomial_p(x, 2); // (3*x^2 - 1) / 2
// Spherical harmonics basis (m=0 case)
const cos_theta = torch.linspace(-1, 1, 100); // cos(θ) in spherical coords
const ell_max = 4; // Maximum angular momentum
const spherical_basis = [];
for (let ell = 0; ell <= ell_max; ell++) {
// Y_ell^0 ∝ P_ell(cos_theta) (azimuthally symmetric)
spherical_basis.push(torch.special.legendre_polynomial_p(cos_theta, ell));
}
// Forms complete orthogonal basis on sphere (with azimuthal dependence for m ≠ 0)
// Electrostatic/gravitational multipole expansion
const r = torch.linspace(0, 10, 50); // Radial distance
const cos_angle = torch.linspace(-1, 1, 50); // cos(angle from z-axis)
// Multipole potential Φ ∝ sum_n (a_n / r^{n+1}) * P_n(cos(angle))
const P_2_coeff = torch.special.legendre_polynomial_p(cos_angle, 2); // Quadrupole term
// Physical potential naturally expanded in Legendre series
// Gauss-Legendre quadrature
// Roots of P_n give optimal quadrature nodes (no explicit computation shown)
// Integration on [-1, 1]: ∫_-1^1 f(x) dx ≈ sum_i w_i f(x_i)
// x_i = roots of P_n(x), w_i = 2 / ((1 - x_i^2) * (P'_n(x_i))^2)
// Approximation on [-1, 1]: function expansion
const x_approx = torch.linspace(-1, 1, 100);
const f_target = x_approx.pow(4); // Target function to approximate
const n_terms = 5;
const legendre_series = [];
for (let n = 0; n < n_terms; n++) {
legendre_series.push(torch.special.legendre_polynomial_p(x_approx, n));
}
// Project f onto Legendre basis; each term P_n has integral ∫ f*P_n*dx / ∫ P_n^2*dx
// Boundary behavior
const x_at_1 = torch.tensor([1.0]);
const x_at_neg1 = torch.tensor([-1.0]);
const P_5_at_1 = torch.special.legendre_polynomial_p(x_at_1, 5); // P_5(1) = 1
const P_5_at_neg1 = torch.special.legendre_polynomial_p(x_at_neg1, 5); // P_5(-1) = (-1)^5 = -1
// Boundary values always ±1 (no explosions unlike Chebyshev extrapolation)
// Symmetry and parity
const x_sym = torch.tensor([0.3, -0.3]);
const P_3 = torch.special.legendre_polynomial_p(x_sym, 3);
// P_3(-0.3) = -P_3(0.3) since P_n has parity (-1)^nSee Also
- PyTorch torch.special.legendre_polynomial_p()
- torch.special.chebyshev_polynomial_t - Chebyshev T_n on [-1, 1] with 1/√(1-x²) weight
- torch.special.hermite_polynomial_h - Hermite on (-∞, ∞) with exp(-x²) weight
- torch.special.laguerre_polynomial_l - Laguerre on [0, ∞) with exp(-x) weight