Computer >> Máy Tính >  >> Lập trình >> Python

Tạo đa thức monic với các gốc phức đã cho bằng Python

Để tạo một đa thức monic với các gốc phức đã cho, hãy sử dụng phương thức polynomial.polyfromaries () trong Python Numpy. Phương thức trả về mảng 1-D của các hệ số của đa thức Nếu tất cả các gốc là thực, thì out cũng là thực, nếu không thì nó là phức. Các gốc tham số là trình tự chứa các gốc.

Các bước

Đầu tiên, hãy nhập các thư viện được yêu cầu -

from numpy.polynomial import polynomial as P

Cho các gốc phức tạp -

j = complex(0,1)
print("Result...\n",P.polyfromroots((-j,j)))

Nhận loại dữ liệu -

print("\nType...\n",P.polyfromroots((-j, j)).dtype)

Lấy hình dạng -

print("\nShape...\n",P.polyfromroots((-j, j)).shape)

Ví dụ

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)

Đầu ra

Result...
[1.+0.j 0.+0.j 1.+0.j]

Type...
complex128

Shape...
(3,)