Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Evaluate a Hermite series at points x in Python
To evaluate a Hermite series at points x, use the hermite.hermval() method in Python NumPy. This function evaluates a Hermite polynomial series at given points using the coefficients provided.
Syntax
numpy.polynomial.hermite.hermval(x, c, tensor=True)
Parameters
The function accepts three parameters ?
- x: Points at which to evaluate the Hermite series. Can be a scalar, list, or array.
- c: Array of coefficients ordered so that coefficients for terms of degree n are in c[n].
- tensor: If True (default), evaluates each coefficient column for every element of x. If False, broadcasts x over coefficient columns.
Basic Example
Let's start with a simple example evaluating a Hermite series at a single point ?
import numpy as np
from numpy.polynomial import hermite as H
# Create coefficient array [1, 2, 3] representing: 1 + 2*H_1(x) + 3*H_2(x)
c = np.array([1, 2, 3])
print("Coefficient array:", c)
print("Array dimensions:", c.ndim)
print("Array shape:", c.shape)
print("Array datatype:", c.dtype)
# Evaluate at x = 1
result = H.hermval(1, c)
print("\nHermite series value at x=1:", result)
Coefficient array: [1 2 3] Array dimensions: 1 Array shape: (3,) Array datatype: int64 Hermite series value at x=1: 11.0
Evaluating at Multiple Points
You can evaluate the Hermite series at multiple points simultaneously ?
import numpy as np
from numpy.polynomial import hermite as H
# Coefficient array
c = np.array([1, 2, 3])
# Evaluate at multiple points
x_points = np.array([0, 1, 2, -1])
results = H.hermval(x_points, c)
print("Coefficients:", c)
print("Evaluation points:", x_points)
print("Results:", results)
# Show individual evaluations
for i, (x, result) in enumerate(zip(x_points, results)):
print(f"At x={x}: {result}")
Coefficients: [1 2 3] Evaluation points: [ 0 1 2 -1] Results: [ 1. 11. 37. -1.] At x=0: 1.0 At x=1: 11.0 At x=2: 37.0 At x=-1: -1.0
Understanding the Calculation
The Hermite series is calculated as: c[0] + c[1]*H?(x) + c[2]*H?(x) + ... where H?(x) are Hermite polynomials ?
import numpy as np
from numpy.polynomial import hermite as H
# For coefficients [1, 2, 3], the series is: 1 + 2*H_1(x) + 3*H_2(x)
c = np.array([1, 2, 3])
# Let's verify the calculation manually at x=1
# H_0(x) = 1, H_1(x) = 2x, H_2(x) = 4x² - 2
x = 1
manual_calc = 1*1 + 2*(2*x) + 3*(4*x**2 - 2)
hermval_result = H.hermval(x, c)
print(f"Manual calculation at x=1: {manual_calc}")
print(f"hermval() result at x=1: {hermval_result}")
print(f"Results match: {manual_calc == hermval_result}")
Manual calculation at x=1: 11.0 hermval() result at x=1: 11.0 Results match: True
Conclusion
The hermite.hermval() function efficiently evaluates Hermite polynomial series at given points using coefficient arrays. It supports both scalar and array inputs, making it versatile for mathematical computations involving Hermite polynomials.
