在Python中生成给定阶数和x、y、z样本点的伪范德蒙德矩阵


要生成给定阶数和x、y、z样本点的伪范德蒙德矩阵,可以使用Python NumPy中的`polynomial.polyvander3d()`。该方法返回度数为deg且样本点为(x, y, z)的伪范德蒙德矩阵。参数x、y、z是点坐标数组,形状相同。数据类型将根据元素是否为复数转换为float64或complex128。标量将转换为一维数组。参数deg是最大度数列表,格式为[x_deg, y_deg, z_deg]。

步骤

首先,导入所需的库:

import numpy as np
from numpy.polynomial.polynomial import polyvander3d

使用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样本点的伪范德蒙德矩阵,可以使用`polynomial.polyvander3d()`:

x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n",polyvander3d(x,y, z, [x_deg, y_deg, z_deg]))

示例

import numpy as np
from numpy.polynomial.polynomial import polyvander3d

# 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
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
print("\nDimensions of Array3...\n",z.ndim)

# Check the Shape
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 given degree and x, y, z sample points, use the polynomial.polyvander3d() in Python Numpy
x_deg, y_deg, z_deg = 2, 3, 4
print("\nResult...\n",polyvander3d(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.00000e+00 5.00000e+00 2.50000e+01 1.25000e+02 6.25000e+02 3.00000e+00
1.50000e+01 7.50000e+01 3.75000e+02 1.87500e+03 9.00000e+00 4.50000e+01
2.25000e+02 1.12500e+03 5.62500e+03 2.70000e+01 1.35000e+02 6.75000e+02
3.37500e+03 1.68750e+04 1.00000e+00 5.00000e+00 2.50000e+01 1.25000e+02
6.25000e+02 3.00000e+00 1.50000e+01 7.50000e+01 3.75000e+02 1.87500e+03
9.00000e+00 4.50000e+01 2.25000e+02 1.12500e+03 5.62500e+03 2.70000e+01
1.35000e+02 6.75000e+02 3.37500e+03 1.68750e+04 1.00000e+00 5.00000e+00
2.50000e+01 1.25000e+02 6.25000e+02 3.00000e+00 1.50000e+01 7.50000e+01
3.75000e+02 1.87500e+03 9.00000e+00 4.50000e+01 2.25000e+02 1.12500e+03
5.62500e+03 2.70000e+01 1.35000e+02 6.75000e+02 3.37500e+03 1.68750e+04]
[1.00000e+00 6.00000e+00 3.60000e+01 2.16000e+02 1.29600e+03 4.00000e+00
2.40000e+01 1.44000e+02 8.64000e+02 5.18400e+03 1.60000e+01 9.60000e+01
5.76000e+02 3.45600e+03 2.07360e+04 6.40000e+01 3.84000e+02 2.30400e+03
1.38240e+04 8.29440e+04 2.00000e+00 1.20000e+01 7.20000e+01 4.32000e+02
2.59200e+03 8.00000e+00 4.80000e+01 2.88000e+02 1.72800e+03 1.03680e+04
3.20000e+01 1.92000e+02 1.15200e+03 6.91200e+03 4.14720e+04 1.28000e+02
7.68000e+02 4.60800e+03 2.76480e+04 1.65888e+05 4.00000e+00 2.40000e+01
1.44000e+02 8.64000e+02 5.18400e+03 1.60000e+01 9.60000e+01 5.76000e+02
3.45600e+03 2.07360e+04 6.40000e+01 3.84000e+02 2.30400e+03 1.38240e+04
8.29440e+04 2.56000e+02 1.53600e+03 9.21600e+03 5.52960e+04 3.31776e+05]]

更新于:2022年2月28日

浏览量:103

启动您的职业生涯

完成课程获得认证

开始学习
广告