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

ประเมิน Legendre Series ที่ Tuple ของ Points x ใน Python

ในการประเมิน Legendre Series ที่ Points x ใช้เมธอด polynomial.legendre.legval() ใน Python NumPy พารามิเตอร์:

  • x: หากเป็น List หรือ Tuple จะแปลงเป็น ndarray มิฉะนั้นถือเป็น Scalar
  • C: อาร์เรย์สัมประสิทธิ์เรียงตาม Degree n อยู่ใน c[n] หากหลายมิติ Index ที่เหลือนับ Polynomials หลายตัว
  • tensor: True (Default) ขยาย Shape ของ Coefficient Array ด้วย Ones สำหรับแต่ละ Dimension ของ x False: Broadcast x ไปยัง Columns ของ c

ขั้นตอน

1. Import ไลบรารี:

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

2. สร้างอาร์เรย์สัมประสิทธิ์ ตรวจสอบ Dimensions Datatype Shape

3. กำหนด x เป็น Tuple:

x = (5, 10, 15)

4. ประเมิน Legendre Series:

print("Result...\n", L.legval(x, c))

ตัวอย่างโค้ดเต็ม

import numpy as np 
from numpy.polynomial import legendre as L 
c = np.array([1, 2, 3]) 
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 = (5, 10, 15) 
print("\nResult...\n", L.legval(x, c))

ผลลัพธ์

Our Array...   [1 2 3] 
Dimensions of our Array... 1 
Datatype of our Array object... int64 
Shape of our Array object... (3,) 
Result...   [ 122. 469.5 1042. ]