在 Python 中評估點 (x, y, z) 處的 3D 多項式


要評估點 (x, y, z) 處的 3D 多項式,請在 Python Numpy 中使用 polynomial.polyval3d() 方法。該方法返回在由 x、y 和 z 中對應值的三個元組形成的點上多維多項式的值。引數為 x、y、z。三維序列在點 (x, y, z) 處進行評估,其中 x、y 和 z 必須具有相同的形狀。如果 x、y 或 z 中的任何一個是列表或元組,則首先將其轉換為 ndarray,否則將其保持不變,如果它不是 ndarray,則將其視為標量。

引數 c 是一個係數陣列,其排序方式使得多度為 i、j、k 的項的係數包含在 c[i,j,k] 中。如果 c 的維度大於 3,則其餘索引列舉多個係數集。如果 c 的維度小於 3,則會隱式地將其形狀附加為 1 以使其成為 3D。結果的形狀將為 c.shape[3:] + x.shape。

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy.polynomial.polynomial import polyval3d

建立一個 3D 係數陣列 -

c = np.arange(24).reshape(2,2,6)

顯示陣列 -

print("Our Array...
",c)

檢查維度 -

print("
Dimensions of our Array...
",c.ndim)

獲取資料型別 -

print("
Datatype of our Array object...
",c.dtype)

獲取形狀 -

print("
Shape of our Array object...
",c.shape)

要評估點 (x, y, z) 處的 3D 多項式,請使用 polynomial.polyval3d() 方法。該方法返回在由 x、y 和 z 中對應值的三個元組形成的點上多維多項式的值 -

print("
Result...
",polyval3d([1,2],[1,2],[1,2], c))

示例

import numpy as np
from numpy.polynomial.polynomial import polyval3d

# Create a 3d array of coefficients
c = np.arange(24).reshape(2,2,6)

# Display the array
print("Our Array...
",c) # Check the Dimensions print("
Dimensions of our Array...
",c.ndim) # Get the Datatype print("
Datatype of our Array object...
",c.dtype) # Get the Shape print("
Shape of our Array object...
",c.shape) # To evaluate a 3-D polynomial at points (x, y, z), use the polynomial.polyval3d() method in Python Numpy print("
Result...
",polyval3d([1,2],[1,2],[1,2], c))

輸出

Our Array...
[[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]

[[12 13 14 15 16 17]
[18 19 20 21 22 23]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

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

Result...
[ 276. 9126.]

更新於: 2022年2月28日

272 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告