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 broadcast over the columns of the coefficient in Python
To evaluate a Hermite series at points x, use the hermite.hermval() method in Python NumPy. This function allows you to evaluate Hermite polynomials with broadcasting capabilities over coefficient arrays.
Syntax
numpy.polynomial.hermite.hermval(x, c, tensor=True)
Parameters
x: If x is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. The elements must support addition and multiplication with themselves and with the elements of c.
c: An array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If c is multidimensional, the remaining indices enumerate multiple polynomials. In the two-dimensional case, the coefficients may be thought of as stored in the columns of c.
tensor: If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of x. If False, x is broadcast over the columns of c for the evaluation. The default value is True.
Example with tensor=False
Let's create a multidimensional coefficient array and evaluate it at specific points ?
import numpy as np
from numpy.polynomial import hermite as H
# Create a multidimensional array of coefficients
c = np.arange(4).reshape(2,2)
# Display the array
print("Our Array...")
print(c)
# Check the Dimensions
print("\nDimensions of our Array...")
print(c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...")
print(c.dtype)
# Get the Shape
print("\nShape of our Array object...")
print(c.shape)
# Evaluate Hermite series at points x with tensor=False
print("\nResult...")
print(H.hermval([1,2], c, tensor=False))
Our Array... [[0 1] [2 3]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [ 4. 13.]
Understanding tensor Parameter
The difference between tensor=True and tensor=False is important for broadcasting ?
import numpy as np
from numpy.polynomial import hermite as H
c = np.arange(6).reshape(3,2)
x = [1, 2]
print("Coefficient array:")
print(c)
print("\nWith tensor=False (broadcasting):")
result_false = H.hermval(x, c, tensor=False)
print("Shape:", result_false.shape)
print("Result:", result_false)
print("\nWith tensor=True (default):")
result_true = H.hermval(x, c, tensor=True)
print("Shape:", result_true.shape)
print("Result:", result_true)
Coefficient array:
[[0 1]
[2 3]
[4 5]]
With tensor=False (broadcasting):
Shape: (2,)
Result: [ 12. 23.]
With tensor=True (default):
Shape: (2, 2)
Result: [[ 8. 11.]
[28. 43.]]
Comparison
| Parameter | Broadcasting Behavior | Output Shape | Use Case |
|---|---|---|---|
tensor=True |
Each coefficient column evaluated at each x point | (len(x), c.shape[1]) | Multiple polynomials, multiple points |
tensor=False |
x points broadcast over coefficient columns | (len(x),) | Single result per x point |
Conclusion
Use hermite.hermval() to evaluate Hermite series efficiently. Set tensor=False for broadcasting x over coefficient columns, or tensor=True (default) when evaluating multiple polynomials at multiple points.
