在 Python 中評估多維陣列點 x 上的拉蓋爾級數


在 Python NumPy 中,可以使用 `polynomial.laguerre.lagval()` 方法評估多維陣列點 x 上的拉蓋爾級數。第一個引數是 x。如果 x 是列表或元組,則將其轉換為 ndarray,否則將其保持不變並視為標量。無論哪種情況,x 或其元素都必須支援自身以及 c 的元素之間的加法和乘法。

第二個引數 C 是一個係數陣列,這些係數按順序排列,以便 n 次項的係數包含在 c[n] 中。如果 c 是多維的,則其餘索引列舉多個多項式。在二維情況下,可以認為係數儲存在 c 的列中。

第三個引數 tensor,如果為 True,則係數陣列的形狀在右側擴充套件為 1,每個維度對應 x 的一個維度。對於此操作,標量具有維度 0。結果是 c 中的每一列係數都針對 x 的每個元素進行評估。如果為 False,則 x 在評估時會廣播到 c 的列上。當 c 是多維時,此關鍵字很有用。預設值為 True。

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy.polynomial import laguerre as L

建立一個係數陣列 -

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 是一個 2D 陣列 -

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

在 Python NumPy 中,可以使用 `polynomial.laguerre.lagval()` 方法評估多維陣列點 x 上的拉蓋爾級數 -

print("\nResult...\n",L.lagval(x,c))

示例

import numpy as np
from numpy.polynomial import laguerre as L

# 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)

# Here, x is a 2D array
x = np.array([[1,2],[3,4]])

# To evaluate a Laguerre series at multi-dimensional array of points x, use the polynomial.laguerre.lagval() method in Python Numpy
print("\nResult...\n",L.lagval(x,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.5 -4. ]
   [-4.5 -2. ]]

更新於: 2022-03-04

97 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告