
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Small Trailing Coefficients from a Polynomial in Python
To remove small trailing coefficients from a polynomial, use the polynomial.polytrim() 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 3-rd and 4-th order coefficients would be “trimmed”. The parameter c is a 1-d array of coefficients, ordered from lowest order to highest.The parameter tol is Trailing (i.e., highest order) elements with absolute value less than or equal to tol are removed.
Steps
At first, import the required libraries −
import numpy as np from numpy.polynomial import polyutils as pu
Create an array using the numpy.array() method. This is the 1-d 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)
To remove small trailing coefficients from a polynomial, use the polynomial.polytrim() 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 −
print("\nResult...\n",pu.trimcoef((c)))
Example
import numpy as np from numpy.polynomial import polyutils as pu # Create an array using the numpy.array() method # This is the 1-d 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) # To remove small trailing coefficients from a polynomial, use the polynomial.polytrim() method in Python Numpy. print("\nResult...\n",pu.trimcoef((c)))
Output
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.]