在 Python 中生成具有給定複數根的單根多項式
要在 Python Numpy 中生成具有給定複數根的單根多項式,請使用 polynomial.polyfromroots() 方法。該方法返回多項式係數的 1-D 陣列。如果所有根都是實數,那麼 out 也是實數,否則它就是複數。引數 roots 是包含根的序列。
步驟
首先,匯入必需的庫 -
from numpy.polynomial import polynomial as P
給定複數根 −
j = complex(0,1)
print("Result...\n",P.polyfromroots((-j,j)))獲取資料型別 −
print("\nType...\n",P.polyfromroots((-j, j)).dtype)
獲取形狀 −
print("\nShape...\n",P.polyfromroots((-j, j)).shape)示例
from numpy.polynomial import polynomial as P
# To generate a monic polynomial with given roots, use the polynomial.polyfromroots() method in Python Numpy.
# The method returns the 1-D array of the polynomial’s coefficients If all the roots are real, then out is also real, otherwise it is complex.
# The parameter roots are the sequence containing the roots.
j = complex(0,1)
print("Result...\n",P.polyfromroots((-j,j)))
# Get the datatype
print("\nType...\n",P.polyfromroots((-j, j)).dtype)
# Get the shape
print("\nShape...\n",P.polyfromroots((-j, j)).shape)輸出
Result... [1.+0.j 0.+0.j 1.+0.j] Type... complex128 Shape... (3,)
廣告
Data Structure
Networking
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP