在Python中计算一个Legendre系列在元组点x上的值
要在点x上计算Legendre系列的值,可以使用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 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)
在这里,x 是一个元组 –
x = (5, 10, 15)
要在点x处评估一个Legendre级数,请使用Python中的polynomial.legendre.legval()方法
Numpy −
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)
# Here, x is a tuple
x = (5, 10, 15)
# To evaluate a Legendre series at points x, use the polynomial.legendre.legval() method in Python Numpy
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...
[ 122. 469.5 1042. ]