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