Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

สร้างเมทริกซ์วนเดอร์มอนด์แบบปลอม (Pseudo-Vandermonde) ของดีกรีที่กำหนดและจุด x, y, z ใน Python

บทนำ

ในการสร้างเมทริกซ์วนเดอร์มอนด์แบบปลอม (pseudo-Vandermonde matrix) ของดีกรีที่กำหนดและจุดตัวอย่าง x, y, z สามารถใช้ฟังก์ชัน polynomial.polyvander3d() ใน NumPy ของ Python นี่เป็นกรณีทั่วไปที่ใช้จุดตัวอย่างแบบจำนวนเต็ม

พารามิเตอร์สำคัญ

  • x, y, z: อาร์เรย์พิกัดจุด ต้องมีรูปร่างเหมือนกัน
  • deg: รายการของดีกรีสูงสุดในรูปแบบ [x_deg, y_deg, z_deg]

ขั้นตอนการทำงาน

1. นำเข้าไลบรารีที่จำเป็น

import numpy as np
from numpy.polynomial.polynomial import polyvander3d

2. สร้างอาร์เรย์พิกัดจุดตัวอย่าง

x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([5, 6])

3. แสดงข้อมูลของอาร์เรย์

print("Array1...\n", x)
print("\nArray2...\n", y)
print("\nArray3...\n", z)
print("\nArray1 datatype...\n", x.dtype)
print("\nArray2 datatype...\n", y.dtype)
print("\nArray3 datatype...\n", z.dtype)
print("\nDimensions of Array1...\n", x.ndim)
print("\nDimensions of Array2...\n", y.ndim)
print("\nDimensions of Array3...\n", z.ndim)
print("\nShape of Array1...\n", x.shape)
print("\nShape of Array2...\n", y.shape)
print("\nShape of Array3...\n", z.shape)

4. สร้างเมทริกซ์วนเดอร์มอนด์ 3 มิติ

x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n", polyvander3d(x, y, z, [x_deg, y_deg, z_deg]))

ตัวอย่างโค้ดสมบูรณ์

import numpy as np
from numpy.polynomial.polynomial import polyvander3d

# สร้างอาร์เรย์พิกัดจุดตัวอย่าง
x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([5, 6])

# แสดงข้อมูลอาร์เรย์
print("Array1...\n", x)
print("\nArray2...\n", y)
print("\nArray3...\n", z)
print("\nArray1 datatype...\n", x.dtype)
print("\nArray2 datatype...\n", y.dtype)
print("\nArray3 datatype...\n", z.dtype)
print("\nDimensions of Array1...\n", x.ndim)
print("\nDimensions of Array2...\n", y.ndim)
print("\nDimensions of Array3...\n", z.ndim)
print("\nShape of Array1...\n", x.shape)
print("\nShape of Array2...\n", y.shape)
print("\nShape of Array3...\n", z.shape)

# สร้างเมทริกซ์วนเดอร์มอนด์ 3 มิติ
x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n", polyvander3d(x, y, z, [x_deg, y_deg, z_deg]))

ผลลัพธ์

ผลลัพธ์เป็นเมทริกซ์ 2x60 (2 จุดตัวอย่าง × 60 พจน์พหุนาม) ที่แสดงค่าของพจน์ x^i y^j z^k สำหรับ i=0..2, j=0..3, k=0..4 ที่แต่ละจุด (1,3,5) และ (2,4,6)

สรุป

ฟังก์ชัน polyvander3d() สร้างเมทริกซ์ฐานสำหรับพหุนาม 3 ตัวแปร ซึ่งเป็นเครื่องมือสำคัญในการแก้ปัญหาการเหมาะสมคอนโวลูชัน (fitting) และการประมาณค่าฟังก์ชันหลายตัวแปร