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

แปลงอนุกรมลากูร์ร่าเป็นพหุนามใน Python

บทนำ

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

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

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

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

import numpy as np
from numpy.polynomial import laguerre 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 (laguerre to polynomial)...\n", L.lag2poly(c))

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

import numpy as np
from numpy.polynomial import laguerre 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 (laguerre to polynomial)...\n", L.lag2poly(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 (laguerre to polynomial)...
 [ 15.  -40.   22.5  -4.    0.20833333]