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

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

บทนำ

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

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

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

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

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

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

2. สร้างอาร์เรย์พิกัดจุดแบบจุดทศนิยม

x = np.array([1.5, 2.3])
y = np.array([3.7, 4.4])
z = np.array([5.3, 6.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.5, 2.3])
y = np.array([3.7, 4.4])
z = np.array([5.3, 6.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]))

ผลลัพธ์

ผลลัพธ์เป็นเมทริกซ์ขนาดใหญ่ที่มีค่าจุดทศนิยม (float64) แสดงค่าของพจน์พหุนาม x^i y^j z^k สำหรับทุกชุดของ i, j, k ที่ i ≤ 2, j ≤ 3, k ≤ 4 ที่แต่ละจุดตัวอย่าง

สรุป

ฟังก์ชัน polyvander3d() ทำงานได้กับทั้งจุดแบบจำนวนเต็ม จุดทศนิยม และจุดซ้อน ให้เมทริกซ์วนเดอร์มอนด์ที่ใช้ในการแก้ระบบสมการเชิงเส้นสำหรับการเหมาะสมพหุนาม 3 ตัวแปร