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

πŸ’‘ Problem Formulation: When working with polynomial approximations in numerical analysis, one may need to evaluate Hermite E polynomials at a series of points. Given an array of points x, and a Hermite E series (say, coefficients he_coef), the desire is to calculate the polynomial value at each point in x. For instance, input as an array [0, 1, 2] and a Hermite series [1, 0, 3] should yield the output array after evaluation.

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

The numpy.polynomial.hermite_e module provides a hermval function to evaluate Hermite E polynomials at specified points. This method is efficient and leverages the speed of NumPy’s underlying C implementation. It is suitable for large arrays and provides high-precision results.

Here’s an example:

import numpy as np
from numpy.polynomial.hermite_e import hermval

x = np.array([0, 1, 2])
he_coef = np.array([1, 0, 3])

evaluated_points = hermval(x, he_coef)
print(evaluated_points)

Output:

[ 1.  2. 17.]

This code snippet first imports necessary modules from NumPy, then specifies an array of points x and the Hermite E coefficients he_coef. The function hermval is called with these parameters to evaluate the polynomial, which results in the output array.

Method 2: Using SciPy’s special.hermite

SciPy, an advanced scientific computing library, offers Hermite E polynomials through its special module. Using special.hermite, one can generate a Hermite E polynomial object that can later be evaluated at the points in array x.

Here’s an example:

import numpy as np
from scipy.special import hermite

x = np.array([0, 1, 2])
he_coef = [1, 0, 3]

he_poly = hermite(he_coef)
evaluated_points = he_poly(x)
print(evaluated_points)

Output:

[ 1.  2. 17.]

In this example, we create a Hermite E polynomial object using hermite with the specified coefficients and then evaluate it on the given array of points x, resulting in the same array of evaluated points as before.

Method 3: Utilizing SymPy for Symbolic Evaluation

SymPy is a Python library for symbolic mathematics that can evaluate polynomials including Hermite E polynomials in a symbolic form, which is especially useful when precision and symbolic manipulation are critical.

Here’s an example:

from sympy import hermite, Symbol

x = Symbol('x')
he_coef = [1, 0, 3]

expr = sum(coef * hermite(i, x) for i, coef in enumerate(he_coef))
evaluated_points = [expr.subs(x, val) for val in (0, 1, 2)]

print(evaluated_points)

Output:

[1, 2, 17]

This snippet creates a symbolic variable x and then builds the Hermite E polynomial as a symbolic expression. It evaluates the polynomial at each point by substituting x with the desired value, resulting in a list of evaluated symbolic expressions.

Method 4: Using a Custom Hermite E Series Evaluation Function

For an educational purpose or to avoid dependencies, one can implement a custom function to directly compute the values of a Hermite E series at given points. This approach offers complete control over the evaluation process.

Here’s an example:

import numpy as np
from math import factorial

def hermite_evaluate(he_coef, x):
    def h(n, xi):
        return sum(((2**i) * factorial(n) / factorial(n-i)) * (xi**i) for i in range(n+1))
    return np.array([sum(coef * h(i, xi) for i, coef in enumerate(he_coef)) for xi in x])

x = np.array([0, 1, 2])
he_coef = [1, 0, 3]
evaluated_points = hermite_evaluate(he_coef, x)

print(evaluated_points)

Output:

[ 1  2 17]

This code defines a function hermite_evaluate that accepts Hermite E series’ coefficients and an array of points. Inside, a nested function calculates the Hermite E polynomial’s terms. Finally, it computes the polynomial at each point x.

Bonus One-Liner Method 5: Using a List Comprehension and Lambda Expression

A Python one-liner can be a quick way to evaluate polynomials if simplicity and readability are not a primary concern. This method uses list comprehension in combination with a lambda function.

Here’s an example:

x = [0, 1, 2]
he_coef = [1, 0, 3]
print([(lambda n, xi: sum(coef * hermite_evaluate([coef if i == n else 0 for i, coef in enumerate(he_coef)], [xi])[0] for n, coef in enumerate(he_coef)))(len(he_coef)-1, xi) for xi in x])

Output:

[1, 2, 17]

This snippet uses a list comprehension to iterate over the array x, applying a lambda function that creates a temporary Hermite E series for each coefficient, evaluates it, and then sums the results.

Summary/Discussion

  • Method 1: NumPy’s polynomial.hermite_e.hermval. This method is efficient, precise, and excellent for those already using NumPy for numerical computations. Its weakness is that it requires the NumPy library.
  • Method 2: SciPy’s special.hermite. It is useful when one needs additional scientific computation tools that are provided in SciPy. As with NumPy, it may be seen as an overhead for simple tasks.
  • Method 3: SymPy for Symbolic Evaluation. Ideal for tasks requiring symbolic computation and precise control over arithmetic, it is slower compared to numeric computations and overkill for simple evaluations.
  • Method 4: Custom Hermite E Series Evaluation Function. It provides complete control without third-party libraries but is less efficient and more error-prone than using tried and tested libraries.
  • Bonus Method 5: One-Liner. It’s a quick and dirty solution for one-off computations, but it is not recommended for production code due to its lack of clarity and maintainability.