บทนำ
เพื่อคูณอนุกรมเลจันเดอร์ (Legendre series) กับอีกอนุกรมหนึ่ง เราสามารถใช้เมธอด polynomial.legendre.legmul() ใน NumPy เมธอดนี้จะส่งคืนอาร์เรย์ที่แสดงอนุกรมเลจันเดอร์ของผลคูณของทั้งสอง
เมธอดนี้คืนค่าผลคูณของอนุกรมเลจันเดอร์สองอนุกรม 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 (product)....\n", L.legmul(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 (product)....\n", L.legmul(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 (product)....
[12.6 25.2 28.28571429 10.8 4.11428571]