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

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

Để tạo một đa thức monic với các căn đã cho, hãy sử dụng phương thức polynomial.polyfromaries () trongPython 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ì 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 thư viện được yêu cầu -

from numpy.polynomial import polynomial as P

Tạo đa thức monic -

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

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

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

Lấy hình dạng -

print("\nShape...\n",P.polyfromroots((-1,0,1)).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.
# x(x - 1)(x + 1) = x^3 - x
print("Result...\n",P.polyfromroots((-1,0,1)))

# Get the datatype
print("\nType...\n",P.polyfromroots((-1,0,1)).dtype)

# Get the shape
print("\nShape...\n",P.polyfromroots((-1,0,1)).shape)

Đầu ra

Result...
[ 0. -1. 0. 1.]

Type...
float64

Shape...
(4,)