Generate a Pseudo-Vandermonde matrix of given degree with float array of points coordinates 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 parameters 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. 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 a complete example showing how to generate a Pseudo-Vandermonde matrix ?

import numpy as np
from numpy.polynomial.polynomial import polyvander2d

# Create arrays of point coordinates, all of the same shape
x = np.array([0.1, 1.4])
y = np.array([1.7, 2.8])

# Display the arrays
print("Array1...\n", x)
print("\nArray2...\n", y)

# Display the datatype
print("\nArray1 datatype...\n", x.dtype)
print("\nArray2 datatype...\n", y.dtype)

# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n", x.ndim)
print("\nDimensions of Array2...\n", y.ndim)

# Check the Shape of both the arrays
print("\nShape of Array1...\n", x.shape)
print("\nShape of Array2...\n", y.shape)

# Generate Pseudo-Vandermonde matrix with degrees [2, 3]
x_deg, y_deg = 2, 3
result = polyvander2d(x, y, [x_deg, y_deg])
print("\nResult...\n", result)
Array1...
 [0.1 1.4]

Array2...
 [1.7 2.8]

Array1 datatype...
float64

Array2 datatype...
float64

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(2,)

Shape of Array2...
(2,)

Result...
 [[1.000000e+00 1.700000e+00 2.890000e+00 4.913000e+00 1.000000e-01
  1.700000e-01 2.890000e-01 4.913000e-01 1.000000e-02 1.700000e-02
  2.890000e-02 4.913000e-02]
 [1.000000e+00 2.800000e+00 7.840000e+00 2.195200e+01 1.400000e+00
  3.920000e+00 1.097600e+01 3.073280e+01 1.960000e+00 5.488000e+00
  1.536640e+01 4.302592e+01]]

How It Works

The Pseudo-Vandermonde matrix is constructed by evaluating polynomial basis functions at given points. For degrees [x_deg, y_deg] = [2, 3], the matrix contains columns for all combinations:

xi * yj where i ? x_deg and j ? y_deg

This results in (x_deg + 1) * (y_deg + 1) = 3 * 4 = 12 columns in the output matrix.

Different Degrees Example

Let's see how changing the degrees affects the matrix dimensions ?

import numpy as np
from numpy.polynomial.polynomial import polyvander2d

x = np.array([1.0, 2.0])
y = np.array([3.0, 4.0])

# Different degree combinations
degrees = [[1, 1], [2, 2], [1, 2]]

for deg in degrees:
    result = polyvander2d(x, y, deg)
    print(f"Degrees {deg}: Matrix shape {result.shape}")
    print(f"Matrix:\n{result}\n")
Degrees [1, 1]: Matrix shape (2, 4)
Matrix:
[[1. 3. 1. 3.]
 [1. 4. 2. 8.]]

Degrees [2, 2]: Matrix shape (2, 9)
Matrix:
[[ 1.  3.  9.  1.  3.  9.  1.  3.  9.]
 [ 1.  4. 16.  2.  8. 32.  4. 16. 64.]]

Degrees [1, 2]: Matrix shape (2, 6)
Matrix:
[[ 1.  3.  9.  1.  3.  9.]
 [ 1.  4. 16.  2.  8. 32.]]

Conclusion

Use polyvander2d() to generate Pseudo-Vandermonde matrices for polynomial fitting in 2D. The degrees parameter [x_deg, y_deg] determines the matrix dimensions as (n_points, (x_deg+1)*(y_deg+1)).

Updated on: 2026-03-26T19:49:50+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements