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

ประเมินพหุนามที่จุด x และ x ถูกถ่ายทอดผ่านคอลัมน์ของ r สำหรับการประเมินใน Python


ในการประเมินพหุนามที่ระบุโดยรูทของมันที่จุด x ให้ใช้เมธอด polynomial.polyvalfromroots() ใน Python Numpy พารามิเตอร์ที่ 1 คือ x ถ้า x เป็นรายการหรือทูเพิล ค่านั้นจะถูกแปลงเป็น ndarray ไม่เช่นนั้นจะไม่เปลี่ยนแปลงและถือเป็นสเกลาร์ ไม่ว่าในกรณีใด x หรือองค์ประกอบของมันจะต้องสนับสนุนการบวกและการคูณด้วยตัวมันเองและองค์ประกอบของ r

พารามิเตอร์ตัวที่ 2 r คืออาร์เรย์ของรูท ถ้า r มีหลายมิติ ดัชนีแรกจะเป็นดัชนีรูท ในขณะที่ดัชนีที่เหลือจะระบุพหุนามหลายตัว ตัวอย่างเช่น ในกรณีสองมิติ รากของพหุนามแต่ละพหุนามอาจถูกมองว่าจัดเก็บไว้ในคอลัมน์ของ r

พารามิเตอร์ที่ 3 คือเทนเซอร์ หากเป็น True รูปร่างของอาร์เรย์รูทจะถูกขยายด้วยรูปร่างทางขวา แบบหนึ่งสำหรับแต่ละมิติของ x สเกลาร์มีมิติ 0 สำหรับการดำเนินการนี้ ผลที่ได้คือทุกคอลัมน์ของสัมประสิทธิ์ใน r ถูกประเมินสำหรับทุกองค์ประกอบของ x หากเป็นเท็จ x จะถูกถ่ายทอดผ่านคอลัมน์ของ r สำหรับการประเมิน คีย์เวิร์ดนี้มีประโยชน์เมื่อ r มีหลายมิติ ค่าเริ่มต้นคือ True

ขั้นตอน

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

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

c = np.arange(-2, 2).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.polyvalfromroots() ใน Python Numpy -

print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=False))

ตัวอย่าง

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

# Create an array of multidimensional coefficients
c = np.arange(-2, 2).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 specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy
print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=False))

ผลลัพธ์

Our Array...
[[-2 -1]
[ 0 1]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2)

Result...
[-0. 0.]