在Python中使用給定根生成勒讓德級數。
要生成勒讓德級數,請在 Python 中使用 polynomial.legendre.legfromroots() 方法。該方法返回一個一維繫數陣列。如果所有根都是實根,那麼輸出將是實陣列,如果某些根是複數根,那麼即使結果中所有係數都是實數,輸出也會是複數。roots 引數是容器,包含根。
步驟
首先,匯入必需的程式庫 −
import numpy as np from numpy.polynomial import legendre as L
要生成勒讓德級數,請在 Python 中使用 polynomial.legendre.legfromroots() 方法 −
print("Result...\n",L.legfromroots((-1,0,1)))
獲取資料型別 −
print("\nType...\n",L.legfromroots((-1,0,1)).dtype)
獲取形狀 −
print("\nShape...\n",L.legfromroots((-1,0,1)).shape)
例子
import numpy as np from numpy.polynomial import legendre as L # To generate a Legendre series, use the polynomial.legendre.legfromroots() method in Python print("Result...\n",L.legfromroots((-1,0,1))) # Get the datatype print("\nType...\n",L.legfromroots((-1,0,1)).dtype) # Get the shape print("\nShape...\n",L.legfromroots((-1,0,1)).shape)
輸出
Result... [ 0. -0.4 0. 0.4] Type... float64 Shape... (4,)
廣告