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 Laguerre polynomial in Python
To remove small trailing coefficients from Laguerre polynomial, use the laguerre.lagtrim() 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. The 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.laguerre.lagtrim(c, tol=0)
Parameters
The parameters are ?
- 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: 0)
Example
Let's create a Laguerre polynomial coefficient array and remove trailing zeros ?
import numpy as np
from numpy.polynomial import laguerre as L
# Create an array of coefficients
c = np.array([0, 5, 0, 0, 9, 0])
# Display the array
print("Our Array...\n", c)
# Check the Dimensions
print("\nDimensions of our Array...\n", c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n", c.dtype)
# Get the Shape
print("\nShape of our Array object...\n", c.shape)
# Remove small trailing coefficients from Laguerre polynomial
print("\nResult...\n", L.lagtrim(c))
Our Array... [0 5 0 0 9 0] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (6,) Result... [0. 5. 0. 0. 9.]
Using Custom Tolerance
You can specify a tolerance value to remove coefficients that are small but not exactly zero ?
import numpy as np
from numpy.polynomial import laguerre as L
# Array with small trailing values
c = np.array([1, 2, 3, 0.001, 0.0005])
print("Original array:", c)
print("After trimming with default tol:", L.lagtrim(c))
print("After trimming with tol=0.01:", L.lagtrim(c, tol=0.01))
Original array: [1. 2. 3. 0.001 0.0005] After trimming with default tol: [1. 2. 3. 0.001 0.0005] After trimming with tol=0.01: [1. 2. 3.]
Conclusion
The lagtrim() method efficiently removes trailing coefficients from Laguerre polynomials. Use the tol parameter to control which coefficients are considered "small" and should be removed.
