在 Python 中生成勒让德多项式的伪范德蒙矩阵和 x、y、z 点数组
pythonnumpyserver side programmingprogramming
要生成具有 x、y、z 样本点的勒让德多项式的伪范德蒙矩阵,请使用 Python Numpy 中的 legendre.legvander3d() 方法。返回度数 deg 和样本点 (x、y、z) 的伪范德蒙矩阵。
参数 x、y、z 是点坐标数组,所有数组的形状相同。dtype 将转换为 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("\n结果...\n",L.legvander3d(x,y,z, [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 Numpy 中的 legendre.legvander3d() 方法 x_deg, y_deg, z_deg = 2, 3, 4 print("\n结果...\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,) 结果... [[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]]