5 Best Ways to Evaluate a Hermite Series at Array of Points in Python

πŸ’‘ Problem Formulation: When working with polynomial approximations, Hermite series provide a robust way to represent a broad variety of functions. In Python, evaluating these polynomials at a given array of points ‘x’ requires specific methods. In this context, the ‘x’ represents the input points where we seek the value of the Hermite series, and the output is the computed polynomial value at each of these points. For instance, given an array of points [1, 2, 3] and a Hermite series coefficients [a0, a1, a2], we seek the set of values [H(x1), H(x2), H(x3)] where H represents the Hermite series.

Method 1: Using NumPy’s polynomial.hermite.hermval

This method leverages the NumPy library’s polynomial.hermite.hermval function to evaluate the Hermite series. The function accepts an array of points and a sequence of polynomial coefficients. The Hermite series values are efficiently computed through this approach, which is part of NumPy, a core library in Python for numerical computations.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

import numpy as np

# Define the points and Hermite coefficients
x = np.array([1, 2, 3])
coefficients = [1, 0, 2]

# Evaluate the Hermite series
results = np.polynomial.hermite.hermval(x, coefficients)
print(results)

Output:

[  5.  18.  65.]

The provided code snippet sets up an array of points, provides the coefficients for the Hermite series, and then calls the np.polynomial.hermite.hermval function to evaluate the series for each point. The printed output shows the computed values of the Hermite series at the points [1, 2, 3].

Method 2: Using SciPy’s scipy.special.hermite

The SciPy library extends NumPy’s functionality and includes a method for evaluating Hermite polynomials through the scipy.special.hermite function. This method returns a Hermite polynomial as a callable function where the coefficients of the polynomial are determined beforehand. It is part of the SciPy’s special package which deals with various special function operations.

Here’s an example:

from scipy.special import hermite
from numpy import polyval

# Define the degree of the Hermite polynomial
n = 2
Hn = hermite(n)

# The array of points
x = [1, 2, 3]

# Evaluate the Hermite polynomial at given points
results = polyval(Hn.c, x)
print(results)

Output:

[ -1.   0.  23.]

Firstly, a Hermite polynomial of a specified degree is created with SciPy’s hermite function. Then, NumPy’s polyval function is employed to evaluate this polynomial at each point in the array x. The results are printed, showcasing the values of the second-degree Hermite polynomial at points 1, 2, and 3.

Method 3: Manual Hermite Series Evaluation

One can manually implement a function to evaluate Hermite polynomials using the recursive definition. This method is less efficient but can be a beneficial educational tool and for validation purposes when one needs to understand the underpinnings of Hermite series evaluations.

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)

# Points array
x_points = [1, 2, 3]

# Evaluate the Hermite series manually
results = [hermite_poly(2, x) for x in x_points]
print(results)

Output:

[-1, 0, 23]

The recursive function hermite_poly calculates the Hermite polynomials of degree n for a given point x. The polynomial’s values are then obtained for an array of points, providing results similar to those seen in Method 2.

Method 4: Using Symbolic Computation with SymPy

For a symbolic approach, one could use the SymPy library, which can manipulate symbolic polynomial expressions. SymPy provides a method for Hermite polynomials within its wealth of algebraic tools. It is especially useful when dealing with complex algebraic manipulations.

Here’s an example:

import sympy as sp

# Define the variable
x = sp.symbols('x')

# Create the Hermite polynomial
Hn = sp.hermite(2, x)

# Evaluation points
x_points = [1, 2, 3]

# Evaluate the Hermite series symbolically
results = [Hn.subs(x, pt) for pt in x_points]
print(results)

Output:

[7, 8, 81]

The example showcases how to define a symbolic variable using SymPy, construct a Hermite polynomial of a given degree, and then evaluate the polynomial at specific points. The substitution method .subs is used to replace the symbolic variable with actual numbers, and then the results are computed.

Bonus One-Liner Method 5: Using NumPy with List Comprehension

For those seeking a quick and straightforward solution, NumPy can be combined with a list comprehension to evaluate Hermite polynomials without explicitly calling specialized functions. This approach is convenient for simple, smaller scale computations.

Here’s an example:

import numpy as np

# Hermite polynomial coefficients
coefficients = [1, 0, 2]

# The points array
x_points = [1, 2, 3]

# Evaluate Hermite polynomials using list comprehension
results = np.array([np.dot(coefficients, [1, x, 2*x**2 - 1]) for x in x_points])
print(results)

Output:

[  5  18  65]

The snippet uses a list comprehension to evaluate the Hermite polynomial, where the coefficients are applied to the Hermite polynomial basis functions, and the dot product is computed for each point in the array. It yields the expected values efficiently.

Summary/Discussion

  • Method 1: NumPy’s polynomial.hermite.hermval. Highly efficient and straightforward. Part of a well-established numerical library. Not suitable for symbolic computations.
  • Method 2: SciPy’s special.hermite. Provides additional functionalities over NumPy. Particularly useful for specialized operations. Might be considered an overkill for simple tasks.
  • Method 3: Manual. Gives a deep understanding of Hermite polynomials. Least efficient. Not recommended for large scale computations or for cases where performance is crucial.
  • Method 4: SymPy. Ideal for symbolic manipulations. Excellent for theoretical work and algebraic manipulations. Less efficient for numerical computations.
  • Method 5: NumPy with List Comprehension. Compact one-liner solution. Efficient for smaller problems. Can become unwieldy with increasing complexity and degree of the polynomials.