บทนำ
เพื่อคูณอนุกรมเลจันเดอร์ c ด้วย x โดยที่ x เป็นตัวแปรอิสระ เราสามารถใช้เมธอด polynomial.legendre.legmulx() ใน NumPy เมธอดนี้จะส่งคืนอาร์เรย์ที่แสดงผลลัพธ์ของการคูณ
พารามิเตอร์ c คืออาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเลจันเดอร์ จัดเรียงจากต่ำไปสูง
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial import legendre as L
2. สร้างอาร์เรย์
c = np.array([1, 2, 3])
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. คูณอนุกรมเลจันเดอร์ด้วยตัวแปรอิสระ x
print("\nResult....\n", L.legmulx(c))
ตัวอย่างโค้ดสมบูรณ์
import numpy as np
from numpy.polynomial import legendre as L
# สร้างอาร์เรย์
c = np.array([1, 2, 3])
# แสดงอาร์เรย์
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)
# คูณอนุกรมเลจันเดอร์ด้วยตัวแปรอิสระ x
print("\nResult....\n", L.legmulx(c))
ผลลัพธ์ที่คาดหวัง
Our Array...
[1 2 3]
Dimensions of our Array...
1
Datatype of our Array object...
int64
Shape of our Array object...
(3,)
Result....
[0.66666667 2.2 1.33333333 1.8]