Remove small trailing coefficients from Legendre polynomial in Python


To remove small trailing coefficients from Legendre polynomial, use the legendre.legtrim() 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 elements with absolute value less than or equal to tol are removed.

Steps

At first, import the required library −

import numpy as np
from numpy.polynomial import legendre as L

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 Legendre polynomial, use the legendre.legtrim() method in Python numpy −

print("\nResult...\n",L.legtrim(c))

Example

import numpy as np
from numpy.polynomial import legendre as L

# 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 Legendre polynomial, use the legendre.legtrim() method in Python numpy
print("\nResult...\n",L.legtrim(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.]

Updated on: 10-Mar-2022

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements