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

ประเมินอนุกรม Hermite_e ที่จุด x และขยายรูปร่างอาร์เรย์สัมประสิทธิ์สำหรับแต่ละมิติของ x ใน Python


บทนำ

การประเมินอนุกรม Hermite_e ที่จุด x ใช้เมธอด hermite.hermeval() ใน NumPy พารามิเตอร์ tensor ควบคุมการขยายรูปร่างอาร์เรย์สัมประสิทธิ์

พารามิเตอร์

  • x: หากเป็น list/tuple จะแปลงเป็น ndarray มิฉะนั้นถือเป็น scalar
  • C: อาร์เรย์สัมประสิทธิ์ c[n] มีสัมประสิทธิ์ของพจน์ดีกรี n หาก c หลายมิติ ดัชนีที่เหลือนับพหุนามหลายตัว
  • tensor: หาก True (default) รูปร่าง c จะขยายด้วย ones ด้านขวา หนึ่งต่อมิติของ x ทุกคอลัมน์ของ c จะประเมินสำหรับทุกองค์ประกอบของ x หาก False x จะ broadcast ไปยังคอลัมน์ของ c

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

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

import numpy as np
from numpy.polynomial import hermite_e as H

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

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

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. ประเมินอนุกรม (tensor=True)

print("\nResult...\n", H.hermeval([1, 2], c, tensor=True))

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

import numpy as np
from numpy.polynomial import hermite_e as H

c = np.arange(8).reshape(2, 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", H.hermeval([1, 2], c, tensor=True))

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

Our Array...   [[0 1 2 3] [4 5 6 7]]
Dimensions of our Array... 2
Datatype of our Array object... int64
Shape of our Array object... (2, 4)
Result...   [[ 4. 8.] [ 6. 11.] [ 8. 14.] [10. 17.]]