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

หารอนุกรมเฮอร์ไมท์อันหนึ่งด้วยอีกอันหนึ่งใน Python

หากต้องการหารอนุกรมเฮอร์ไมท์อันหนึ่งด้วยอีกอันหนึ่ง ให้ใช้เมธอด polynomial.hermite.hermdiv() ใน Python NumPy

เมธอดคืนค่าอาร์เรย์ของสัมประสิทธิ์อนุกรมเฮอร์ไมท์ที่แทนผลหาร (quotient) และเศษ (remainder) คืนค่าผลหารพร้อมเศษของอนุกรมเฮอร์ไมท์สองอัน c1 / c2 อาร์กิวเมนต์เป็นลำดับของสัมประสิทธิ์จากอันดับต่ำสุดไปสูงสุด เช่น [1,2,3] แทนอนุกรม P₀ + 2·P₁ + 3·P₂

พารามิเตอร์

  • c1: อาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเฮอร์ไมท์ตัวหาร (dividend) เรียงจากอันดับต่ำไปสูง
  • c2: อาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเฮอร์ไมท์ตัวหาร (divisor) เรียงจากอันดับต่ำไปสูง

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

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

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

2. สร้างอาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเฮอร์ไมท์

c1 = np.array([53., 30., 52., 7., 6.])
c2 = np.array([1, 2, 3])

3. แสดงอาร์เรย์สัมประสิทธิ์

print("Array1...\n", c1)
print("\nArray2...\n", c2)

4. แสดงชนิดข้อมูล

print("\nArray1 datatype...\n", c1.dtype)
print("\nArray2 datatype...\n", c2.dtype)

5. ตรวจสอบมิติของทั้งสองอาร์เรย์

print("\nDimensions of Array1...\n", c1.ndim)
print("\nDimensions of Array2...\n", c2.ndim)

6. ตรวจสอบรูปร่างของทั้งสองอาร์เรย์

print("\nShape of Array1...\n", c1.shape)
print("\nShape of Array2...\n", c2.shape)

7. หารอนุกรมเฮอร์ไมท์ด้วย hermite.hermdiv()

print("\nResult (division)....\n", H.hermdiv(c1, c2))

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

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

# สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเฮอร์ไมท์
c1 = np.array([53., 30., 52., 7., 6.])
c2 = np.array([1, 2, 3])

# แสดงอาร์เรย์สัมประสิทธิ์
print("Array1...\n", c1)
print("\nArray2...\n", c2)

# แสดงชนิดข้อมูล
print("\nArray1 datatype...\n", c1.dtype)
print("\nArray2 datatype...\n", c2.dtype)

# ตรวจสอบมิติ
print("\nDimensions of Array1...\n", c1.ndim)
print("\nDimensions of Array2...\n", c2.ndim)

# ตรวจสอบรูปร่าง
print("\nShape of Array1...\n", c1.shape)
print("\nShape of Array2...\n", c2.shape)

# หารอนุกรมเฮอร์ไมท์
print("\nResult (division)....\n", H.hermdiv(c1, c2))

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

Array1...
 [53. 30. 52.  7.  6.]
Array2...
 [1 2 3]
Array1 datatype...
 float64
Array2 datatype...
 int64
Dimensions of Array1...
 1
Dimensions of Array2...
 1
Shape of Array1...
 (5,)
Shape of Array2...
 (3,)
Result (division)....
 (array([0., 1., 2.]), array([1., 1.]))

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

ผลลัพธ์เป็น tuple ของสองอาร์เรย์: (quotient, remainder)

  • Quotient (ผลหาร): [0., 1., 2.] → 0·H₀ + 1·H₁ + 2·H₂
  • Remainder (เศษ): [1., 1.] → 1·H₀ + 1·H₁

ความสัมพันธ์: c1 = c2 × quotient + remainder

ตรวจสอบ: อนุกรม c1 อันดับ 4 (5 สัมประสิทธิ์) หารด้วย c2 อันดับ 2 (3 สัมประสิทธิ์) ให้ผลหารอันดับ 2 (3 สัมประสิทธิ์) และเศษอันดับ 1 (2 สัมประสิทธิ์) - ถูกต้องตามกฎการหารพหุนาม

ฟังก์ชันที่เกี่ยวข้อง: hermmul() (คูณ), hermadd() (บวก), hermsub() (ลบ), hermpow() (ยกกำลัง)

หารอนุกรมเฮอร์ไมท์อันหนึ่งด้วยอีกอันหนึ่งใน Python

ใช้ hermite.hermdiv() หารอนุกรมเฮอร์ไมท์ c1/c2 ได้ผลหาร [0,1,2] และเศษ [1,1] ตรวจสอบ c1 = c2×quotient + remainder