ในการประเมินพหุนามที่จุด x ให้ใช้เมธอด polynomial.polyval() ใน Python Numpy พารามิเตอร์ตัวที่ 1 x ถ้า x เป็นรายการหรือทูเพิล พารามิเตอร์นั้นจะถูกแปลงเป็น ndarray ไม่เช่นนั้น จะไม่เปลี่ยนแปลงและถือเป็นสเกลาร์ ไม่ว่าในกรณีใด x หรือองค์ประกอบของมันจะต้องสนับสนุนการบวกและการคูณด้วยตัวมันเองและองค์ประกอบของค
พารามิเตอร์ตัวที่ 2, C, อาร์เรย์ของสัมประสิทธิ์ที่เรียงลำดับเพื่อให้สัมประสิทธิ์สำหรับเงื่อนไขของดีกรี n อยู่ใน c[n] ถ้า c เป็นหลายมิติ ดัชนีที่เหลือจะระบุพหุนามหลายตัว ในกรณีสองมิติ สัมประสิทธิ์อาจคิดว่าจัดเก็บไว้ในคอลัมน์ของค
พารามิเตอร์ตัวที่ 3 เทนเซอร์ ถ้า True รูปร่างของอาร์เรย์สัมประสิทธิ์จะถูกขยายด้วยตัวที่ด้านขวา หนึ่งตัวสำหรับแต่ละมิติของ x สเกลาร์มีมิติ 0 สำหรับการดำเนินการนี้ ผลที่ได้คือทุกคอลัมน์ของสัมประสิทธิ์ใน c ถูกประเมินสำหรับทุกองค์ประกอบของ x หากเป็นเท็จ x จะถูกถ่ายทอดผ่านคอลัมน์ของ c สำหรับการประเมิน คีย์เวิร์ดนี้มีประโยชน์เมื่อ c เป็นแบบหลายมิติ ค่าเริ่มต้นคือ 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) ในการประเมินพหุนามที่จุด x ให้ใช้วิธี polynomial.polyval() ใน Python Numpy -
print("\nResult...\n",polyval([1,2], c, tensor=True)) ตัวอย่าง
import numpy as np
from numpy.polynomial.polynomial import polyval
# Create a multidimensional array of coefficients
c = np.arange(4).reshape(2,2)
# Display the array
print("Our Array...\n",c)
# Check the Dimensions
print("\nDimensions of our Array...\n",c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",c.dtype)
# Get the Shape
print("\nShape of our Array object...\n",c.shape)
# To evaluate a polynomial at points x, use the polynomial.polyval() method in Python Numpy
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.]]