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 in Python
To generate a Pseudo-Vandermonde matrix of given degree, use the polynomial.polyvander2d() in Python NumPy. The method returns the pseudo-Vandermonde matrix of degrees deg and sample points (x, y). The parameter, x and y, are arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. The parameter, deg is a list of maximum degrees of the form [x_deg, y_deg].
Syntax
numpy.polynomial.polynomial.polyvander2d(x, y, deg)
Parameters
- x, y − Arrays of point coordinates, all of the same shape
- deg − List of maximum degrees of the form [x_deg, y_deg]
Example
Let's create arrays of point coordinates and generate a pseudo-Vandermonde matrix ?
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
# Create arrays of point coordinates
x = np.array([1, 2])
y = np.array([3, 4])
print("Array1...")
print(x)
print("\nArray2...")
print(y)
print("\nArray1 datatype...")
print(x.dtype)
print("\nArray2 datatype...")
print(y.dtype)
print("\nDimensions of Array1...")
print(x.ndim)
print("\nDimensions of Array2...")
print(y.ndim)
print("\nShape of Array1...")
print(x.shape)
print("\nShape of Array2...")
print(y.shape)
# Generate Pseudo-Vandermonde matrix
x_deg, y_deg = 2, 3
result = polyvander2d(x, y, [x_deg, y_deg])
print("\nPseudo-Vandermonde Matrix...")
print(result)
Array1... [1 2] Array2... [3 4] Array1 datatype... int64 Array2 datatype... int64 Dimensions of Array1... 1 Dimensions of Array2... 1 Shape of Array1... (2,) Shape of Array2... (2,) Pseudo-Vandermonde Matrix... [[ 1. 3. 9. 27. 1. 3. 9. 27. 1. 3. 9. 27.] [ 1. 4. 16. 64. 2. 8. 32. 128. 4. 16. 64. 256.]]
How It Works
The pseudo-Vandermonde matrix is constructed by evaluating polynomial basis functions at the given points. For degrees [2, 3], the matrix contains columns corresponding to:
- x^0 * y^0, x^0 * y^1, x^0 * y^2, x^0 * y^3
- x^1 * y^0, x^1 * y^1, x^1 * y^2, x^1 * y^3
- x^2 * y^0, x^2 * y^1, x^2 * y^2, x^2 * y^3
Different Degree Example
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
x = np.array([1, 2, 3])
y = np.array([2, 3, 4])
# Generate with different degrees
result = polyvander2d(x, y, [1, 2])
print("Pseudo-Vandermonde Matrix with degrees [1, 2]...")
print(result)
print("\nShape:", result.shape)
Pseudo-Vandermonde Matrix with degrees [1, 2]... [[ 1. 2. 4. 1. 2. 4.] [ 1. 3. 9. 2. 6. 18.] [ 1. 4. 16. 3. 12. 48.]] Shape: (3, 6)
Conclusion
The polyvander2d() function generates pseudo-Vandermonde matrices useful for polynomial fitting and interpolation. The matrix size depends on the specified degrees: (x_deg+1) * (y_deg+1) columns for each input point.
