在Python中评估Legendre级数的点x

在Python中评估Legendre级数的点x

要在点x处评估Legendre级数,可以使用Python中的polynomial.legendre.legval()方法。第一个参数是x。如果x是一个列表或元组,它将被转换为ndarray,否则将保持不变并被视为标量。无论哪种情况,x或其元素都必须支持与自身和c的元素的加法和乘法。

第二个参数C是一个按次数n包含在c[n]中的系数数组。如果c是多维的,剩余的索引会枚举多个多项式。在二维情况下,系数可以被认为是存储在c的列中的。

第三个参数tensor如果为True,则在系数数组的右侧扩展形状,每个维度都增加一个。对于此操作,标量的维度为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)

Get the Shape –

print("\nShape of our Array object...\n",c.shape)

在这里,x是一个列表 –

x = [5, 10, 15]

要在点x处评估Legendre级数,请使用Python Numpy库中的polynomial.legendre.legval()方法。

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 list
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. ]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程