Generate a Vandermonde matrix of the Legendre series in Python

To generate a pseudo Vandermonde matrix of the Legendre polynomial, use the polynomial.legvander() method in NumPy. This function creates a matrix where each row represents the Legendre polynomial evaluated at a specific point.

The method returns the pseudo-Vandermonde matrix with shape x.shape + (deg + 1,), where the last index corresponds to the degree of the Legendre polynomial. The dtype matches the converted input array x.

Syntax

numpy.polynomial.legendre.legvander(x, deg)

Parameters

The function accepts the following parameters:

  • x ? Array of points. Converted to float64 or complex128 depending on complexity. Scalars are converted to 1-D arrays.
  • deg ? Degree of the resulting matrix (non-negative integer).

Example

Let's create a Vandermonde matrix of Legendre polynomials with degree 2:

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

# Create an array of points
x = np.array([0, 1, -1, 2])

# Display the array
print("Input Array:")
print(x)

# Check array properties
print("\nArray Properties:")
print("Dimensions:", x.ndim)
print("Datatype:", x.dtype)
print("Shape:", x.shape)

# Generate Vandermonde matrix of Legendre polynomials (degree 2)
result = L.legvander(x, 2)
print("\nVandermonde Matrix:")
print(result)
Input Array:
[ 0  1 -1  2]

Array Properties:
Dimensions: 1
Datatype: int64
Shape: (4,)

Vandermonde Matrix:
[[ 1.   0.  -0.5]
 [ 1.   1.   1. ]
 [ 1.  -1.   1. ]
 [ 1.   2.   5.5]]

How It Works

The Vandermonde matrix contains Legendre polynomials of degrees 0, 1, and 2:

  • Column 0: L?(x) = 1 (constant polynomial)
  • Column 1: L?(x) = x (linear polynomial)
  • Column 2: L?(x) = (3x² - 1)/2 (quadratic polynomial)

Higher Degree Example

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

# Create points
x = np.array([0, 0.5, 1])

# Generate matrix with degree 3
matrix = L.legvander(x, 3)
print("Degree 3 Vandermonde Matrix:")
print(matrix)
print("\nShape:", matrix.shape)
Degree 3 Vandermonde Matrix:
[[ 1.     0.    -0.5   -0.   ]
 [ 1.     0.5   -0.125 -0.4375]
 [ 1.     1.     1.     1.    ]]

Shape: (3, 4)

Conclusion

The legvander() function efficiently generates Vandermonde matrices for Legendre polynomial fitting and interpolation. Each row evaluates all polynomial degrees at a specific point, making it useful for numerical analysis and curve fitting applications.

Updated on: 2026-03-26T21:02:42+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements