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

ประเมินอนุกรมเลจันเดอร์ 3 มิติ ที่จุด (x,y,z) ด้วยอาร์เรย์สัมประสิทธิ์ 2D ใน Python


บทนำ

การประเมินอนุกรมเลจันเดอร์ 3 มิติ ที่จุด x, y, z ใช้เมธอด polynomial.legendre.legval3d() ใน NumPy คืนค่าของพหุนามหลายมิติที่จุดที่สร้างจาก triple ของค่าที่สอดคล้องจาก x, y, z

รายละเอียด

  • หาก c มีมิติน้อยกว่า 3 จะเพิ่ม ones ทำให้เป็น 3-D
  • รูปร่างผลลัพธ์: c.shape[3:] + x.shape
  • x, y, z: ต้องมีรูปร่างเท่ากัน list/tuple จะแปลงเป็น ndarray
  • c: อาร์เรย์สัมประสิทธิ์ c[i,j,k] มีสัมประสิทธิ์ของพจน์ multi-degree i,j,k

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

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

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

2. สร้างอาร์เรย์สัมประสิทธิ์ 2D

c = np.arange(4).reshape(2, 2)

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. ประเมินอนุกรม 3D

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

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

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

c = np.arange(4).reshape(2, 2)
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.legval3d([1, 2], [1, 2], [1, 2], c))

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

Our Array...   [[0 1] [2 3]]
Dimensions of our Array... 2
Datatype of our Array object... int64
Shape of our Array object... (2, 2)
Result...   [24. 42.]