在Python中根據多項式的根評估x點的多項式值


為了根據根評估x點的多項式,可以使用Python NumPy中的`polynomial.polyvalfromroots()`方法。第一個引數是x。如果x是列表或元組,它將被轉換為ndarray,否則它將保持不變並被視為標量。無論哪種情況,x或其元素都必須支援自身以及r的元素的加法和乘法。

第二個引數r是根的陣列。如果r是多維的,則第一個索引是根索引,其餘索引列舉多個多項式。例如,在二維情況下,每個多項式的根可以認為儲存在r的列中。第三個引數是tensor。如果為True,則根陣列的形狀將在右側擴充套件為1,每個x的維度一個。標量對此操作的維度為0。結果是,r中的每一列係數都將針對x的每個元素進行評估。如果為False,則x將在評估中廣播到r的列上。此關鍵字在r是多維時很有用。預設值為True。

步驟

首先,匯入所需的庫:

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

建立一個係數陣列:

c = np.array([1, 2, 3])

顯示陣列:

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點的多項式,可以使用Python中的`polynomial.polyvalfromroots()`方法:

print("\nResult...\n",polyvalfromroots(1, c))

示例

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

# Create an array of coefficients
c = np.array([1, 2, 3])

# 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(1, c))

輸出

Our Array...
[1 2 3]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
0.0

更新於:2022年2月28日

116 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告