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

สร้างเมทริกซ์ Vandermonde ของพหุนามเลจันเดอร์ด้วยจุด complex array ใน Python


บทนำ

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

พารามิเตอร์

  • x: อาร์เรย์จุด dtype แปลงเป็น float64 หรือ complex128 สเกลาร์แปลงเป็น 1-D array
  • deg: ดีกรีของเมทริกซ์ที่ได้

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

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

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

2. สร้างอาร์เรย์ (Complex)

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

print("\nResult...\n", L.legvander(x, 2))

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

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

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", L.legvander(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 -2. +2.j -0.5-12.j] [ 1. +0.j -1. +2.j -5. -6.j] ...]