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 Chebyshev polynomial in Python
To generate a Vandermonde matrix of the Chebyshev polynomial, use the chebyshev.chebvander() function in NumPy. The method returns the Vandermonde matrix where each column represents a different degree of the Chebyshev polynomial. The shape of the returned matrix is x.shape + (deg + 1,), where the last index corresponds to the degree of the Chebyshev polynomial.
The function takes two parameters: x is an array of points (converted to float64 or complex128), and deg is the degree of the resulting matrix.
Syntax
numpy.polynomial.chebyshev.chebvander(x, deg)
Parameters
x: Array of points. The dtype is converted to float64 or complex128 depending on whether any elements are complex. If x is scalar, it is converted to a 1-D array.
deg: Degree of the resulting matrix. Must be a non-negative integer.
Example
Let's create a Vandermonde matrix of degree 2 for a set of points ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Create an array of points
x = np.array([0, 1, -1, 2])
# Display the array
print("Our Array:")
print(x)
# Check the dimensions and properties
print("\nDimensions of our Array:", x.ndim)
print("Datatype of our Array:", x.dtype)
print("Shape of our Array:", x.shape)
# Generate Vandermonde matrix of degree 2
result = C.chebvander(x, 2)
print("\nVandermonde matrix:")
print(result)
Our Array: [ 0 1 -1 2] Dimensions of our Array: 1 Datatype of our Array: int64 Shape of our Array: (4,) Vandermonde matrix: [[ 1. 0. -1.] [ 1. 1. 1.] [ 1. -1. 1.] [ 1. 2. 7.]]
How It Works
The Vandermonde matrix has three columns (degree + 1 = 3) representing Chebyshev polynomials of degrees 0, 1, and 2:
- Column 0: T?(x) = 1 for all x
- Column 1: T?(x) = x
- Column 2: T?(x) = 2x² - 1
Different Degrees
You can create matrices with different degrees ?
import numpy as np
from numpy.polynomial import chebyshev as C
x = np.array([0, 1, -1])
# Degree 1 matrix
print("Degree 1 matrix:")
print(C.chebvander(x, 1))
# Degree 3 matrix
print("\nDegree 3 matrix:")
print(C.chebvander(x, 3))
Degree 1 matrix: [[ 1. 0.] [ 1. 1.] [ 1. -1.]] Degree 3 matrix: [[ 1. 0. -1. 0.] [ 1. 1. 1. 1.] [ 1. -1. 1. -1.]]
Conclusion
The chebvander() function efficiently generates Vandermonde matrices for Chebyshev polynomials. Each row corresponds to a point in your input array, and each column represents a different polynomial degree, making it useful for polynomial fitting and numerical analysis.
