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

To generate a pseudo Vandermonde matrix of the Legendre polynomial with x, y, z sample points, use the legendre.legvander3d() method in NumPy. This function returns a 3D pseudo-Vandermonde matrix of degrees deg and sample points (x, y, z).

The parameters x, y, z 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, z_deg].

Syntax

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

Parameters

x, y, z − Arrays of point coordinates, all of the same shape

deg − List of maximum degrees of the form [x_deg, y_deg, z_deg]

Example

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

# Create arrays of point coordinates with complex values
x = np.array([-2.+2.j, -1.+2.j])
y = np.array([0.+2.j, 1.+2.j])
z = np.array([2.+2.j, 3.+3.j])

# Display the arrays
print("Array x:", x)
print("Array y:", y)
print("Array z:", z)

# Display the datatype
print("\nDatatype x:", x.dtype)
print("Datatype y:", y.dtype)
print("Datatype z:", z.dtype)

# Check dimensions and shape
print("\nDimensions - x:", x.ndim, "y:", y.ndim, "z:", z.ndim)
print("Shape - x:", x.shape, "y:", y.shape, "z:", z.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("Result (first few values):")
print(result[0, :5])  # Show first 5 values of first row
Array x: [-2.+2.j -1.+2.j]
Array y: [0.+2.j 1.+2.j]
Array z: [2.+2.j 3.+3.j]

Datatype x: complex128
Datatype y: complex128
Datatype z: complex128

Dimensions - x: 1 y: 1 z: 1
Shape - x: (2,) y: (2,) z: (2,)

Pseudo Vandermonde matrix shape: (2, 60)
Result (first few values):
[ 1.  +0.j   2.  +2.j  -0.5+12.j -43. +37.j -279.625-30.j]

How It Works

The legvander3d() function creates a 3D pseudo-Vandermonde matrix where each column represents evaluations of Legendre polynomial products at the given points. For degrees [2, 3, 4], the resulting matrix has dimensions (n_points, (x_deg+1)*(y_deg+1)*(z_deg+1)) = (2, 3*4*5) = (2, 60).

Complete Example with Different Degrees

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

# Create sample points
x = np.array([1.0, 2.0])
y = np.array([0.5, 1.5]) 
z = np.array([0.0, 1.0])

# Generate matrices with different degree combinations
deg1 = [1, 1, 1]  # Lower degrees
deg2 = [2, 2, 2]  # Higher degrees

matrix1 = L.legvander3d(x, y, z, deg1)
matrix2 = L.legvander3d(x, y, z, deg2)

print("Matrix with degrees [1,1,1] shape:", matrix1.shape)
print("Matrix with degrees [2,2,2] shape:", matrix2.shape)
print("\nMatrix 1 (degrees [1,1,1]):")
print(matrix1)
Matrix with degrees [1,1,1] shape: (2, 8)
Matrix with degrees [2,2,2] shape: (2, 27)

Matrix 1 (degrees [1,1,1]):
[[1.   0.   0.5  0.   1.   0.   0.5  0. ]
 [1.   1.   1.5  1.5  2.   2.   3.   3. ]]

Conclusion

The legendre.legvander3d() function efficiently generates 3D pseudo-Vandermonde matrices for Legendre polynomials. The matrix dimensions depend on the degree parameters, with higher degrees producing larger matrices suitable for polynomial fitting and evaluation tasks.

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

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements