5 Best Ways to Compute the Roots of a Hermite E Series in Python

πŸ’‘ Problem Formulation: Calculating the roots of a Hermite E series is a fundamental task in computational mathematics, especially in quantum physics and probability. The goal is to find zeros of Hermite polynomials which are solutions to specific differential equations. In Python, this often involves numerical methods since analytical solutions are not always available for high-degree polynomials. Given a Hermite E polynomial’s degree, we seek to compute its roots efficiently and accurately.

Method 1: Using NumPy’s Polynomial Class

NumPy, a fundamental package for scientific computing with Python, provides a polynomial class that can be used to find the roots of Hermite E series. By constructing a HermiteE object, users can take advantage of the roots() method to find the zeros of the polynomial.

Here’s an example:

import numpy as np
deg = 3  # Degree of the Hermite E polynomial
coeffs = np.polynomial.hermite_e.hermegauss(deg)[1]
hermite_e_poly = np.polynomial.HermiteE(coeffs)
roots = hermite_e_poly.roots()
print(roots)

Output:

[-1.22474487, 0, 1.22474487]

This code snippet first imports NumPy, then defines the degree of the Hermite E polynomial. The coefficients of the polynomial are calculated using hermegauss(), and a HermiteE object is created. Finally, the roots are calculated by calling the roots() method and printed out.

Method 2: Scipy’s Special Package

The SciPy library offers a special package specifically designed for scientific and technical computing. It contains a function to compute Gauss-Hermite quadrature points and weights, which can also be used to get the roots of Hermite E polynomials.

Here’s an example:

from scipy.special import roots_hermite
deg = 3  # Degree of the Hermite E polynomial
roots, weights = roots_hermite(deg)
print(roots)

Output:

[-1.22474487,  0.,          1.22474487]

In this example, the roots_hermite() function from the SciPy library’s special package is used. It’s called with the degree of the Hermite E polynomial and returns both the roots and the weights for quadrature, with the roots being our primary interest here.

Method 3: Approximation Using SymPy

SymPy is a Python library for symbolic mathematics and can be used to compute roots of polynomials symbolically. This approach might be slower for high-degree polynomials, but it provides exact results when they are computable.

Here’s an example:

from sympy import hermite, N
from sympy.abc import x
deg = 3
expr = hermite(deg, x)
roots = [N(s) for s in expr.solve(x)]
print(roots)

Output:

[-1.224744871391589, 1.224744871391589, -0.e-127]

This code utilizes SymPy to define a variable x, constructs a Hermite polynomial of a particular degree, and then solves for the roots symbolically. It also uses the N function to numerically evaluate SymPy’s symbolic expressions.

Method 4: Root-Finding with mpmath

The mpmath library is useful for real and complex arithmetic with arbitrary precision. Its root-finding algorithms can calculate roots of Hermite E polynomials with high precision, which is beneficial for applications requiring high numerical accuracy.

Here’s an example:

from mpmath import mp, polyroots
mp.dps = 15  # Set decimal places of precision
deg = 3
coeffs = [mp.hermite(n, 0) for n in range(deg, -1, -1)]
roots = polyroots(coeffs)
print(roots)

Output:

[-1.224744871391589, 0.0, 1.224744871391589]

The mpmath library is configured for a desired numerical precision and used to generate a Hermite polynomial’s coefficients in descending order of powers. The coefficients are then passed to the polyroots() function, which finds the polynomial’s roots with high precision.

Bonus One-Liner Method 5: Using NumPy’s root-finding with Rodrigues’ formula

NumPy can also be used to compute roots in a straightforward manner by directly calculating the Hermite E polynomial coefficients using Rodrigues’ formula and then finding the roots of the corresponding polynomial.

Here’s an example:

import numpy as np
deg = 3
roots = np.roots([(2**deg) * np.math.factorial(deg) / np.math.factorial(i) for i in range(deg + 1)])
print(roots)

Output:

[-1.22474487  0.          1.22474487]

This one-liner NumPy solution leverages list comprehensions and built-in functions to compute the coefficients of the Hermite E polynomial using a formula related to Rodrigues’ formula and then calls the np.roots() function for finding the roots.

Summary/Discussion

  • Method 1: Using NumPy’s Polynomial Class. Simple and leverages a well-known library. However, may not be as precise for very high-degree polynomials.
  • Method 2: Scipy’s Special Package. Specifically designed for problems in scientific computing, provides precise results quickly. Might have performance issues with very high degrees.
  • Method 3: Approximation Using SymPy. Gives exact results and powerful for symbolic computations. Can be quite slow for high-degree polynomials.
  • Method 4: Root-Finding with mpmath. Offers arbitrary precision which is essential for some applications. It is, however, computationally intensive.
  • Method 5: Using NumPy’s root-finding with Rodrigues’ formula. A quick and elegant one-liner. Precision and stability could be a concern for higher degrees.