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

คำนวณรากของพหุนามใน Python

บทนำ

เมธอด polynomial.polyroots() คำนวณรากของพหุนามจากสัมประสิทธิ์ 1 มิติ

ตัวอย่าง: พหุนาม x² - 1 = 0

from numpy.polynomial import polynomial as P

# สัมประสิทธิ์: -1 + 0*x + 1*x² = x² - 1
print("Result (roots of a polynomial)...\n", P.polyroots((-1, 0, 1)))

print("\nType...\n", P.polyroots((-1, 0, 1)).dtype)
print("\nShape...\n", P.polyroots((-1, 0, 1)).shape)

ผลลัพธ์

Result (roots of a polynomial)... [-1.  1.]
Type... float64
Shape... (2,)

พหุนาม x² - 1 มีรากคือ -1 และ 1 ทั้งคู่เป็นจริง (real) ดังนั้น dtype คือ float64