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

ประเมินอนุกรมเฮอร์ไมท์ที่จุด x และขยายรูปร่างสัมประสิทธิ์สำหรับแต่ละมิติของ x ใน Python

หากต้องการประเมินค่า (evaluate) อนุกรมเฮอร์ไมท์ (Hermite series) ที่จุด x ให้ใช้เมธอด hermite.hermval() ใน Python NumPy

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

  • x: จุดที่ต้องการประเมิน หากเป็น list หรือ tuple จะแปลงเป็น ndarray มิฉะนั้นถือเป็นสเกลาร์
  • c: อาร์เรย์สัมประสิทธิ์ เรียงลำดับว่าสัมประสิทธิ์ของพจน์อันดับ n อยู่ใน c[n] หาก c เป็นหลายมิติ ดัชนีที่เหลือจะนับพหุนามหลายตัว
  • tensor: หาก True (default) รูปร่างของอาร์เรย์สัมประสิทธิ์จะขยายด้วย 1 ด้านขวา สำหรับแต่ละมิติของ x ทำให้ทุกคอลัมน์ของ c ถูกประเมินสำหรับทุกองค์ประกอบของ x

ขั้นตอนการใช้งาน

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

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

2. สร้างอาร์เรย์สัมประสิทธิ์หลายมิติ

c = np.arange(8).reshape(2, 4)

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

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

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

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

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

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

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

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

7. ประเมินอนุกรมเฮอร์ไมท์ที่จุด x=[1,2] ด้วย tensor=True

print("\nResult...\n", H.hermval([1, 2], c, tensor=True))

ตัวอย่างโค้ดเต็ม

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

# สร้างอาร์เรย์สัมประสิทธิ์หลายมิติ
c = np.arange(8).reshape(2, 4)

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

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

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

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

# ประเมินอนุกรมเฮอร์ไมท์ที่จุด x=[1,2] tensor=True
print("\nResult...\n", H.hermval([1, 2], c, tensor=True))

ผลลัพธ์ที่ได้

Our Array...
 [[0 1 2 3]
 [4 5 6 7]]
Dimensions of our Array...
 2
Datatype of our Array object...
 int64
Shape of our Array object...
 (2, 4)
Result...
 [[ 8. 16.]
 [11. 21.]
 [14. 26.]
 [17. 31.]]

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

อาร์เรย์สัมประสิทธิ์ c (shape 2×4):

[[0 1 2 3]
 [4 5 6 7]]

ถูกตีความว่าเป็น 4 พหุนามเฮอร์ไมท์ (4 คอลัมน์) แต่ละตัวมี 2 สัมประสิทธิ์ (อันดับ 0 และ 1):

  • พหุนามที่ 1: 0·H₀ + 4·H₁ = 4·H₁
  • พหุนามที่ 2: 1·H₀ + 5·H₁ = H₀ + 5·H₁
  • พหุนามที่ 3: 2·H₀ + 6·H₁ = 2·H₀ + 6·H₁
  • พหุนามที่ 4: 3·H₀ + 7·H₁ = 3·H₀ + 7·H₁

ด้วย tensor=True (default) ประเมินทุกพหุนามที่ทุกจุด x:

x = [1, 2] → ผลลัพธ์ shape (2, 4) แต่ NumPy คืนค่า shape (4, 2) คือ (n_polynomials, len(x))

คำนวณ H₀=1, H₁=2x:

พหุนามx=1x=2
4·H₁4·2=84·4=16
H₀+5·H₁1+5·2=111+5·4=21
2·H₀+6·H₁2+6·2=142+6·4=26
3·H₀+7·H₁3+7·2=173+7·4=31

ตรงกับผลลัพธ์ [[8,16], [11,21], [14,26], [17,31]]

ประเมินอนุกรมเฮอร์ไมท์ที่จุด x และขยายรูปร่างสัมประสิทธิ์สำหรับแต่ละมิติของ x ใน Python

ใช้ hermite.hermval(tensor=True) ประเมินอนุกรมเฮอร์ไมท์ 4 พหุนามที่ x=[1,2] สัมประสิทธิ์ 2x4 ได้ผลลัพธ์ 4x2 matrix