Generate a Vandermonde matrix of the Legendre series in Python using NumPy
Last Updated :
26 Apr, 2022
In this article, we will be looking toward the approach to generating a Vandermonde matrix of the Legendre series in Python using NumPy.
Example:
Array:
[-1 2 -3 4 -5]
Result:
[[ 1. -1. 1. ]
[ 1. 2. 5.5]
[ 1. -3. 13. ]
[ 1. 4. 23.5]
[ 1. -5. 37. ]]
To generate a pseudo Vandermonde matrix of the Legendre polynomial with an array, the user has to call the NumPy.legvander() method in Python Numpy. This will return the pseudo-Vandermonde matrix the shape of the returned matrix is x.shape + (deg + 1,), where The last index is the degree of the corresponding Legendre polynomial.
Syntax : np.legvander(x, deg)
Parameters:
- x :[ array_like ] Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex.
- deg :[int] Degree of the resulting matrix.
Return : Return the matrix having size i.e array.size + (degree + 1).
Example:
In this example, we are firstly creating an array with five data points of the float data type, and further, with the NumPy.legvander() method, we are generating a Vandermonde matrix of the Legendre polynomial with 2 degrees in python.
Python3
import numpy as np
from numpy.polynomial import legendre
gfg_data = np.array([-1,2,-3,4,-5])
# Display Elements of Array
print("Array:\n",gfg_data)
# Display Dimensions of Array
print("\nDimensions:\n",gfg_data.ndim)
# To generate a pseudo Vandermonde
# matrix of the Legendre polynomial
gfg=legendre.legvander(gfg_data, 2)
print("\nResult:\n",gfg_data)
Output:
Array:
[-1 2 -3 4 -5]
Dimensions:
1
Result:
[[ 1. -1. 1. ]
[ 1. 2. 5.5]
[ 1. -3. 13. ]
[ 1. 4. 23.5]
[ 1. -5. 37. ]]
Example:
In this example, we are firstly creating an array with ten data points of the float data type, and further, with the NumPy.legvander() method, we are generating a Vandermonde matrix of the Legendre polynomial with 5 degrees in python.
Python3
import numpy as np
from numpy.polynomial import legendre
gfg_data = np.array([-1, 2, -3, 4, -5, 6, -7, 8, -9, 10])
# Display Elements of Array
print("Array:\n", gfg_data)
# Display Dimensions of Array
print("\nDimensions:\n", gfg_data.ndim)
# To generate a pseudo Vandermonde
# matrix of the Legendre polynomial
gfg_data = legendre.legvander(gfg_data, 5)
print("\nResult:\n", gfg_data)
Output:
Array:
[-1 2 -3 4 -5 6 -7 8 -9 10]
Dimensions:
1
Result:
[[ 1.0000000e+00 -1.0000000e+00 1.0000000e+00 -1.0000000e+00
1.0000000e+00 -1.0000000e+00]
[ 1.0000000e+00 2.0000000e+00 5.5000000e+00 1.7000000e+01
5.5375000e+01 1.8575000e+02]
[ 1.0000000e+00 -3.0000000e+00 1.3000000e+01 -6.3000000e+01
3.2100000e+02 -1.6830000e+03]
[ 1.0000000e+00 4.0000000e+00 2.3500000e+01 1.5400000e+02
1.0603750e+03 7.5115000e+03]
[ 1.0000000e+00 -5.0000000e+00 3.7000000e+01 -3.0500000e+02
2.6410000e+03 -2.3525000e+04]
[ 1.0000000e+00 6.0000000e+00 5.3500000e+01 5.3100000e+02
5.5353750e+03 5.9357250e+04]
[ 1.0000000e+00 -7.0000000e+00 7.3000000e+01 -8.4700000e+02
1.0321000e+04 -1.2936700e+05]
[ 1.0000000e+00 8.0000000e+00 9.5500000e+01 1.2680000e+03
1.7680375e+04 2.5358300e+05]
[ 1.0000000e+00 -9.0000000e+00 1.2100000e+02 -1.8090000e+03
2.8401000e+04 -4.5864900e+05]
[ 1.0000000e+00 1.0000000e+01 1.4950000e+02 2.4850000e+03
4.3375375e+04 7.7876875e+05]]
Similar Reads
Generate a Vandermonde matrix of given degree using NumPy in Python In this article, we will cover generating a Vandermonde matrix of a given degree in Python using NumPy. In algebra, a Vandermonde matrix is an m*n matrix that has the terms of a geometric progression in each row. The matrix generated will be of the form : [1 x11 x12 ........ x1(n-1) ................
3 min read
Generate a Vandermonde Matrix of the Legendre Polynomial with Float Array of Points in Python using NumPy In this article, we will be looking at the approach to generating a Vandermonde matrix of the Legendre polynomial with a float array of points in Python using NumPy. Example: Array: [-1.57 0.58 -3.57 1.44 2.75] Result: [[ 1.000000e+00 -1.570000e+00 3.197350e+00] [ 1.000000e+00 5.800000e-01 4.600000e
3 min read
Generate a Pseudo Vandermonde matrix of the Hermite_e polynomial using NumPy in Python In this article, we will be generating a Pseudo Vandermonde matrix of the Hermite_e polynomial using NumPy in Python. Example 1: Generating a Pseudo Vandermonde matrix using hermite_e.hermevander() function We use the hermite_e.hermevander() function in the Numpy module of python to construct a Van
4 min read
Generate a Pseudo Vandermonde matrix of the Chebyshev and Legendre polynomial in Python In this article, we will generate a Pseudo Vandermonde matrix of the Chebyshev and Legendre polynomial x, y, and z floating array of points in Python. Example 1 Generating a Pseudo Vandermonde matrix of Legendre..legvander3d() function We use the legendre.legvander3d() method present in the Numpy mo
4 min read
Differentiate a Legendre series using NumPy in Python In this article, we will cover how to differentiate a Legendre series in Python using NumPy. legendre.legder method In python, the Legendre module provides many functions like legder to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Leg
3 min read