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

ยกกำลังอนุกรมเลจันเดอร์ ใน Python

บทนำ

เพื่อยกกำลังอนุกรมเลจันเดอร์ (Legendre series) เราสามารถใช้เมธอด polynomial.legendre.legpow() ใน NumPy เมธอดนี้จะส่งคืนอนุกรมเลจันเดอร์ c ที่ยกกำลัง pow อาร์กิวเมนต์ c เป็นลำดับของสัมประสิทธิ์ที่จัดเรียงจากต่ำไปสูง เช่น [1, 2, 3] คืออนุกรม P_0 + 2*P_1 + 3*P_2

พารามิเตอร์หลัก:

  • c: อาร์เรย์ 1 มิติ ของสัมประสิทธิ์อนุกรมเลจันเดอร์ จัดเรียงจากต่ำไปสูง
  • pow: กำลังที่จะยกอนุกรม
  • maxpower: กำลังสูงสุดที่อนุญาต เพื่อจำกัดการเติบโตของอนุกรมให้ไม่ใหญ่เกินไป ค่าเริ่มต้นคือ 16

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

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

import numpy as np
from numpy.polynomial import legendre as L

2. สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเลจันเดอร์ 1 มิติ

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

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

print("Our coefficient 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. ยกกำลังอนุกรมเลจันเดอร์ (กำลัง 3)

print("\nResult....\n", L.legpow(c, 3))

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

import numpy as np
from numpy.polynomial import legendre as L

# สร้างอาร์เรย์สัมประสิทธิ์อนุกรมเลจันเดอร์ 1 มิติ
c = np.array([1, 2, 3])

# แสดงอาร์เรย์สัมประสิทธิ์
print("Our coefficient 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)

# ยกกำลังอนุกรมเลจันเดอร์ เป็นกำลัง 3
print("\nResult....\n", L.legpow(c, 3))

ผลลัพธ์ที่คาดหวัง

Our coefficient Array...
 [1 2 3]
Dimensions of our Array...
 1
Datatype of our Array object...
 int64
Shape of our Array object...
 (3,)

Result....
 [16.74285714 42.17142857 55.14285714 46.4  33.8025974  15.42857143  6.31168831]