在 Python 中生成勒讓德多項式和 x、y 點陣列的偽範德蒙矩陣


要生成勒讓德多項式的偽範德蒙矩陣,請在 Python NumPy 中使用 legendre.legvander2d() 方法。該方法返回偽範德蒙矩陣。返回矩陣的形狀為 x.shape + (deg + 1,),其中最後一個索引是相應勒讓德多項式的階數。dtype 將與轉換後的 x 相同。

引數 x、y 是點座標陣列,所有陣列都具有相同的形狀。根據元素是否為複數,dtype 將轉換為 float64 或 complex128。標量將轉換為一維陣列。引數 deg 是 [x_deg, y_deg] 形式的最大階數列表。

步驟

首先,匯入所需的庫 -

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

使用 numpy.array() 方法建立具有相同形狀的點座標陣列 -

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

顯示陣列 -

print("Array1...\n",x)
print("\nArray2...\n",y)

顯示資料型別 -

print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)

檢查兩個陣列的維度 -

print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)

檢查兩個陣列的形狀 -

print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)

要生成勒讓德多項式的偽範德蒙矩陣,請在 Python NumPy 中使用 legendre.legvander2d() 方法 -

x_deg, y_deg = 2, 3
print("\nResult...\n",L.legvander2d(x,y, [x_deg, y_deg]))

示例

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

# Create arrays of point coordinates, all of the same shape using the numpy.array() method
x = np.array([1, 2])
y = np.array([3, 4])

# Display the arrays
print("Array1...\n",x)
print("\nArray2...\n",y)

# Display the datatype
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)

# To generate a pseudo Vandermonde matrix of the Legendre polynomial, use the legendre.legvander2d() method in Python Numpy

x_deg, y_deg = 2, 3
print("\nResult...\n",L.legvander2d(x,y, [x_deg, y_deg]))

輸出

Array1...
   [1 2]

Array2...
   [3 4]

Array1 datatype...
int64

Array2 datatype...
int64

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(2,)

Shape of Array2...
(2,)

Result...
   [[ 1. 3. 13. 63. 1. 3. 13. 63. 1. 3.
     13. 63. ]
   [ 1. 4. 23.5 154. 2. 8. 47. 308. 5.5 22.
    129.25 847. ]]

更新於: 2022-03-09

122 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.