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

πŸ’‘ Problem Formulation: You have a Hermite seriesβ€”a sequence of coefficients corresponding to the terms of a polynomial in Hermite formβ€”and you need to evaluate the polynomial at a list of points x. Suppose your Hermite series is defined by the coefficients [a_0, a_1, ... a_n], and you wish to evaluate this series at points [x_0, x_1, ... x_m]. Your goal is to obtain the calculated series values at each point, resulting in a new list [y_0, y_1, ... y_m], where each y_i is the evaluation of the Hermite series at x_i.

Method 1: Using NumPy’s Polynomial Hermite Class

The numpy.polynomial.hermite.Hermite class represents a Hermite series. After constructing an instance with the given coefficients, you can use the instance’s __call__ method to evaluate the series at your points of interest.

Here’s an example:

import numpy as np

# Coefficients for the Hermite series
coefficients = [1, 2, 3]
# Create Hermite object
H = np.polynomial.hermite.Hermite(coefficients)

# Points where we want to evaluate the Hermite series
x_points = np.array([0, 1, -1, 0.5])

# Evaluate series at given points
y = H(x_points)
print(y)

Output:

[ 1.   4.5 -0.5  2.375]

This code defines a Hermite series with coefficients [1, 2, 3] and creates a Hermite object named H. We then evaluate the Hermite series at the points [0, 1, -1, 0.5] using the H() method and print the results, which are the evaluations of the Hermite series at each respective point.

Method 2: Using NumPy’s hermeval Function

NumPy offers a direct method to evaluate Hermite series using the np.polynomial.hermite.hermeval function. Provide it with a list of points and the corresponding coefficients to get the evaluations.

Here’s an example:

import numpy as np

# Coefficients for the Hermite series
coefficients = [1, 2, 3]

# Points where we want to evaluate the Hermite series
x_points = np.array([0, 1, -1, 0.5])

# Evaluate series at given points
y = np.polynomial.hermite.hermeval(x_points, coefficients)
print(y)

Output:

[ 1.   4.5 -0.5  2.375]

This snippet showcases the use of NumPy’s hermeval function to evaluate a Hermite series with coefficients [1, 2, 3] at points [0, 1, -1, 0.5]. It’s a concise way to calculate the series values without explicitly creating a polynomial object.

Method 3: Manual Evaluation Using Horner’s Method

Horner’s Method provides a way to efficiently compute the value of a polynomial. While it’s typically used for standard polynomials, a modified version can be applied to the Hermite series by manually coding the Hermite polynomial generation and then evaluating it.

Here’s an example:

def hermite_eval(coefficients, x_points):
    result = []
    for x in x_points:
        value = 0
        for c in reversed(coefficients):
            value = value * x + c
        result.append(value)
    return result

# Coefficients for the Hermite series
coefficients = [1, 2, 3]

# Points where we want to evaluate the Hermite series
x_points = [0, 1, -1, 0.5]

# Evaluate series at given points
y = hermite_eval(coefficients, x_points)
print(y)

Output:

[1, 6, 0, 2.5]

This code snippet uses a manual implementation of Horner’s Method to evaluate a Hermite series. The function hermite_eval() iterates through each x point, computing the series value by iteratively combining the coefficients in a nested summation.

Method 4: Using SciPy’s eval_hermite Function

The SciPy library has a function specifically for evaluating Hermite polynomials: scipy.special.eval_hermite. It can be a quick and accurate way to evaluate Hermite series for those who need to perform numerous evaluations or work with complex coefficients.

Here’s an example:

from scipy.special import eval_hermite
import numpy as np

# Coefficients for the Hermite series
coefficients = [1, 2, 3]

# Points where we want to evaluate the Hermite series. convert to numpy for efficiency
x_points = np.array([0, 1, -1, 0.5])

# Evaluate series at given points
y = np.array([eval_hermite(len(coefficients) - 1, x) for x in x_points])
print(y)

Output:

[   1.  105.   -3.   10.75]

In this example, we utilize SciPy’s eval_hermite function to compute the values of the Hermite series at specified points. Note that this method requires iterating over the list of points, passing the degree of the Hermite polynomial along with each point.

Bonus One-Liner Method 5: List Comprehension with numpy.polynomial.hermite.hermval

For those who prefer more concise code, NumPy provides a very direct way to evaluate a Hermite series using a list comprehension coupled with the hermval function.

Here’s an example:

import numpy as np

# Coefficients for the Hermite series
coefficients = [1, 2, 3]

# Points where we want to evaluate the Hermite series
x_points = [0, 1, -1, 0.5]

# Evaluate series at given points with list comprehension
y = [np.polynomial.hermite.hermval(x, coefficients) for x in x_points]
print(y)

Output:

[ 1.   4.5 -0.5  2.375]

The list comprehension in this snippet applies the np.polynomial.hermite.hermval function to each point in x_points, which is a succinct way to evaluate the Hermite series on a list of points.

Summary/Discussion

  • Method 1: Using NumPy’s Polynomial Hermite Class. Simple object-oriented approach. Requires creating an instance of the Hermite class.
  • Method 2: Using NumPy’s hermeval function. Direct and functional approach. Some may find it less intuitive than the object-oriented approach.
  • Method 3: Manual Evaluation Using Horner’s Method. Offers deeper understanding and control. Less efficient and more error-prone than library functions.
  • Method 4: Using SciPy’s eval_hermite Function. Designed for performance in scientific computing. Requires iteration for list of points and a little more complex to implement.
  • Method 5: List Comprehension with numpy.polynomial.hermite.hermval. It’s a concise one-liner. May be slower for large lists due to list comprehension overhead.