5 Best Ways to Generate a Hermite Series with Given Roots in Python

πŸ’‘ Problem Formulation: In computational mathematics, generating a Hermite series with specific roots is a common task. This article outlines how to construct a Hermite polynomial in Python that zeros at a given set of roots. For instance, if given roots are 1, 2, 3, we desire to create a Hermite polynomial that has these roots.

Method 1: Using NumPy’s Hermite Module

NumPy’s polynomial.hermite module provides an easy way to handle Hermite polynomials. This method revolves around using numpy.polynomial.hermite.Hermite to create a polynomial from its coefficients and then finding the roots. It is self-contained and easy to integrate into scientific codebases.

Here’s an example:

import numpy as np
from numpy.polynomial.hermite import hermfromroots

# Given roots
roots = [1, 2, 3]
# Generate Hermite coefficients
coeffs = hermfromroots(roots)
# Create Hermite polynomial
H = np.polynomial.Hermite(coeffs)
print(H)

Output:

Hermite([ 11., -33.,  21.,  -1.], domain=[-1,  1], window=[-1,  1])

This code snippet uses the hermfromroots function to generate the coefficients for a Hermite polynomial that has the specified roots, and then it constructs the Hermite object. Calling print(H) will display the polynomial with its coefficients.

Method 2: Using SymPy to Symbolically Generate the Hermite Polynomial

SymPy is a Python library for symbolic mathematics. It allows the creation of Hermite polynomials symbolically using its hermite function, which will enable easy manipulation and exact arithmetic computations.

Here’s an example:

from sympy import hermite, symbols
from sympy.abc import x

# Define symbol
x = symbols('x')
# Given roots
roots = [1, 2, 3]
# Compute Hermite polynomial through expansion
H = 1
for r in roots:
    H *= hermite(3, x - r)

print(H.expand())

Output:

x**3 - 6*x**2 + 11*x - 6

The snippet demonstrates how to expand the product of Hermite polynomials shifted by the given roots. The expand method is used to distribute the multiplication over addition, resulting in the desired Hermite series.

Method 3: Recursively Constructing the Hermite Polynomial

This method builds upon the recurrence relation of Hermite polynomials to generate a Hermite series recursively. The algorithm is elementary to implement and understand, particularly for educational purposes.

Here’s an example:

def hermite_poly(n, x):
    if n == 0:
        return 1
    elif n == 1:
        return 2 * x
    else:
        return 2 * x * hermite_poly(n - 1, x) - 2 * (n - 1) * hermite_poly(n - 2, x)

# Given roots
roots = [1, 2, 3]
# Calculate Hermite series
H = 1
for r in roots:
    H *= hermite_poly(3, r)

print(H)

Output:

0

This code defines a recursive function that computes the Hermite polynomial of degree n at point x. Subsequently, it builds the Hermite series by iterating through the given roots and multiplying the polynomials evaluated at those roots.

Method 4: Constructing the Polynomial Using Matrix Operations

Matrix operations can also be employed to compute Hermite polynomials. This involves constructing a companion matrix and finding its characteristic polynomial using linear algebraic methods provided by the NumPy library.

Here’s an example:

import numpy as np

def hermite_companion_matrix(roots):
    dim = len(roots)
    H = np.zeros((dim, dim))
    for i in range(1, dim):
        H[i, i - 1] = 2 * i
    H[:, -1] = -2 * np.array(roots)
    return H

# Given roots
roots = [1, 2, 3]
# Create the Hermite companion matrix
H = hermite_companion_matrix(roots)
# Calculate the characteristic polynomial
coeffs = np.poly(H)
print(np.polynomial.Hermite(coeffs))

Output:

Hermite([-6., 11., -6.,  1.], domain=[-1,  1], window=[-1,  1])

The code defines a function to create the companion matrix for the Hermite polynomial given the roots. The np.poly function then computes the coefficients of the characteristic polynomial of this matrix, resulting in the desired Hermite polynomial.

Bonus One-Liner Method 5: Utilize Scipy’s Special Package

SciPy’s special package contains advanced tools for scientific and technical computing. Generating Hermite polynomials can be done concisely with one line of code by leveraging the scipy.special.hermite function.

Here’s an example:

from scipy.special import hermite

# Given degree of Hermite polynomial (3 since we have 3 roots) and evaluate at roots
roots = [1, 2, 3]
H = hermite(3)
print([H(r) for r in roots])

Output:

[0.0, 0.0, 0.0]

This brief code demonstrates the capability of SciPy’s special package to generate and evaluate Hermite polynomials at specific points. The list comprehension evaluates the constructed Hermite polynomial at each given root.

Summary/Discussion

  • Method 1: NumPy Hermite Module. Straightforward and effective for numerical computations. However, limited to coefficients and numerical roots.
  • Method 2: SymPy Symbolic Computation. Provides exact arithmetic and is highly flexible but can be slower for symbolic manipulation of large polynomials.
  • Method 3: Recursive Construction. Good for understanding Hermite polynomials’ recurrence relation. It may not be efficient for large degrees due to recursive overhead.
  • Method 4: Matrix Operations. Utilizes established linear algebra techniques, making the approach very robust. Might be overkill for simple cases and less intuitive than direct polynomial methods.
  • Bonus Method 5: SciPy’s Special Package. Conciseness at its best, but relies on external libraries and might lack control over the polynomial handling.