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


要生成具有 x、y、z 樣本點的勒讓德多項式的偽範德蒙矩陣,請在 Python Numpy 中使用 legendre.legvander3d() 方法。返回度數為 deg 和樣本點 (x, y, z) 的偽範德蒙矩陣。

引數 x、y、z 是點座標陣列,它們都具有相同的形狀。資料型別將轉換為 float64 或 complex128,具體取決於任何元素是否為複數。標量將轉換為一維陣列。引數 deg 是形式為 [x_deg, y_deg, z_deg] 的最大度數列表。

步驟

首先,匯入所需的庫 -

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

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

x = np.array([1.5, 2.3])
y = np.array([3.7, 4.4])
z = np.array([5.3, 6.6])

顯示陣列 -

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

顯示資料型別 -

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

檢查兩個陣列的維度 -

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

檢查兩個陣列的形狀 -

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

要生成具有 x、y、z 樣本點的勒讓德多項式的偽範德蒙矩陣,請在 Python 中使用 legendre.legvander3d() 方法 -

x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n",L.legvander3d(x,y,z, [x_deg, y_deg, z_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.5, 2.3])
y = np.array([3.7, 4.4])
z = np.array([5.3, 6.6])

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

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

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

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

# To generate a pseudo Vandermonde matrix of the Legendre polynomial with x, y, z sample points, use the legendre.legvander3d() method in Python Numpy
x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n",L.legvander3d(x,y,z, [x_deg, y_deg, z_deg]))

輸出

Array1...
   [1.5 2.3]

Array2...
[3.7 4.4]

Array3...
[5.3 6.6]

Array1 datatype...
float64

Array2 datatype...
float64

Array3 datatype...
float64

Dimensions of Array1...
1

Dimensions of Array2...
1

Dimensions of Array3...
1

Shape of Array1...
(2,)

Shape of Array2...
(2,)

Shape of Array3...
(2,)

Result...
   [[1.00000000e+00 5.30000000e+00 4.16350000e+01 3.64242500e+02
     3.34712294e+03 3.70000000e+00 1.96100000e+01 1.54049500e+02
     1.34769725e+03 1.23843549e+04 2.00350000e+01 1.06185500e+02
     8.34157225e+02 7.29759849e+03 6.70596081e+04 1.21082500e+02
     6.41737250e+02 5.04126989e+03 4.41033925e+04 4.05278013e+05
     1.50000000e+00 7.95000000e+00 6.24525000e+01 5.46363750e+02
     5.02068441e+03 5.55000000e+00 2.94150000e+01 2.31074250e+02
     2.02154588e+03 1.85765323e+04 3.00525000e+01 1.59278250e+02
     1.25123584e+03 1.09463977e+04 1.00589412e+05 1.81623750e+02
     9.62605875e+02 7.56190483e+03 6.61550888e+04 6.07917020e+05
     2.87500000e+00 1.52375000e+01 1.19700625e+02 1.04719719e+03
     9.62297845e+03 1.06375000e+01 5.63787500e+01 4.42892313e+02
     3.87462959e+03 3.56050202e+04 5.76006250e+01 3.05283313e+02
     2.39820202e+03 2.09805957e+04 1.92796373e+05 3.48112188e+02
     1.84499459e+03 1.44936509e+04 1.26797253e+05 1.16517429e+06]
    [1.00000000e+00 6.60000000e+00 6.48400000e+01 7.08840000e+02
     8.13847200e+03 4.40000000e+00 2.90400000e+01 2.85296000e+02
     3.11889600e+03 3.58092768e+04 2.85400000e+01 1.88364000e+02
     1.85053360e+03 2.02302936e+04 2.32271991e+05 2.06360000e+02
     1.36197600e+03 1.33803824e+04 1.46276222e+05 1.67945508e+06
     2.30000000e+00 1.51800000e+01 1.49132000e+02 1.63033200e+03
     1.87184856e+04 1.01200000e+01 6.67920000e+01 6.56180800e+02
     7.17346080e+03 8.23613366e+04 6.56420000e+01 4.33237200e+02
     4.25622728e+03 4.65296753e+04 5.34225579e+05 4.74628000e+02
     3.13254480e+03 3.07748795e+04 3.36435312e+05 3.86274669e+06
     7.43500000e+00 4.90710000e+01 4.82085400e+02 5.27022540e+03
     6.05095393e+04 3.27140000e+01 2.15912400e+02 2.12117576e+03
     2.31889918e+04 2.66241973e+05 2.12194900e+02 1.40048634e+03
     1.37587173e+04 1.50412233e+05 1.72694225e+06 1.53428660e+03
     1.01262916e+04 9.94831431e+04 1.08756371e+06 1.24867485e+07]]

更新於: 2022-03-10

100 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.