在Python中,当系数为多维时,如何计算勒让德级数在x点的值
在Python Numpy中,可以使用`polynomial.legendre.legval()`方法计算勒让德级数在x点的值。第一个参数是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中,可以使用`polynomial.legendre.legval()`方法计算勒让德级数在x点的值:
print("\nResult...\n",L.legval([1,2],c))示例
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))输出
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.]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP