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

คำนวณรากของอนุกรมเลจันเดอร์ใน Python


บทนำ

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

ขั้นตอนการทำงาน

1. นำเข้าไลบรารี

from numpy.polynomial import legendre as L

2. คำนวณรากของอนุกรมเลจันเดอร์

print("Result...\n", L.legroots((0, 1, 2)))

3. ตรวจสอบชนิดข้อมูล

print("\nType...\n", L.legroots((0, 1, 2)).dtype)

4. ตรวจสอบรูปร่าง

print("\nShape...\n", L.legroots((0, 1, 2)).shape)

ตัวอย่างโค้ดสมบูรณ์

from numpy.polynomial import legendre as L

print("Result...\n", L.legroots((0, 1, 2)))
print("\nType...\n", L.legroots((0, 1, 2)).dtype)
print("\nShape...\n", L.legroots((0, 1, 2)).shape)

ผลลัพธ์ที่คาดหวัง

Result...   [-0.76759188 0.43425855]
Type... float64
Shape... (2,)