บทนำ
เพื่อหารอนุกรมเลจันเดอร์ (Legendre series) ด้วยอีกอนุกรมหนึ่ง เราสามารถใช้เมธอด polynomial.legendre.legdiv() ใน NumPy เมธอดนี้จะส่งคืน quo, rem ของสัมประสิทธิ์อนุกรมเลจันเดอร์ ที่แสดงถึงผลหาร (quotient) และเศษ (remainder)
เมธอดนี้คืนค่าผลหารพร้อมเศษของอนุกรมเลจันเดอร์สองอนุกรม c1 / c2 อาร์กิวเมนต์คือลำดับของสัมประสิทธิ์จากอันดับต่ำสุดไปสูงสุด เช่น [1,2,3] แสดงถึงอนุกรม P_0 + 2*P_1 + 3*P_2 พารามิเตอร์ c1 และ c2 คืออาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเลจันเดอร์ จัดเรียงจากต่ำไปสูง
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial import legendre as L
2. สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเลจันเดอร์ 1 มิติ
c1 = np.array([2, 3, 4])
c2 = np.array([4, 3, 2])
3. แสดงอาร์เรย์สัมประสิทธิ์
print("Array1...\n", c1)
print("\nArray2...\n", c2)
4. แสดงชนิดข้อมูล
print("\nArray1 datatype...\n", c1.dtype)
print("\nArray2 datatype...\n", c2.dtype)
5. ตรวจสอบมิติของทั้งสองอาร์เรย์
print("\nDimensions of Array1...\n", c1.ndim)
print("\nDimensions of Array2...\n", c2.ndim)
6. ตรวจสอบรูปทรงของทั้งสองอาร์เรย์
print("\nShape of Array1...\n", c1.shape)
print("\nShape of Array2...\n", c2.shape)
7. หารอนุกรมเลจันเดอร์
print("\nResult (division)....\n", L.legdiv(c1, c2))
ตัวอย่างโค้ดสมบูรณ์
import numpy as np
from numpy.polynomial import legendre as L
# สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเลจันเดอร์ 1 มิติ
c1 = np.array([2, 3, 4])
c2 = np.array([4, 3, 2])
# แสดงอาร์เรย์สัมประสิทธิ์
print("Array1...\n", c1)
print("\nArray2...\n", c2)
# แสดงชนิดข้อมูล
print("\nArray1 datatype...\n", c1.dtype)
print("\nArray2 datatype...\n", c2.dtype)
# ตรวจสอบมิติของทั้งสองอาร์เรย์
print("\nDimensions of Array1...\n", c1.ndim)
print("\nDimensions of Array2...\n", c2.ndim)
# ตรวจสอบรูปทรงของทั้งสองอาร์เรย์
print("\nShape of Array1...\n", c1.shape)
print("\nShape of Array2...\n", c2.shape)
# หารอนุกรมเลจันเดอร์ด้วยอีกอนุกรมหนึ่ง
print("\nResult (division)....\n", L.legdiv(c1, c2))
ผลลัพธ์ที่คาดหวัง
Array1...
[2 3 4]
Array2...
[4 3 2]
Array1 datatype...
int64
Array2 datatype...
int64
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(3,)
Shape of Array2...
(3,)
Result (division)....
(array([2.]), array([-6., -3.]))