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

πŸ’‘ Problem Formulation: This article addresses the problem of evaluating a Hermite series for a given set of coefficients at multiple points arranged in a multidimensional array in Python. For instance, if we have a set of coefficients a_n for a Hermite series, and points X in an N-dimensional grid, the task is to compute the series value at each point in X. The desired output is an array of the same shape as X, containing the evaluated series values.

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

This method entails using the numpy.polynomial.hermite.hermval() function, which is part of NumPy library’s polynomial module. It is designed to handle Hermite series evaluations efficiently, even for multidimensional input arrays.

Here’s an example:

import numpy as np

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

# Multidimensional array of points
points = np.array([[0, 1],
                   [2, 3]])

# Evaluating the Hermite series
evaluated = np.polynomial.hermite.hermval(points, coefficients)
print(evaluated)

Output:

[[   1.    5.]
 [  17.   53.]]

This example showcases the use of NumPy’s hermval() function which evaluates a Hermite series based on the provided coefficients for each point in the multidimensional array points. The output reflects the evaluated series values corresponding to each of the input points.

Method 2: Utilizing SciPy’s scipy.special.eval_hermite

SciPy provides another function scipy.special.eval_hermite() for evaluating Hermite polynomials. Although it is primarily designed for single points, it can be adapted for multidimensional arrays using array operations.

Here’s an example:

import numpy as np
from scipy.special import eval_hermite

# Degree of the Hermite polynomial
degree = 2

# Multidimensional array of points
points = np.array([[0, 1],
                   [2, 3]])

# Create an empty array with the same shape
evaluated = np.empty_like(points, dtype=float)

# Evaluate the Hermite polynomial for each point
for i in range(points.shape[0]):
    for j in range(points.shape[1]):
        evaluated[i, j] = eval_hermite(degree, points[i, j])

print(evaluated)

Output:

[[-2.  2.]
 [22. 86.]]

This snippet loops through each element in the points array, applying the function eval_hermite() from SciPy’s special package to compute the Hermite polynomial of the specified degree. The results are stored in an array evaluated with the same shape as the input array.

Method 3: Building a Multidimensional Grid and Evaluating

When dealing with multidimensional data, creating a grid of points using numpy.meshgrid() and evaluating the Hermite series at each point can offer a structured approach.

Here’s an example:

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

# Coefficients of the Hermite series
coefficients = [0, 0, 1]  # H2(x)

# Range of points
x = np.linspace(-1, 1, 3)
y = np.linspace(-1, 1, 3)

# Create a 2D grid
X, Y = np.meshgrid(x, y)
points = np.vstack((X.ravel(), Y.ravel())).T

# Evaluate the Hermite series on the grid
evaluated = hermval(points, coefficients).reshape(X.shape)
print(evaluated)

Output:

[[-1.  0. -1.]
 [ 0.  1.  0.]
 [-1.  0. -1.]]

The code example generates a two-dimensional grid of points using numpy.meshgrid() and evaluates a second-degree Hermite polynomial at each grid point. The results are reshaped to match the grid’s dimensions.

Method 4: Applying Vectorized Functions for Efficiency

We can significantly boost performance by vectorizing the evaluation function. In NumPy, this can be done by converting the original function into a vectorized form that operates over arrays instead of scalars using the numpy.vectorize() function.

Here’s an example:

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

# Coefficients of the Hermite series
coefficients = [1, 0, -1]

# Vectorized evaluation function
vec_hermval = np.vectorize(hermval, excluded=['c'])

# Multidimensional array of points
points = np.array([[0, 1], [2, 3]])

# Evaluate the Hermite series
evaluated = vec_hermval(x=points, c=coefficients)
print(evaluated)

Output:

[[  1.  -2.]
 [ -7. -40.]]

In this code snippet, we create a vectorized version of the hermval() function, allowing us to pass the entire array of points at once. This approach takes advantage of the efficient array operation capabilities in NumPy to speed up evaluations.

Bonus One-Liner Method 5: Using Lambda Functions and Map

For an extremely concise (albeit less efficient) one-liner solution, we can combine Python’s map() function with a lambda function that wraps the Hermite evaluation.

Here’s an example:

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

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

# Multidimensional array of points
points = np.array([[0, 1], [2, 3]])

# Evaluate the Hermite series using map and lambda
evaluated = np.array(list(map(lambda point: hermval(point, coefficients), points.flatten()))).reshape(points.shape)
print(evaluated)

Output:

[[   1.    5.]
 [  17.   53.]]

This quick one-liner flattens the points array, applies the Hermite series evaluation using a lambda function, and then reshapes the result back to the original array shape. It’s a compact approach that leverages functional programming constructs.

Summary/Discussion

  • Method 1: NumPy’s hermval. Very efficient and concise. Direct support for multidimensional arrays. Best for large-scale computations.
  • Method 2: SciPy’s eval_hermite. Allows for fine-grained control over polynomial degree. Requires looping over elements for multidimensional arrays.
  • Method 3: Grid Building and Evaluation. Ideal for evaluating over a structured grid. Can become memory-intensive for large grids.
  • Method 4: Vectorized Functions. Great balance between conciseness and performance. Takes full advantage of NumPy’s vectorized operations.
  • Method 5: Lambda Functions and Map. Extremely compact code. Not as performant as other methods. Best for simple, one-off computations.