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

สร้างอนุกรมเลจันเดอร์จากรากที่กำหนดใน Python


บทนำ

การสร้างอนุกรมเลจันเดอร์จากรากที่กำหนด ใช้เมธอด polynomial.legendre.legfromroots() ใน Python เมธอดคืนค่าอาร์เรย์ 1-D ของสัมประสิทธิ์ หากรากทั้งหมดเป็นจริง ผลลัพธ์เป็นอาร์เรย์จริง หากมีรากซ้อน ผลลัพธ์จะเป็นซ้อนแม้สัมประสิทธิ์จะเป็นจริงทั้งหมด พารามิเตอร์ roots คือลำดับที่มีราก

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

1. นำเข้าไลบรารี

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

2. สร้างอนุกรมเลจันเดอร์จากราก

print("Result...\n", L.legfromroots((-1, 0, 1)))

3. ตรวจสอบชนิดข้อมูล

print("\nType...\n", L.legfromroots((-1, 0, 1)).dtype)

4. ตรวจสอบรูปร่าง

print("\nShape...\n", L.legfromroots((-1, 0, 1)).shape)

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

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

print("Result...\n", L.legfromroots((-1, 0, 1)))
print("\nType...\n", L.legfromroots((-1, 0, 1)).dtype)
print("\nShape...\n", L.legfromroots((-1, 0, 1)).shape)

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

Result...   [ 0. -0.4 0. 0.4]
Type... float64
Shape... (4,)