หากต้องการสร้างเมทริกซ์วนเดอร์มอนด์แบบปลอม (Pseudo-Vandermonde matrix) ของอันดับที่กำหนด ให้ใช้ polynomial.polyvander2d() ใน Python NumPy
เมธอดคืนค่าเมทริกซ์วนเดอร์มอนด์แบบปลอมของอันดับ deg และจุดตัวอย่าง (x, y)
พารามิเตอร์
- x, y: อาร์เรย์พิกัดจุด ทุกตัวต้องมีรูปร่างเหมือนกัน dtypes จะแปลงเป็น float64 หรือ complex128
- deg: รายการอันดับสูงสุด รูปแบบ
[x_deg, y_deg]
ขั้นตอนการใช้งาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
2. สร้างอาร์เรย์พิกัดจุดทศนิยม
x = np.array([0.1, 1.4])
y = np.array([1.7, 2.8])
3. แสดงอาร์เรย์
print("Array1...\n", x)
print("\nArray2...\n", y)
4. ตรวจสอบชนิดข้อมูล
print("\nArray1 datatype...\n", x.dtype)
print("\nArray2 datatype...\n", y.dtype)
5. ตรวจสอบมิติ
print("\nDimensions of Array1...\n", x.ndim)
print("\nDimensions of Array2...\n", y.ndim)
6. ตรวจสอบรูปร่าง
print("\nShape of Array1...\n", x.shape)
print("\nShape of Array2...\n", y.shape)
7. สร้างเมทริกซ์วนเดอร์มอนด์แบบปลอมด้วย polyvander2d()
x_deg, y_deg = 2, 3
print("\nResult...\n", polyvander2d(x, y, [x_deg, y_deg]))
ตัวอย่างโค้ดเต็ม
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
# สร้างอาร์เรย์พิกัดจุดทศนิยม
x = np.array([0.1, 1.4])
y = np.array([1.7, 2.8])
# แสดงอาร์เรย์
print("Array1...\n", x)
print("\nArray2...\n", y)
# ตรวจสอบชนิดข้อมูล
print("\nArray1 datatype...\n", x.dtype)
print("\nArray2 datatype...\n", y.dtype)
# ตรวจสอบมิติ
print("\nDimensions of Array1...\n", x.ndim)
print("\nDimensions of Array2...\n", y.ndim)
# ตรวจสอบรูปร่าง
print("\nShape of Array1...\n", x.shape)
print("\nShape of Array2...\n", y.shape)
# สร้างเมทริกซ์วนเดอร์มอนด์แบบปลอม
x_deg, y_deg = 2, 3
print("\nResult...\n", polyvander2d(x, y, [x_deg, y_deg]))
ผลลัพธ์ที่ได้
Array1...
[0.1 1.4]
Array2...
[1.7 2.8]
Array1 datatype...
float64
Array2 datatype...
float64
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(2,)
Shape of Array2...
(2,)
Result...
[[1.000000e+00 1.700000e+00 2.890000e+00 4.913000e+00 1.000000e-01
1.700000e-01 2.890000e-01 4.913000e-01 1.000000e-02 1.700000e-02
2.890000e-02 4.913000e-02]
[1.000000e+00 2.800000e+00 7.840000e+00 2.195200e+01 1.400000e+00
3.920000e+00 1.097600e+01 3.073280e+01 1.960000e+00 5.488000e+00
1.536640e+01 4.302592e+01]]
คำอธิบายเพิ่มเติม
เมทริกซ์วนเดอร์มอนด์แบบปลอม 2 มิติ สำหรับพหุนามมาตรฐาน (monomial basis) มีโครงสร้าง:
คอลัมน์แทนพจน์ xⁱ · yʲ สำหรับ i=0..x_deg, j=0..y_deg
จำนวนคอลัมน์ = (x_deg+1) × (y_deg+1) = 3 × 4 = 12 คอลัมน์
จำนวนแถว = จำนวนจุดตัวอย่าง = 2
ในตัวอย่าง ใช้จุดทศนิยม (float64):
- จุดที่ 1: (x=0.1, y=1.7)
- จุดที่ 2: (x=1.4, y=2.8)
แถวแรก (จุดที่ 1): คอลัมน์คือ 1, y, y², y³, x, xy, xy², xy³, x², x²y, x²y², x²y³
คำนวณตัวอย่าง: x=0.1, y=1.7
- 1, 1.7, 2.89, 4.913, 0.1, 0.17, 0.289, 0.4913, 0.01, 0.017, 0.0289, 0.04913
ตรงกับผลลัพธ์แถวแรก
การใช้งาน: Polynomial fitting 2 มิติ, Surface fitting, การประมาณค่าฟังก์ชันหลายตัวแปร
ใช้ polynomial.polyvander2d() สร้าง pseudo-Vandermonde matrix 2D อันดับ [2,3] ที่จุดทศนิยม (0.1,1.7) และ (1.4,2.8) ได้เมทริกซ์ float64 2x12