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

ประเมินพหุนามที่จุด x แบบ broadcast ผ่านคอลัมน์สัมประสิทธิ์ใน Python

บทนำ

เมธอด polynomial.polyval() พร้อมพารามิเตอร์ tensor=False ทำให้ x ถูก broadcast ผ่านคอลัมน์ของสัมประสิทธิ์ c แทนที่จะประเมินทุกคอลัมน์สำหรับทุก x

พารามิเตอร์ tensor

  • True (ค่าเริ่มต้น): ขยายรูปร่าง c ด้วย 1 ด้านขวา ประเมินทุกคอลัมน์ c สำหรับทุก x
  • False: broadcast x ผ่านคอลัมน์ c ใช้เมื่อ c เป็นหลายมิติ

ตัวอย่าง: tensor=False

import numpy as np
from numpy.polynomial.polynomial import polyval

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)

# tensor=False: broadcast x ผ่านคอลัมน์
print("\nResult...\n", polyval([1, 2], c, tensor=False))

ผลลัพธ์

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... [2. 7.]

x = [1, 2] มี 2 องค์ประกอบ c มี 2 คอลัมน์ ผลลัพธ์คือ [p₁(1), p₂(2)] โดย p₁ คือคอลัมน์แรก [0,2] และ p₂ คือคอลัมน์ที่สอง [1,3]