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

สร้างเมทริกซ์แบบヴァンเดอร์มอนด์ของพหุนามเฮอร์ไมท์ ด้วยอาร์เรย์จุดซ้อนใน Python

บทนำ

เพื่อสร้างเมทริกซ์แบบヴァンเดอร์มอนด์ (Vandermonde matrix) ของพหุนามเฮอร์ไมท์ (Hermite polynomial) เราสามารถใช้เมธอด hermite.hermvander() ใน NumPy (หมายเหตุ: ไม่ใช่ chebyshev.hermvander() ตามที่โค้ดต้นฉบับอาจสับสน) เมธอดนี้จะส่งคืนเมทริกซ์แบบヴァンเดอร์มอนด์疑似 รูปทรงของเมทริกซ์ที่ส่งคืนคือ x.shape + (deg + 1,) โดยดัชนีสุดท้ายคือดีกรีของพหุนามเฮอร์ไมท์ที่สอดคล้องกัน ชนิดข้อมูล (dtype) จะเหมือนกับ x ที่แปลงแล้ว

พารามิเตอร์หลัก:

  • x: อาร์เรย์ของจุด ชนิดข้อมูลจะถูกแปลงเป็น float64 หรือ complex128 ขึ้นอยู่กับว่ามีองค์ประกอบซ้อนหรือไม่ หาก x เป็นสเกลาร์ จะถูกแปลงเป็นอาร์เรย์ 1 มิติ
  • deg: ดีกรีของเมทริกซ์ที่ได้

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

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

import numpy as np
from numpy.polynomial import hermite as H

2. สร้างอาร์เรย์จุดซ้อน

x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])

3. แสดงอาร์เรย์

print("Our Array...\n", x)

4. ตรวจสอบมิติ

print("\nDimensions of our Array...\n", x.ndim)

5. ตรวจสอบชนิดข้อมูล

print("\nDatatype of our Array object...\n", x.dtype)

6. ตรวจสอบรูปทรง

print("\nShape of our Array object...\n", x.shape)

7. สร้างเมทริกซ์แบบヴァンเดอร์มอนด์ของพหุนามเฮอร์ไมท์ (ดีกรี 2)

print("\nResult...\n", H.hermvander(x, 2))

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

import numpy as np
from numpy.polynomial import hermite as H

# สร้างอาร์เรย์จุดซ้อน
x = np.array([-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j])

# แสดงอาร์เรย์
print("Our Array...\n", x)

# ตรวจสอบมิติ
print("\nDimensions of our Array...\n", x.ndim)

# ตรวจสอบชนิดข้อมูล
print("\nDatatype of our Array object...\n", x.dtype)

# ตรวจสอบรูปทรง
print("\nShape of our Array object...\n", x.shape)

# สร้างเมทริกซ์แบบヴァンเดอร์มอนด์ของพหุนามเฮอร์ไมท์
print("\nResult...\n", H.hermvander(x, 2))

ผลลัพธ์ที่คาดหวัง

Our Array...
 [-2.+2.j -1.+2.j 0.+2.j 1.+2.j 2.+2.j]
Dimensions of our Array...
 1
Datatype of our Array object...
 complex128
Shape of our Array object...
 (5,)

Result...
 [[ 1. +0.j  -4. +4.j  -2. -32.j]
 [ 1. +0.j  -2. +4.j  -14. -16.j]
 [ 1. +0.j   0. +4.j  -18. +0.j]
 [ 1. +0.j   2. +4.j  -14. +16.j]
 [ 1. +0.j   4. +4.j  -2. +32.j]]