在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, 2])
y = np.array([3, 4])
z = np.array([5, 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, 2])
y = np.array([3, 4])
z = np.array([5, 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 2]

Array2...
   [3 4]

Array3...
   [5 6]

Array1 datatype...
int64

Array2 datatype...
int64

Array3 datatype...
int64

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.00000000e+00 3.70000000e+01 3.05000000e+02
     2.64100000e+03 3.00000000e+00 1.50000000e+01 1.11000000e+02
     9.15000000e+02 7.92300000e+03 1.30000000e+01 6.50000000e+01
     4.81000000e+02 3.96500000e+03 3.43330000e+04 6.30000000e+01
     3.15000000e+02 2.33100000e+03 1.92150000e+04 1.66383000e+05
     1.00000000e+00 5.00000000e+00 3.70000000e+01 3.05000000e+02
     2.64100000e+03 3.00000000e+00 1.50000000e+01 1.11000000e+02
     9.15000000e+02 7.92300000e+03 1.30000000e+01 6.50000000e+01
     4.81000000e+02 3.96500000e+03 3.43330000e+04 6.30000000e+01
     3.15000000e+02 2.33100000e+03 1.92150000e+04 1.66383000e+05
     1.00000000e+00 5.00000000e+00 3.70000000e+01 3.05000000e+02
     2.64100000e+03 3.00000000e+00 1.50000000e+01 1.11000000e+02
     9.15000000e+02 7.92300000e+03 1.30000000e+01 6.50000000e+01
     4.81000000e+02 3.96500000e+03 3.43330000e+04 6.30000000e+01
     3.15000000e+02 2.33100000e+03 1.92150000e+04 1.66383000e+05]
    [1.00000000e+00 6.00000000e+00 5.35000000e+01 5.31000000e+02
     5.53537500e+03 4.00000000e+00 2.40000000e+01 2.14000000e+02
     2.12400000e+03 2.21415000e+04 2.35000000e+01 1.41000000e+02
     1.25725000e+03 1.24785000e+04 1.30081312e+05 1.54000000e+02
     9.24000000e+02 8.23900000e+03 8.17740000e+04 8.52447750e+05
     2.00000000e+00 1.20000000e+01 1.07000000e+02 1.06200000e+03
     1.10707500e+04 8.00000000e+00 4.80000000e+01 4.28000000e+02
     4.24800000e+03 4.42830000e+04 4.70000000e+01 2.82000000e+02
     2.51450000e+03 2.49570000e+04 2.60162625e+05 3.08000000e+02
     1.84800000e+03 1.64780000e+04 1.63548000e+05 1.70489550e+06
     5.50000000e+00 3.30000000e+01 2.94250000e+02 2.92050000e+03
     3.04445625e+04 2.20000000e+01 1.32000000e+02 1.17700000e+03
     1.16820000e+04 1.21778250e+05 1.29250000e+02 7.75500000e+02
     6.91487500e+03 6.86317500e+04 7.15447219e+05 8.47000000e+02
     5.08200000e+03 4.53145000e+04 4.49757000e+05 4.68846262e+06]]

更新于:2022年3月9日

104 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告