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

สร้างเมทริกซ์ Vandermonde แบบปลอมของพหุนามเลจันเดอร์และจุด x, y แบบ complex array ใน Python


บทนำ

การสร้างเมทริกซ์ Vandermonde แบบปลอมของพหุนามเลจันเดอร์ ใช้เมธอด legendre.legvander2d() ใน NumPy เมธอดคืนค่าเมทริกซ์ Vandermonde แบบปลอม รูปร่างของเมทริกซ์ที่คืนคือ x.shape + (deg + 1,) โดยดัชนีสุดท้ายคือดีกรีของพหุนามเลจันเดอร์ที่สอดคล้อง dtype จะเหมือนกับ x ที่แปลงแล้ว

พารามิเตอร์

  • x, y: อาร์เรย์พิกัดจุด รูปร่างเท่ากัน dtype แปลงเป็น float64 หรือ complex128 ขึ้นกับว่ามีองค์ประกอบซ้อนหรือไม่ สเกลาร์แปลงเป็น 1-D array
  • deg: รายการดีกรีสูงสุด [x_deg, y_deg]

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

1. นำเข้าไลบรารี

import numpy as np
from numpy.polynomial import legendre as L

2. สร้างอาร์เรย์พิกัดจุด (Complex)

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. สร้างเมทริกซ์ Vandermonde แบบปลอม

x_deg, y_deg = 2, 3
print("\nResult...\n", L.legvander2d(x, y, [x_deg, y_deg]))

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

import numpy as np
from numpy.polynomial import legendre as L

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", L.legvander2d(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  -5. +6.j  -29. -8.j  ...]]