在Python中按多项式的系数阵列在x的每个维度上进行扩展的点上评估多项式
要在点x上评估多项式,可以在Python的Numpy库中使用多项式.polyval()方法。第一个参数x,如果x是一个列表或元组,则将其转换为ndarray,否则保持不变并视为标量。无论哪种情况,x或其元素都必须支持自身和c元素之间的加法和乘法。
第二个参数C是一个按阶数存放系数的数组。如果c是多维的,则其余索引用于列举多个多项式。在二维情况下,可以将系数视为存储在c的列中。
第三个参数tensor,如果为True,则按右侧添加的1来扩展系数数组的形状,每个维度的x都添加一个。对于此操作,标量的维数为0。结果是对c的每一列在x的每个元素上进行评估。如果为False,则在评估时将x广播到c的列上。当c是多维的时,此关键字很有用。默认值为True。
步骤
首先,导入所需的库 –
import numpy as np
from numpy.polynomial.polynomial import polyval
创建一个多维数组的系数 −
c = np.arange(4).reshape(2,2)
显示数组 –
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.polyval()方法,在点x处评估多项式。
print("\nResult...\n",polyval([1,2], c, tensor=True))
示例
import numpy as np
from numpy.polynomial.polynomial import polyval
# Create a multidimensional array of coefficients
c = np.arange(4).reshape(2,2)
# 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 evaluate a polynomial at points x, use the polynomial.polyval() method in Python Numpy
print("\nResult...\n",polyval([1,2], c, tensor=True))
输出
Our Array...
[[0 1]
[2 3]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(2, 2)
Result...
[[2. 4.]
[4. 7.]]