Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คำนวณรากของพหุนามด้วยรากที่ซับซ้อนที่กำหนดใน Python


ในการคำนวณรากของพหุนาม ให้ใช้เมธอด polynomial.polyroots() ใน Python Numpy วิธีการส่งกลับอาร์เรย์ของรากของพหุนาม ถ้ารากทั้งหมดเป็นของจริง ผลลัพธ์ก็คือของจริงด้วย ไม่เช่นนั้นมันจะซับซ้อน พารามิเตอร์ c คืออาร์เรย์ 1-D ของสัมประสิทธิ์พหุนาม

ค่าประมาณของรูทได้มาจากค่าลักษณะเฉพาะของเมทริกซ์ที่แสดงร่วม รูตที่อยู่ไกลจากจุดกำเนิดของระนาบเชิงซ้อนอาจมีข้อผิดพลาดขนาดใหญ่เนื่องจากความไม่เสถียรเชิงตัวเลขของอนุกรมกำลังสำหรับค่าดังกล่าว รูตที่มีหลายหลากมากกว่า 1 จะแสดงข้อผิดพลาดที่มากขึ้นเช่นกัน เนื่องจากค่าของอนุกรมใกล้จุดดังกล่าวค่อนข้างไม่ไวต่อข้อผิดพลาดในรูท รากที่แยกออกมาใกล้จุดกำเนิดสามารถปรับปรุงได้ด้วยการทำซ้ำสองสามครั้งในวิธีของนิวตัน

ขั้นตอน

ขั้นแรก นำเข้าไลบรารีที่จำเป็น-

from numpy.polynomial import polynomial as P

ในการคำนวณรากของพหุนาม ให้ใช้เมธอด polynomial.polyroots() ใน Python Numpy -

j = complex(0,1)
print("Result (roots of a polynomial)...\n",P.polyroots((-j,j)))

รับประเภทข้อมูล -

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

รับรูปร่าง -

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

ตัวอย่าง

from numpy.polynomial import polynomial as P

# To compute the roots of a polynomials, use the polynomial.polyroots() method in Python Numpy.

# The method returns an array of the roots of the polynomial. If all the roots are real, then out is also real, otherwise it is complex.

# The parameter, c is a 1-D array of polynomial coefficients.
j = complex(0,1)
print("Result (roots of a polynomial)...\n",P.polyroots((-j,j)))

# Get the datatype
print("\nType...\n",P.polyroots((-j, j)).dtype)

# Get the shape
print("\nShape...\n",P.polyroots((-j, j)).shape)

ผลลัพธ์

Result (roots of a polynomial)...
[1.+0.j]

Type...
complex128

Shape...
(1,)