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

ประเมินอนุกรมเชบิชีฟ ที่จุด x พร้อมขยายรูปร่างอาร์เรย์สัมประสิทธิ์ใน Python

บทนำ

ในการประเมินอนุกรมเชบิชีฟ (Chebyshev series) ที่จุด x สามารถใช้เมธอด chebyshev.chebval() ใน NumPy พร้อมพารามิเตอร์ tensor=True (ค่าเริ่มต้น) ซึ่งจะขยายรูปร่างของอาร์เรย์สัมประสิทธิ์โดยเพิ่ม 1 ทางด้านขวาสำหรับแต่ละมิติของ x

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

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

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

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

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

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

c = np.arange(6).reshape(2, 3)

3. แสดงข้อมูลของอาร์เรย์

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)

4. ประเมินอนุกรมเชบิชีฟ ด้วย tensor=True

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

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

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

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

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

# ประเมินอนุกรมเชบิชีฟ ด้วย tensor=True (ค่าเริ่มต้น)
print("\nResult (chebval)...\n", C.chebval([1, 2], c, tensor=True))

ผลลัพธ์

Our Array...
 [[0 1 2]
  [3 4 5]]
Dimensions of our Array...
 2
Datatype of our Array object...
 int64
Shape of our Array object...
 (2, 3)
Result (chebval)...
 [[ 3.  6.]
  [ 5.  9.]
  [ 7. 12.]]

สรุป

เมื่อใช้ tensor=True (ค่าเริ่มต้น) ผลลัพธ์จะมีรูปร่างเป็น (3, 2) ซึ่งตรงกับจำนวนคอลัมน์ของ c (3 คอลัมน์) และจำนวนจุด x (2 จุด) ทุกคอลัมน์ของ c จะถูกประเมินที่ทุกจุดของ x