在Python中计算二维多项式的值
要在点(x, y)处计算二维多项式的值,可以使用Python Numpy中的polynomial.polyval2d()方法。该方法返回由x和y的对应值组成的点上的二维多项式的值。参数x,y。必须使x和y具有相同的形状。如果x或y是一个列表或元组,则首先将其转换为ndarray,否则保持不变,如果它不是ndarray,则将其视为标量。
参数c是一个系数数组,以使多重度i,j的系数包含在c[i, j]中。如果c的维度大于二,则其余索引枚举多组系数。如果c的维数少于二维,则会隐式地在其形状上附加一个ones,使其成为2-D。结果的形状将为c.shape[2:] + x.shape。
步骤
首先,导入所需的库−
import numpy as np
from numpy.polynomial.polynomial import polyval2d
创建一个多维系数数组−
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)
要在点(x,y)处评估二维多项式,请使用polynomial.polyval2d()方法−
print("\nResult...\n",polyval2d([1,2],[1,2], c))
示例
import numpy as np
from numpy.polynomial.polynomial import polyval2d
# 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 2-D polynomial at points (x, y), use the polynomial.polyval2d() method in Python Numpy
print("\nResult...\n",polyval2d([1,2],[1,2], c))
输出
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...
[ 6. 18.]