从Python中的多项式中删除小的尾部系数
要从多项式中删除小的尾部系数,请在Python的Numpy中使用多项式.polytrim()方法。该方法返回一个去除尾部零的一维数组。如果结果系列为空,将返回一个包含单个零的系列。
“Small”表示“绝对值较小”,由参数tol控制;“trailing”表示最高阶系数,例如在[0, 1, 1, 0, 0](表示0 + x + x2 + 0*x3 + 0*x**4)中,第3和第4阶系数都将被“修整”。参数c是按低到高顺序排列的一维系数数组。参数tol是最高阶元素的绝对值小于或等于tol时被删除。
步骤
首先,导入所需的库−
import numpy as np
from numpy.polynomial import polyutils as pu
使用numpy.array()方法创建一个数组。这是一维的系数数组−
c = np.array([0,5,0, 0,9,0])
显示数组 –
print("Our Array...\n",c)
检查尺寸−
print("\nDimensions of our Array...\n",c.ndim)
获取数据类型−
print("\nDatatype of our Array object...\n",c.dtype)
获得形状−
print("\nShape of our Array object...\n",c.shape)
要从多项式中删除小的尾系数,请在Python的Numpy中使用polynomial.polytrim()方法。该方法返回一个1维数组,其中移除了尾部的零。如果结果系列为空,则返回一个包含单个零的系列。
print("\nResult...\n",pu.trimcoef((c)))
示例
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)))
输出
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.]