在Python中计算Legendre级数在点x的数组中的值
要在点x的数组中计算Legendre级数的值,在Python Numpy中使用polynomial.legendre.legval()方法。第一个参数是x。如果x是一个列表或元组,它将转换为ndarray,否则将保持不变并被视为标量。在任何情况下,x或其元素必须支持相加和与自身及c的元素相乘。
第二个参数C是一个按照顺序排列的系数数组,其中第n次项的系数包含在c[n]中。如果c是多维的,则剩余的索引枚举多个多项式。在二维情况下,系数可以视为存储在c的列中。
第三个参数tensor,如果为True,则扩展系数数组的形状,在右侧添加一个维度,每个x的维度增加一个。标量在此操作中具有维度0。结果是将c中的每一列系数对x的每个元素进行评估。如果为False,则将x在c的列上进行广播评估。当c是多维的时,此关键字非常有用。默认值为True。
步骤
首先,导入所需的库 –
import numpy as np
from numpy.polynomial import legendre as L
创建一个系数数组 –
c = np.array([1, 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)
在Python的Numpy中,要在一组点x处评估Legendre级数,可以使用polynomial.legendre.legval()方法。
x = np.array([[1,2],[3,4]])
print("\nResult...\n",L.legval(x,c))
示例
import numpy as np
from numpy.polynomial import legendre as L
# Create an array of coefficients
c = np.array([1, 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 Legendre series at array of points x, use the polynomial.legendre.legval() method in Python Numpy
x = np.array([[1,2],[3,4]])
print("\nResult...\n",L.legval(x,c))
输出
Our Array...
[1 2 3]
Dimensions of our Array...
1
Datatype of our Array object...
int64
Shape of our Array object...
(3,)
Result...
[[ 6. 21.5]
[46. 79.5]]