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