在Python中计算在点x处对勒让德级数进行求值并为每个维度的x扩展系数数组的形状
要在点x处计算勒让德级数,请在Python中使用多项式.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 laguerre as L
创建一个多维数组的系数 −
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中的多项式.legendre.legval()方法在点x处评估Legendre级数。
print("\nResult...\n",L.legval([1,2],c, tensor = True))
示例
import numpy as np
from numpy.polynomial import legendre as L
# 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 Legendre series at points x, use the polynomial.legendre.legval() method in Python Numpy
print("\nResult...\n",L.legval([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.]]