使用Python生成具有给定复根的首一多项式
要生成具有给定复根的首一多项式,请使用Python Numpy中的polynomial.polyfromroots()方法。该方法返回多项式系数的一维数组。如果所有根都是实数,则结果也是实数,否则为复数。参数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,)