บทนำ
เพื่อประเมินอนุกรมเลจันเดอร์ (Legendre series) 3 มิติ ที่จุด x, y, z เราสามารถใช้เมธอด polynomial.legendre.legval3d() ใน NumPy เมธอดนี้จะส่งคืนค่าของพหุนามหลายมิติ (multidimensional polynomial) ที่จุดที่สร้างจากชุดของค่าที่สอดคล้องกันจาก x, y และ z
หาก c มีขนาดน้อยกว่า 3 มิติ จะมีการเพิ่มมิติที่มีค่า 1 เข้าไปโดยอัตโนมัติ เพื่อให้กลายเป็น 3 มิติ รูปทรงของผลลัพธ์จะเป็น c.shape[3:] + x.shape
พารามิเตอร์
- x, y, z: อนุกรม 3 มิติจะถูกประเมินที่จุด (x, y, z) โดยที่ x, y และ z ต้องมีรูปทรงเหมือนกัน หาก x, y, หรือ z เป็นลิสต์หรือทูเพิล จะถูกแปลงเป็น ndarray ก่อน มิฉะนั้นจะคงค่าเดิม และหากไม่ใช่ ndarray จะถูกถือว่าเป็นสเกลาร์ (scalar)
- c: อาร์เรย์ของสัมประสิทธิ์ ที่จัดเรียงโดยสัมประสิทธิ์ของพจน์ที่มีมัลติดีกรี (multidegree) i, j, k จะอยู่ใน
c[i,j,k]หากcมีมิติมากกว่า 3 ดัชนีที่เหลือจะนับชุดของสัมประสิทธิ์หลายชุด
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial import legendre as L
2. สร้างอาร์เรย์สัมประสิทธิ์ 4 มิติ
c = np.arange(48).reshape(2, 2, 6, 2)
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. ประเมินอนุกรมเลจันเดอร์ 3 มิติ ที่จุด (x, y, z)
print("\nResult...\n", L.legval3d([1, 2], [1, 2], [1, 2], c))
ตัวอย่างโค้ดสมบูรณ์
import numpy as np
from numpy.polynomial import legendre as L
# สร้างอาร์เรย์สัมประสิทธิ์ 4 มิติ
c = np.arange(48).reshape(2, 2, 6, 2)
# แสดงอาร์เรย์
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)
# ประเมินอนุกรมเลจันเดอร์ 3 มิติ ที่จุด (x, y, z)
print("\nResult...\n", L.legval3d([1, 2], [1, 2], [1, 2], c))
ผลลัพธ์ที่คาดหวัง
Our Array...
[[[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]
[[12 13]
[14 15]
[16 17]
[18 19]
[20 21]
[22 23]]]
[[[24 25]
[26 27]
[28 29]
[30 31]
[32 33]
[34 35]]
[[36 37]
[38 39]
[40 41]
[42 43]
[44 45]
[46 47]]]]
Dimensions of our Array...
4
Datatype of our Array object...
int64
Shape of our Array object...
(2, 2, 6, 2)
Result...
[[ 552. 79447.5 ]
[ 576. 81847.125]]