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 Vandermonde matrix of the Legendre polynomial with float array of points in Python
A Vandermonde matrix of the Legendre polynomial is useful for polynomial fitting and interpolation. NumPy provides the legvander() method to generate this matrix efficiently with float array points.
Syntax
numpy.polynomial.legendre.legvander(x, deg)
Parameters
- x ? Array of points. Converted to float64 or complex128 depending on element types
- deg ? Degree of the resulting matrix. Must be non-negative integer
Return Value
Returns the pseudo-Vandermonde matrix with shape x.shape + (deg + 1,). The last index corresponds to the degree of the Legendre polynomial.
Example
Generate a Vandermonde matrix of degree 2 for float array points ?
import numpy as np
from numpy.polynomial import legendre as L
# Create an array of float points
x = np.array([0, 3.5, -1.4, 2.5])
# Display the array
print("Our Array...")
print(x)
# Check array properties
print("\nDimensions:", x.ndim)
print("Datatype:", x.dtype)
print("Shape:", x.shape)
# Generate Vandermonde matrix of degree 2
result = L.legvander(x, 2)
print("\nVandermonde Matrix:")
print(result)
Our Array... [ 0. 3.5 -1.4 2.5] Dimensions: 1 Datatype: float64 Shape: (4,) Vandermonde Matrix: [[ 1. 0. -0.5 ] [ 1. 3.5 17.875 ] [ 1. -1.4 2.44 ] [ 1. 2.5 8.875 ]]
How It Works
Each row corresponds to a point in the input array x. Each column represents the Legendre polynomial of increasing degree (0, 1, 2) evaluated at those points:
- Column 0 ? L?(x) = 1 (constant)
- Column 1 ? L?(x) = x (linear)
- Column 2 ? L?(x) = ½(3x² - 1) (quadratic)
Conclusion
The legvander() method generates Vandermonde matrices for Legendre polynomials efficiently. This is essential for polynomial fitting, interpolation, and numerical analysis applications.
