在 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,)

更新于: 08-Mar-2022

132 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.