เพื่อลบสัมประสิทธิ์ท้าย (Trailing Coefficients) ที่มีค่าน้อยออกจากพหุนามเลจันดร์ (Legendre Polynomial) ให้ใช้เมธอด legendre.legtrim() ใน NumPy เมธอดนี้จะคืนค่าอาร์เรย์ 1 มิติที่ลบศูนย์ท้ายออกแล้ว หากอนุกรมที่ได้ว่างเปล่า จะคืนอาร์เรย์ที่มีศูนย์เพียงค่าเดียว
"เล็ก" (Small) หมายถึง "เล็กในค่าสัมบูรณ์" และควบคุมโดยพารามิเตอร์ tol "ท้าย" (Trailing) หมายถึงสัมประสิทธิ์อันดับสูงสุด เช่นใน [0, 1, 1, 0, 0] (ซึ่งแทน 0 + x + x² + 0x³ + 0x⁴) สัมประสิทธิ์อันดับ 3 และ 4 จะถูก "ตัด" (Trimmed) พารามิเตอร์ c คืออาร์เรย์ 1 มิติของสัมประสิทธิ์ เรียงจากอันดับต่ำไปสูง พารามิเตอร์ tol คือค่าสัมบูรณ์ขององค์ประกอบท้ายที่น้อยกว่าหรือเท่ากับ tol จะถูกลบออก
ขั้นตอน
นำเข้าไลบรารี:
import numpy as np from numpy.polynomial import legendre as L
สร้างอาร์เรย์สัมประสิทธิ์ 1 มิติ:
c = np.array([0, 5, 0, 0, 9, 0])
แสดงอาร์เรย์:
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...\n", L.legtrim(c))ตัวอย่างโค้ดเต็ม
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([0, 5, 0, 0, 9, 0])
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...\n", L.legtrim(c))ผลลัพธ์
Our Array... [0 5 0 0 9 0] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (6,) Result... [0. 5. 0. 0. 9.]