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
Remove small trailing coefficients from Hermite polynomial in Python
To remove small trailing coefficients from Hermite polynomial, use the hermite.hermtrim() method in Python NumPy. The method returns a 1-d array with trailing zeros removed. If the resulting series would be empty, a series containing a single zero is returned.
The "Small" means "small in absolute value" and is controlled by the parameter tol; "trailing" means highest order coefficient(s), e.g., in [0, 1, 1, 0, 0] (which represents 0 + x + x**2 + 0*x**3 + 0*x**4) both the 3rd and 4th order coefficients would be "trimmed."
Syntax
numpy.polynomial.hermite.hermtrim(c, tol=0)
Parameters
c − A 1-d array of coefficients, ordered from lowest order to highest.
tol − Trailing elements with absolute value less than or equal to tol are removed. Default is 0.
Basic Example
import numpy as np
from numpy.polynomial import hermite as H
# Create an array with trailing zeros
c = np.array([0, 5, 0, 0, 9, 0])
print("Original array:", c)
# Remove trailing coefficients
result = H.hermtrim(c)
print("After trimming:", result)
Original array: [0 5 0 0 9 0] After trimming: [0. 5. 0. 0. 9.]
Using Tolerance Parameter
You can specify a tolerance value to remove coefficients that are small but not exactly zero ?
import numpy as np
from numpy.polynomial import hermite as H
# Array with small coefficients at the end
c = np.array([1.0, 2.0, 3.0, 0.001, 0.0001])
print("Original array:", c)
# Remove coefficients smaller than 0.01
result = H.hermtrim(c, tol=0.01)
print("After trimming (tol=0.01):", result)
Original array: [1.0000e+00 2.0000e+00 3.0000e+00 1.0000e-03 1.0000e-04] After trimming (tol=0.01): [1. 2. 3.]
Edge Cases
If all coefficients are removed, the function returns a single zero ?
import numpy as np
from numpy.polynomial import hermite as H
# Array of only small values
c = np.array([0.001, 0.002, 0.0005])
print("Original array:", c)
# Remove all coefficients with tol=0.01
result = H.hermtrim(c, tol=0.01)
print("After trimming:", result)
print("Length:", len(result))
Original array: [0.001 0.002 0.0005] After trimming: [0.] Length: 1
Conclusion
The hermite.hermtrim() method efficiently removes trailing coefficients from Hermite polynomials. Use the tol parameter to control the threshold for "small" coefficients, making it useful for cleaning up numerical approximations.
