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

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

บทนำ

เมื่อใช้ polynomial.polyval() ด้วย tensor=True (ค่าเริ่มต้น) รูปร่างอาร์เรย์สัมประสิทธิ์จะถูกขยายด้วย 1 สำหรับแต่ละมิติของ x ทำให้ทุกคอลัมน์ของ c ถูกประเมินสำหรับทุกองค์ประกอบของ x

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

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=True (ค่าเริ่มต้น): ประเมินทุกคอลัมน์ c สำหรับทุก x
print("\nResult...\n", polyval([1, 2], c, tensor=True))

ผลลัพธ์

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

ผลลัพธ์เป็นเมทริกซ์ 2x2 โดยแถว = องค์ประกอบของ x (1, 2) และคอลัมน์ = คอลัมน์ของ c

  • คอลัมน์แรกของ c = [0, 2] → p(1)=2, p(2)=4
  • คอลัมน์ที่สองของ c = [1, 3] → p(1)=4, p(2)=7