Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Generate a Pseudo-Vandermonde matrix of given degree and x, y, z sample points in Python
To generate a pseudo Vandermonde matrix of given degree and x, y, z sample points, use the polynomial.polyvander3d() function in NumPy. This method returns a pseudo-Vandermonde matrix of degrees deg and sample points (x, y, z). The parameters x, y, z are arrays of point coordinates with the same shape, and deg is a list of maximum degrees of the form [x_deg, y_deg, z_deg].
Syntax
numpy.polynomial.polynomial.polyvander3d(x, y, z, deg)
Parameters
x, y, z: Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any elements are complex. Scalars are converted to 1-D arrays.
deg: List of maximum degrees of the form [x_deg, y_deg, z_deg].
Example
Let's create arrays of point coordinates and generate a pseudo Vandermonde matrix ?
import numpy as np
from numpy.polynomial.polynomial import polyvander3d
# Create arrays of point coordinates with the same shape
x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([5, 6])
print("Array x:", x)
print("Array y:", y)
print("Array z:", z)
print("Shape:", x.shape)
# Generate pseudo Vandermonde matrix with degrees [2, 3, 4]
x_deg, y_deg, z_deg = 2, 3, 4
result = polyvander3d(x, y, z, [x_deg, y_deg, z_deg])
print("\nPseudo Vandermonde Matrix shape:", result.shape)
print("\nFirst few columns of the matrix:")
print(result[:, :10])
Array x: [1 2] Array y: [3 4] Array z: [5 6] Shape: (2,) Pseudo Vandermonde Matrix shape: (2, 60) First few columns of the matrix: [[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] [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]]
Understanding the Output
The pseudo Vandermonde matrix has shape (2, 60) because:
- 2 sample points (length of input arrays)
- 60 columns = (x_deg + 1) × (y_deg + 1) × (z_deg + 1) = 3 × 4 × 5 = 60
Each row corresponds to a sample point, and each column represents a term in the polynomial expansion x^i × y^j × z^k where i ? x_deg, j ? y_deg, k ? z_deg.
Example with Different Degrees
Let's try with smaller degrees to see a more manageable output ?
import numpy as np
from numpy.polynomial.polynomial import polyvander3d
# Create sample points
x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([5, 6])
# Generate matrix with degrees [1, 1, 1]
result = polyvander3d(x, y, z, [1, 1, 1])
print("Matrix with degrees [1, 1, 1]:")
print("Shape:", result.shape)
print("Matrix:")
print(result)
Matrix with degrees [1, 1, 1]: Shape: (2, 8) Matrix: [[ 1. 5. 3. 15. 1. 5. 3. 15.] [ 1. 6. 4. 24. 2. 12. 8. 48.]]
Conclusion
The polyvander3d() function generates a pseudo Vandermonde matrix for 3D polynomial fitting. The matrix dimensions depend on the degree parameters, with each row representing a sample point and columns representing polynomial terms up to the specified degrees.
