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

แปลงอนุกรมเลจันเดอร์เป็นพหุนามใน Python

บทนำ

เพื่อแปลงอนุกรมเลจันเดอร์ (Legendre series) เป็นพหุนาม (polynomial) เราสามารถใช้เมธอด legendre.leg2poly() ใน NumPy เมธอดนี้จะแปลงอาร์เรย์ที่แสดงสัมประสิทธิ์ของอนุกรมเลจันเดอร์ ซึ่งจัดเรียงจากดีกรีต่ำสุดไปสูงสุด ให้กลายเป็นอาร์เรย์ของสัมประสิทธิ์พหุนามที่เทียบเท่า (ตามฐาน "มาตรฐาน") ซึ่งจัดเรียงจากดีกรีต่ำสุดไปสูงสุด

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

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

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

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

2. สร้างอาร์เรย์

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

3. แสดงอาร์เรย์

print("Our 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. แปลงอนุกรมเลจันเดอร์เป็นพหุนาม

print("\nResult (legendre to polynomial)...\n", L.leg2poly(c))

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

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

# สร้างอาร์เรย์
c = np.array([1, 2, 3, 4, 5])

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

# แปลงอนุกรมเลจันเดอร์เป็นพหุนาม
print("\nResult (legendre to polynomial)...\n", L.leg2poly(c))

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

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

Result (legendre to polynomial)...
 [ 1.375 -4.   -14.25  10.    21.875]