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

ประเมินอนุกรมเชบิชีฟ 2 มิติ ที่จุด (x, y) ใน Python

บทนำ

ในการประเมินอนุกรมเชบิชีฟ 2 มิติ (2-D Chebyshev series) ที่จุด (x, y) สามารถใช้เมธอด polynomial.chebval2d() ใน NumPy ของ Python เมธอดนี้จะคืนค่าของอนุกรมเชบิชีฟ 2 มิติ ที่จุดที่สร้างจากคู่ของค่าที่ตรงกันจาก x และ y

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

  • x, y: จุดที่ต้องการประเมิน ต้องมีรูปร่าง (shape) เหมือนกัน หากเป็นลิสต์หรือทูเพิล จะถูกแปลงเป็น ndarray อัตโนมัติ
  • c: อาร์เรย์ของสัมประสิทธิ์ จัดเรียงโดยที่สัมประสิทธิ์ของพจน์ที่มีมัลติดีกรี i,j จะอยู่ใน c[i,j] หาก c มีมิติมากกว่า 2 ดัชนีที่เหลือจะนับเซตของสัมประสิทธิ์หลายชุด

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

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

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

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

c = np.arange(4).reshape(2, 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)

4. ประเมินอนุกรมเชบิชีฟ 2 มิติ

print("\nResult...\n", C.chebval2d([1, 2], [1, 2], c))

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

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

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

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

# ประเมินอนุกรมเชบิชีฟ 2 มิติ ที่จุด (x, y)
print("\nResult...\n", C.chebval2d([1, 2], [1, 2], c))

ผลลัพธ์

Our Array...
 [[0 1]
  [2 3]]
Dimensions of our Array...
 2
Datatype of our Array object...
 int64
Shape of our Array object...
 (2, 2)
Result...
 [ 6. 18.]

สรุป

ตัวอย่างนี้แสดงวิธีการใช้ chebval2d() กับอาร์เรย์สัมประสิทธิ์ 2 มิติ (2x2) ผลลัพธ์จะแตกต่างจากกรณีที่ใช้อาร์เรย์ 1 มิติ เนื่องจากโครงสร้างของสัมประสิทธิ์เปลี่ยนไป