Given a Chebyshev Polynomial, the task is to Remove Small Trailing Coefficients from Chebyshev Polynomial in Python and NumPy.
Example
Input: [-1, 0, 2, 0]
Output: [-1. 0. 2.]
Explanation: One dimensional array in which trailing zeroes are removed.
NumPy.polynomial.Chebyshev method
Python provides a method called NumPy.polynomial.Chebyshev removes the trailing zeroes in a polynomial. This method accepts a one-dimensional (1D) array that consists of coefficients of a polynomial starting from lower order to higher order and returns a one-dimensional (1D) array in which trailing zeroes are removed. Let’s consider a polynomial 0x3+2x2+0x-1 and for the given polynomial, the coefficient array is [-1, 0, 2, 0] starting from lower-order constant to higher-order x3. The chebtrim method will remove the trailing zeroes and return the resultant coefficient array.
Syntax: numpy.polynomial.chebyshev.chebtrim(arr)
Parameter:
- arr: 1-d array of coefficients.
Returns: trimmed ndarray.
Example 1:
Python program to remove the small trailing coefficients for the polynomial 0x3+2x2+0x-1.
Python3
# import necessary packages import numpy as np import numpy.polynomial.chebyshev as c # create 1D array of # coefficients for the given polynomial coeff = np.array([ - 1 , 0 , 2 , 0 ]) # returns array where trailing zeroes # got removed print (c.chebtrim(coeff)) |
Output:
The x3 coefficient is removed because there is no significance for the higher-order term as its coefficient is zero.
[-1. 0. 2.]
Example 2:
Python program to remove the small trailing coefficients for the polynomial 0x5+0x4+x3-x2+10x+0.
Python3
# import necessary packages import numpy as np import numpy.polynomial.chebyshev as c # create 1D array of coefficients for the given polynomial coeff = np.array([ 0 , 10 , - 1 , 1 , 0 , 0 ]) # returns array where trailing zeroes got removed print (c.chebtrim(coeff)) |
Output:
The coefficients of x4 and x5 are removed by the chebtrim method from the input array of coefficients.
[ 0. 10. -1. 1.]
Example 3:
Python program to remove the small trailing coefficients for the polynomial 4x4+3x3-2x2-1x+0.
Python3
# import necessary packages import numpy as np import numpy.polynomial.chebyshev as c # create 1D array of coefficients for the # given polynomial coeff = np.array([ 0 , - 1 , - 2 , 3 , 4 ]) # returns array where trailing zeroes got removed print (c.chebtrim(coeff)) |
Output:
Here no coefficients are removed by the chebtrim method as there are no trailing zeroes.
[ 0. -1. -2. 3. 4.]