Generate a Pseudo Vandermonde matrix of the Legendre polynomial and x, y, z floating array of points in Python

The pseudo-Vandermonde matrix of Legendre polynomials is a mathematical construct used in polynomial interpolation and approximation. In NumPy, the legendre.legvander3d() function generates this matrix for three-dimensional sample points (x, y, z).

Syntax

numpy.polynomial.legendre.legvander3d(x, y, z, deg)

Parameters

The function accepts the following parameters ?

  • x, y, z ? Arrays of point coordinates with the same shape
  • deg ? List of maximum degrees [x_deg, y_deg, z_deg]

Example

Here's how to generate a pseudo-Vandermonde matrix using three-dimensional sample points ?

import numpy as np
from numpy.polynomial import legendre as L

# Create arrays of point coordinates with the same shape
x = np.array([1.5, 2.3])
y = np.array([3.7, 4.4])
z = np.array([5.3, 6.6])

print("Array x:", x)
print("Array y:", y)
print("Array z:", z)
print("Data type:", x.dtype)
print("Shape:", x.shape)

# Generate pseudo-Vandermonde matrix with degrees [2, 3, 4]
x_deg, y_deg, z_deg = 2, 3, 4
result = L.legvander3d(x, y, z, [x_deg, y_deg, z_deg])

print("\nPseudo-Vandermonde matrix shape:", result.shape)
print("Matrix (first few elements):")
print(result[:, :5])  # Show first 5 columns for readability
Array x: [1.5 2.3]
Array y: [3.7 4.4]
Array z: [5.3 6.6]
Data type: float64
Shape: (2,)

Pseudo-Vandermonde matrix shape: (2, 60)
Matrix (first few elements):
[[1.00000000e+00 5.30000000e+00 4.16350000e+01 3.64242500e+02
  3.34712294e+03]
 [1.00000000e+00 6.60000000e+00 6.48400000e+01 7.08840000e+02
  8.13847200e+03]]

Understanding the Output

The resulting matrix has dimensions (n_points, (x_deg+1)*(y_deg+1)*(z_deg+1)). For degrees [2, 3, 4], this gives us (2, 60) since (2+1)*(3+1)*(4+1) = 3*4*5 = 60 columns.

import numpy as np
from numpy.polynomial import legendre as L

# Simple example with lower degrees for clarity
x = np.array([1.0, 2.0])
y = np.array([1.0, 2.0]) 
z = np.array([1.0, 2.0])

# Use degrees [1, 1, 1] for a smaller matrix
result = L.legvander3d(x, y, z, [1, 1, 1])
print("Matrix with degrees [1,1,1]:")
print("Shape:", result.shape)
print(result)
Matrix with degrees [1,1,1]:
Shape: (2, 8)
[[ 1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  2.  4.  8.  2.  4.  8. 16.]]

Conclusion

The legendre.legvander3d() function efficiently generates pseudo-Vandermonde matrices for three-dimensional Legendre polynomial evaluation. The matrix dimensions depend on the specified polynomial degrees, making it useful for multivariate polynomial fitting and interpolation.

Updated on: 2026-03-26T21:11:07+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements