ในการสร้างพหุนามโมนิกที่มีรากที่ซับซ้อนที่กำหนด ให้ใช้เมธอด polynomial.polyfromroots() ใน Python Numpy เมธอดจะคืนค่าอาร์เรย์ 1-D ของสัมประสิทธิ์พหุนาม หากรูตทั้งหมดเป็นจริง ค่าที่ออกมาจะเป็นค่าจริงด้วย ไม่เช่นนั้นจะซับซ้อน พารามิเตอร์รากคือลำดับที่มีราก
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
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,)