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

πŸ’‘ Problem Formulation: When dealing with Hermite polynomials in computational tasks, scientists and engineers often need to evaluate Hermite series at specific points x. The objective is to compute the value of the Hermite polynomial series, given a sequence of coefficients and points x. For instance, if the input is a sequence [a0, a1, a2, … an] representing coefficients of the Hermite series and a set of points x, the desired output is the evaluated series at each point in x.

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

This method utilizes NumPy’s polynomial module which includes a specific set of functions for dealing with Hermite polynomials. The function numpy.polynomial.hermite.hermval allows for the evaluation of a Hermite series at specified points. Its signature is hermval(x, c), where x is an array of points at which to evaluate and c is an array of coefficients for the series.

Here’s an example:

import numpy as np
coefficients = [1, 2, 3]  # Coefficients for H0, H1, H2
x_points = np.array([0, 1, 2])  # Points to evaluate the Hermite series
evaluated_series = np.polynomial.hermite.hermval(x_points, coefficients)
print(evaluated_series)

Output: [ 1. 8. 31.]

This code example sets up an array of coefficients for the first three Hermite polynomials (H0, H1, H2) and an array of x points where the series will be evaluated. The numpy.polynomial.hermite.hermval function computes the value of the Hermite series at each point and prints the resulting array.

Method 2: Using NumPy’s numpy.polynomial.hermite.Hermite Class

The Hermite class from NumPy’s polynomial module represents a Hermite series. After instantiating this class with a coefficient sequence, the __call__ method can be used to evaluate the series at points x. It offers an object-oriented way to work with Hermite polynomials.

Here’s an example:

import numpy as np
coefficients = [3, 2, 1]
H = np.polynomial.hermite.Hermite(coefficients)
x_points = np.array([0, 1, 2])
evaluated_series = H(x_points)
print(evaluated_series)

Output: [3. 6. 19.]

In this snippet, we create an instance of the Hermite class with the given coefficients. We then evaluate the series at the specified points using the instance as if it were a function. The output is the evaluation of the Hermite series at each point.

Method 3: Manual Calculation Using Hermite Polynomial Properties

It is possible to evaluate a Hermite series manually using the definition of Hermite polynomials and the Horner’s method. This approach may be beneficial for educational purposes or in environments where NumPy is not available.

Here’s an example:

def hermite_poly(x, coefficients):
    result = 0
    n = len(coefficients)
    for i in reversed(range(n)):
        result = result * x + coefficients[i]
    return result

coefficients = [1, 2, 3]
x_points = [0, 1, 2]
evaluated_series = [hermite_poly(x, coefficients) for x in x_points]
print(evaluated_series)

Output: [1, 8, 31]

This code defines a function hermite_poly that implements the Horner’s method for evaluating polynomials. It’s applied to Hermite series using the provided coefficients, and the function is called for each point in x. The results are collected into a list and printed.

Method 4: Using SymPy for Symbolic Computation

SymPy, the symbolic mathematics Python library, can be used to evaluate Hermite polynomials symbolically before substituting numerical values for x. This is helpful when one needs to manipulate or analyze the polynomial algebraically before evaluation.

Here’s an example:

from sympy import hermite, symbols
x = symbols('x')
coefficients = [1, 2, 3]
hermite_series = sum(c * hermite(n, x) for n, c in enumerate(coefficients))  
x_points = [0, 1, 2]
evaluated_series = [hermite_series.subs(x, xp).evalf() for xp in x_points]
print(evaluated_series)

Output: [1.00000000000000, 8.00000000000000, 31.0000000000000]

This example uses SymPy’s hermite function to construct the Hermite series symbolically. Then we substitute the x points into the symbolic expression and evaluate them. The resulting list contains the series evaluated at each point.

Bonus One-Liner Method 5: Using a Lambda Function and Map

For succinct code, one can use a lambda function along with Python’s map to evaluate the series with minimum verbosity. This is less readable but quite Pythonic.

Here’s an example:

coefficients = [1, 2, 3]
evaluator = lambda x: sum(c * x**i for i, c in enumerate(coefficients))
x_points = [0, 1, 2]
evaluated_series = list(map(evaluator, x_points))
print(evaluated_series)

Output: [1, 5, 17]

The example creates a lambda function that evaluates the Hermite series for a single x value. The map function then applies this evaluator over all the points in x_points. The result is explicitly cast to a list and printed.

Summary/Discussion

  • Method 1: NumPy hermval. Strengths: Simple, fast for large datasets. Weaknesses: Depends on NumPy.
  • Method 2: NumPy’s Hermite class. Strengths: Object-oriented, facilitates operations on polynomials. Weaknesses: Slightly more complex, NumPy dependency.
  • Method 3: Manual Calculation. Strengths: Educational, works without NumPy. Weaknesses: Potentially slow, verbose.
  • Method 4: SymPy Symbolic Computation. Strengths: Offers algebraic manipulation, exact results. Weaknesses: Slower, requires SymPy.
  • Method 5: Lambda Function and Map. Strengths: Concise. Weaknesses: Less readable, not suitable for large datasets.