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

สร้างเมทริกซ์วนเดอร์มอนด์แบบปลอมอันดับกำหนดด้วยอาร์เรย์จุดเชิงซ้อนใน Python

หากต้องการสร้างเมทริกซ์วนเดอร์มอนด์แบบปลอม (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([-2.+2.j, -1.+2.j])
y = np.array([1.+2.j, 2.+2.j])

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([-2.+2.j, -1.+2.j])
y = np.array([1.+2.j, 2.+2.j])

# แสดงอาร์เรย์
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...
 [-2.+2.j -1.+2.j]
Array2...
 [1.+2.j 2.+2.j]
Array1 datatype...
 complex128
Array2 datatype...
 complex128
Dimensions of Array1...
 1
Dimensions of Array2...
 1
Shape of Array1...
 (2,)
Shape of Array2...
 (2,)
Result...
 [[ 1. +0.j  1. +2.j -3. +4.j -11. -2.j -2. +2.j -6. -2.j -2.-14.j 26.-18.j  0. -8.j 16. -8.j 32.+24.j -16.+88.j]
 [ 1. +0.j  2. +2.j  0. +8.j -16.+16.j -1. +2.j -6. +2.j -16. -8.j -16.-48.j -3. -4.j  2.-14.j 32.-24.j 112.+16.j]]

คำอธิบายเพิ่มเติม

เมทริกซ์วนเดอร์มอนด์แบบปลอม 2 มิติ สำหรับพหุนามมาตรฐาน (monomial basis) มีโครงสร้าง:

คอลัมน์แทนพจน์ xⁱ · yʲ สำหรับ i=0..x_deg, j=0..y_deg

จำนวนคอลัมน์ = (x_deg+1) × (y_deg+1) = 3 × 4 = 12 คอลัมน์

จำนวนแถว = จำนวนจุดตัวอย่าง = 2

ในตัวอย่าง ใช้จุดเชิงซ้อน:

  • จุดที่ 1: (x=-2+2j, y=1+2j)
  • จุดที่ 2: (x=-1+2j, y=2+2j)

ผลลัพธ์เป็นเมทริกซ์ complex128 ขนาด 2×12

การใช้งาน: การ fit พหุนาม 2 มิติ, การประมาณค่าฟังก์ชันหลายตัวแปร, การแก้ PDE แบบ spectral methods

สร้างเมทริกซ์วนเดอร์มอนด์แบบปลอมอันดับกำหนดด้วยอาร์เรย์จุดเชิงซ้อนใน Python

ใช้ polynomial.polyvander2d() สร้าง pseudo-Vandermonde matrix 2D อันดับ [2,3] ที่จุดเชิงซ้อน (-2+2j,1+2j) และ (-1+2j,2+2j) ได้เมทริกซ์ complex128 2x12