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

ลบอนุกรมเชบิชีฟ หนึ่งออกจากอีกอันหนึ่งใน Python

บทนำ

ในการลบอนุกรมเชบิชีฟ (Chebyshev series) หนึ่งออกจากอีกอันหนึ่ง สามารถใช้เมธอด polynomial.chebyshev.chebsub() ใน NumPy ของ Python เมธอดนี้จะคืนอาร์เรย์ของสัมประสิทธิ์อนุกรมเชบิชีฟที่แทนความต่าง

พารามิเตอร์

  • c1, c2: อาร์เรย์ 1 มิติของสัมประสิทธิ์อนุกรมเชบิชีฟ จัดเรียงจากดีกรีต่ำไปสูง (เช่น [1,2,3] แทน T₀ + 2T₁ + 3T₂)

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

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

import numpy as np
from numpy.polynomial import chebyshev as C

2. สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเชบิชีฟ 2 ชุด

c1 = np.array([1, 2, 3])
c2 = np.array([3, 2, 1])

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)

4. ลบอนุกรมเชบิชีฟ

print("\nResult (subtraction)....\n", C.chebsub(c1, c2))

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

import numpy as np
from numpy.polynomial import chebyshev as C

# สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเชบิชีฟ 2 ชุด
c1 = np.array([1, 2, 3])
c2 = np.array([3, 2, 1])

# แสดงข้อมูลอาร์เรย์
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)

# ลบอนุกรมเชบิชีฟ c1 - c2
print("\nResult (subtraction)....\n", C.chebsub(c1, c2))

ผลลัพธ์

Array1...
 [1 2 3]
Array2...
 [3 2 1]
Array1 datatype...
 int64
Array2 datatype...
 int64
Dimensions of Array1...
 1
Dimensions of Array2...
 1
Shape of Array1...
 (3,)
Shape of Array2...
 (3,)
Result (subtraction)...
 [-2.  0.  2.]

สรุป

เมธอด chebsub() คำนวณความต่าง c1 - c2 ของอนุกรมเชบิชีฟ ผลลัพธ์ [-2, 0, 2] แสดงถึงอนุกรม -2T₀ + 0T₁ + 2T₂