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

ประเมินค่าอนุกรมลากูร์ร์ที่จุด x ด้วยอาร์เรย์係數หลายมิติใน Python

เพื่อประเมินค่า (evaluate) อนุกรมลากูร์ร์ (Laguerre series) ที่จุด x ให้ใช้เมธอด polynomial.laguerre.lagval() ใน NumPy

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

  • x: จุดที่ต้องการประเมินค่า หากเป็นลิสต์หรือทูเพิล จะถูกแปลงเป็น ndarray มิฉะนั้นจะถือเป็นสเกลาร์
  • c: อาร์เรย์ของ係數เรียงตามลำดับดีกรี โดย c[n] เก็บ係數ของพจน์ดีกรี n หาก c เป็นหลายมิติ ดัชนีที่เหลือจะนับหลายพหุนาม
  • tensor: หากเป็น True (ค่าเริ่มต้น) รูปร่างของอาร์เรย์係ณจะถูกขยายด้วย 1 ด้านขวาสำหรับแต่ละมิติของ x ทำให้ทุกคอลัมน์ของ係數ถูกประเมินสำหรับทุกสมาชิกของ x หากเป็น False x จะถูก broadcast ไปยังคอลัมน์ของ c

ขั้นตอน

นำเข้าไลบรารี:

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

สร้างอาร์เรย์係ณหลายมิติ:

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

แสดงอาร์เรย์และตรวจสอบคุณสมบัติ:

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)

ประเมินค่าอนุกรมลากูร์ร์ที่จุด x = [1, 2]:

print("\nResult...\n", L.lagval([1, 2], c))

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

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

c = np.array([[1, 2], [3, 4]])
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.lagval([1, 2], c))

ผลลัพธ์

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