บทนำ
การแปลงพหุนาม (Polynomial) เป็นอนุกรมเลจันเดอร์ (Legendre Series) ใช้เมธอด legendre.poly2leg() ใน NumPy แปลงอาร์เรย์ที่แสดงสัมประสิทธิ์พหุนามเรียงจากดีกรีต่ำสุดไปสูงสุด เป็นอาร์เรย์สัมประสิทธิ์อนุกรมเลจันเดอร์ที่เทียบเท่า เรียงจากต่ำสุดไปสูงสุด คืนค่าอาร์เรย์ 1-D
ขั้นตอนการทำงาน
1. นำเข้าไลบรารี
import numpy as np from numpy.polynomial import legendre as L
2. สร้างอาร์เรย์
c = np.array([1, 2, 3, 4, 5])
3. แสดงอาร์เรย์
print("Our Array...\n", c)
4. ตรวจสอบมิติ
print("\nDimensions of our Array...\n", c.ndim)
5. ตรวจสอบชนิดข้อมูล
print("\nDatatype of our Array object...\n", c.dtype)
6. ตรวจสอบรูปร่าง
print("\nShape of our Array object...\n", c.shape)
7. แปลงพหุนามเป็นอนุกรมเลจันเดอร์
print("\nResult (polynomial to legendre)...\n", L.poly2leg(c))
ตัวอย่างโค้ดสมบูรณ์
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([1, 2, 3, 4, 5])
print("Our Array...\n", c)
print("\nDimensions of our Array...\n", c.ndim)
print("\nDatatype of our Array object...\n", c.dtype)
print("\nShape of our Array object...\n", c.shape)
print("\nResult (polynomial to legendre)...\n", L.poly2leg(c))
ผลลัพธ์ที่คาดหวัง
Our Array... [1 2 3 4 5] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (5,) Result (polynomial to legendre)... [3. 4.4 4.85714286 1.6 1.14285714]