在Python中计算给定复根的Legendre级数的根
要计算Legendre级数的根,使用Python中的polynomial.legendre.lagroots()方法。该方法返回一个数组,包含级数的所有根。如果所有的根都是实数,则输出也是实数,否则它是复数。参数c是一个一维数组,包含系数。
步骤
首先,导入所需的库 –
from numpy.polynomial import legendre as L
计算Legendre级数的根−
j = complex(0,1)
print("Result...\n",L.legroots((-j, j)))
获取数据类型 −
print("\nType...\n",L.legroots((-j, j)).dtype)
获取形状−
print("\nShape...\n",L.legroots((-j, j)).shape)
示例
from numpy.polynomial import legendre as L
# To compute the roots of a Legendre series, use the polynomial.legendre.lagroots() method in Python
# The method returns an array of the roots of the series. If all the roots are real, then out is also real, otherwise it is complex.
# The parameter c is a 1-D array of coefficients.
j = complex(0,1)
print("Result...\n",L.legroots((-j, j)))
# Get the datatype
print("\nType...\n",L.legroots((-j, j)).dtype)
# Get the shape
print("\nShape...\n",L.legroots((-j, j)).shape)
输出
Result...
[1.+0.j]
Type...
complex128
Shape...
(1,)