用 Python 计算具有给定复数根的勒让德级数根
为计算勒让德级数的根,可在 Python 中使用 polynomial.legendre.lagroots() 方法。此方法返回一个级数根数组。如果所有根都是实数,则 out 也是实数,否则就是复数。参数 c 是一个一维系数数组。
步骤
首先,导入所需的库 −
from numpy.polynomial import legendre as L
计算勒让德级数的根 −
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,)
广告