在 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([-2.+2.j, -1.+2.j]) y = np.array([0.+2.j, 1.+2.j]) z = np.array([2.+2.j, 3. + 3.j])
显示数组−
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]))
示例
import numpy as np from numpy.polynomial import legendre as L # 使用 numpy.array() 方法创建点坐标数组,所有数组的形状相同 x = np.array([-2.+2.j, -1.+2.j]) y = np.array([0.+2.j, 1.+2.j]) z = np.array([2.+2.j, 3. + 3.j]) # 显示数组 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... [-2.+2.j -1.+2.j] Array2... [0.+2.j 1.+2.j] Array3... [2.+2.j 3.+3.j] Array1 datatype... complex128 Array2 datatype... complex128 Array3 datatype... complex128 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 +0.0000000e+00j 2.00000000e+00 +2.0000000e+00j -5.00000000e-01 +1.2000000e+01j -4.30000000e+01 +3.7000000e+01j -2.79625000e+02 -3.0000000e+01j 0.00000000e+00 +2.0000000e+00j -4.00000000e+00 +4.0000000e+00j -2.40000000e+01 -1.0000000e+00j -7.40000000e+01 -8.6000000e+01j 6.00000000e+01 -5.5925000e+02j -6.50000000e+00 +0.0000000e+00j -1.30000000e+01 -1.3000000e+01j 3.25000000e+00 -7.8000000e+01j 2.79500000e+02 -2.4050000e+02j 1.81756250e+03 +1.9500000e+02j 0.00000000e+00 -2.3000000e+01j 4.60000000e+01 -4.6000000e+01j 2.76000000e+02 +1.1500000e+01j 8.51000000e+02 +9.8900000e+02j -6.90000000e+02 +6.4313750e+03j -2.00000000e+00 +2.0000000e+00j -8.00000000e+00 +0.0000000e+00j -2.30000000e+01 -2.5000000e+01j 1.20000000e+01 -1.6000000e+02j 6.19250000e+02 -4.9925000e+02j -4.00000000e+00 -4.0000000e+00j 0.00000000e+00 -1.6000000e+01j 5.00000000e+01 -4.6000000e+01j 3.20000000e+02 +2.4000000e+01j 9.98500000e+02 +1.2385000e+03j 1.30000000e+01 -1.3000000e+01j 5.20000000e+01 +0.0000000e+00j 1.49500000e+02 +1.6250000e+02j -7.80000000e+01 +1.0400000e+03j -4.02512500e+03 +3.2451250e+03j 4.60000000e+01 +4.6000000e+01j 0.00000000e+00 +1.8400000e+02j -5.75000000e+02 +5.2900000e+02j -3.68000000e+03 -2.7600000e+02j -1.14827500e+04 -1.4242750e+04j -5.00000000e-01 -1.2000000e+01j 2.30000000e+01 -2.5000000e+01j 1.44250000e+02 +0.0000000e+00j 4.65500000e+02 +4.9750000e+02j -2.20187500e+02 +3.3705000e+03j 2.40000000e+01 -1.0000000e+00j 5.00000000e+01 +4.6000000e+01j 0.00000000e+00 +2.8850000e+02j -9.95000000e+02 +9.3100000e+02j -6.74100000e+03 -4.4037500e+02j 3.25000000e+00 +7.8000000e+01j -1.49500000e+02 +1.6250000e+02j -9.37625000e+02 +0.0000000e+00j -3.02575000e+03 -3.2337500e+03j 1.43121875e+03 -2.1908250e+04j -2.76000000e+02 +1.1500000e+01j -5.75000000e+02 -5.2900000e+02j 0.00000000e+00 -3.3177500e+03j 1.14425000e+04 -1.0706500e+04j 7.75215000e+04 +5.0643125e+03j] [ 1.00000000e+00 +0.0000000e+00j 3.00000000e+00 +3.0000000e+00j -5.00000000e-01 +2.7000000e+01j -1.39500000e+02 +1.3050000e+02j -1.41712500e+03 -6.7500000e+01j 1.00000000e+00 +2.0000000e+00j -3.00000000e+00 +9.0000000e+00j -5.45000000e+01 +2.6000000e+01j -4.00500000e+02 -1.4850000e+02j -1.28212500e+03 -2.9017500e+03j -5.00000000e+00 +6.0000000e+00j -3.30000000e+01 +3.0000000e+00j -1.59500000e+02 -1.3800000e+02j -8.55000000e+01 -1.4895000e+03j 7.49062500e+03 -8.1652500e+03j -2.90000000e+01 -8.0000000e+00j -6.30000000e+01 -1.1100000e+02j 2.30500000e+02 -7.7900000e+02j 5.08950000e+03 -2.6685000e+03j 4.05566250e+04 +1.3294500e+04j -1.00000000e+00 +2.0000000e+00j -9.00000000e+00 +3.0000000e+00j -5.35000000e+01 -2.8000000e+01j -1.21500000e+02 -4.0950000e+02j 1.55212500e+03 -2.7667500e+03j -5.00000000e+00 +0.0000000e+00j -1.50000000e+01 -1.5000000e+01j 2.50000000e+00 -1.3500000e+02j 6.97500000e+02 -6.5250000e+02j 7.08562500e+03 +3.3750000e+02j -7.00000000e+00 -1.6000000e+01j 2.70000000e+01 -6.9000000e+01j 4.35500000e+02- 1.8100000e+02j 3.06450000e+03 +1.3185000e+03j 8.83987500e+03 +2.3146500e+04j 4.50000000e+01 -5.0000000e+01j 2.85000000e+02 -1.5000000e+01j 1.32750000e+03 +1.2400000e+03j 2.47500000e+02 +1.2847500e+04j -6.71456250e+04 +6.7818750e+04j -5.00000000e+00 -6.0000000e+00j 3.00000000e+00 -3.3000000e+01j 1.64500000e+02 -1.3200000e+02j 1.48050000e+03 +1.8450000e+02j 6.68062500e+03 +8.8402500e+03j 7.00000000e+00 -1.6000000e+01j 6.90000000e+01 -2.7000000e+01j 4.28500000e+02 +1.9700000e+02j 1.11150000e+03 +3.1455000e+03j -1.09998750e+04 +2.2201500e+04j 6.10000000e+01 +0.0000000e+00j 1.83000000e+02 +1.8300000e+02j -3.05000000e+01 +1.6470000e+03j -8.50950000e+03 +7.9605000e+03j -8.64446250e+04 -4.1175000e+03j 9.70000000e+01 +2.1400000e+02j -3.51000000e+02 +9.3300000e+02j -5.82650000e+03 +2.5120000e+03j -4.14585000e+04 -1.7194500e+04j -1.23016125e+05 -3.0981225e+05j]]