Python 对系数的列广播进行Chebyshev级数求值
要在给定的点x处对Chebyshev级数进行求值,可以使用Python的Numpy库中的chebyshev.chebval()方法。 第一个参数x,若x是一个列表或元组,将其转换为ndarray;否则保持不变并视为标量。无论哪种情况,x或其元素都必须支持自身和c的元素的加法和乘法操作。
第二个参数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(3,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中,使用chebyshev.chebval(()方法来计算在点x上的Chebyshev级数−
print("\nResult (chebval)...\n",C.chebval([1,2],c,tensor=False))
示例
import numpy as np
from numpy.polynomial import chebyshev as C
# Create a multidimensional array of coefficients
c = np.arange(6).reshape(3,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 Chebyshev series at points x, use the chebyshev.chebval(() method in Python Numpy
print("\nResult (chebval)...\n",C.chebval([1,2],c,tensor=False))
输出
Our Array...
[[0 1]
[2 3]
[4 5]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(3, 2)
Result (chebval)...
[ 6. 42.]