บทนำ
ในการสร้างเมทริกซ์วนเดอร์มอนด์ (Vandermonde matrix) ของดีกรีที่กำหนด สามารถใช้ฟังก์ชัน polynomial.polyvander() ใน NumPy ของ Python เมทริกซ์นี้มีรูปร่าง x.shape + (deg + 1,) โดยดัชนีสุดท้ายคือกำลังของ x
พารามิเตอร์สำคัญ
- x: อาร์เรย์ของจุด dtype จะถูกแปลงเป็น float64 หรือ complex128
- deg: ดีกรีของเมทริกซ์ที่ได้
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial.polynomial import polyvander
2. สร้างอาร์เรย์จุดซ้อน
x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])
3. แสดงข้อมูลของอาร์เรย์
print("Our Array...\n", x)
print("\nDimensions of our Array...\n", x.ndim)
print("\nDatatype of our Array object...\n", x.dtype)
print("\nShape of our Array object...\n", x.shape)
4. สร้างเมทริกซ์วนเดอร์มอนด์
print("\nResult...\n", polyvander(x, 2))
ตัวอย่างโค้ดสมบูรณ์
import numpy as np
from numpy.polynomial.polynomial import polyvander
# สร้างอาร์เรย์จุดซ้อน
x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])
# แสดงข้อมูลอาร์เรย์
print("Our Array...\n", x)
print("\nDimensions of our Array...\n", x.ndim)
print("\nDatatype of our Array object...\n", x.dtype)
print("\nShape of our Array object...\n", x.shape)
# สร้างเมทริกซ์วนเดอร์มอนด์ ดีกรี 2
print("\nResult...\n", polyvander(x, 2))
ผลลัพธ์
Our Array...
[-2.+2.j -1.+2.j 0.+2.j 1.+2.j 2.+2.j]
Dimensions of our Array...
1
Datatype of our Array object...
complex128
Shape of our Array object...
(5,)
Result...
[[ 1.+0.j -2.+2.j 0.-8.j]
[ 1.+0.j -1.+2.j -3.-4.j]
[ 1.+0.j 0.+2.j -4.+0.j]
[ 1.+0.j 1.+2.j -3.+4.j]
[ 1.+0.j 2.+2.j 0.+8.j]]
สรุป
เมทริกซ์วนเดอร์มอนด์ที่ได้มีขนาด (5, 3) สำหรับ 5 จุดและดีกรี 2 (มี 3 คอลัมน์: x⁰, x¹, x²) ทำงานได้กับทั้งจำนวนจริงและจำนวนซ้อน