Generate a Vandermonde Matrix of the Legendre Polynomial with Float Array of Points in Python using NumPy
Last Updated :
26 Apr, 2022
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-03]
[ 1.000000e+00 -3.570000e+00 1.861735e+01]
[ 1.000000e+00 1.440000e+00 2.610400e+00]
[ 1.000000e+00 2.750000e+00 1.084375e+01]]
To generate a pseudo Vandermonde matrix of the Legendre polynomial with a float array of points, the user has to call the NumPy.legvander() method in Python Numpy. This will return the pseudo-Vandermonde matrix the with 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. If x is scalar it is converted to a 1-D array.
- 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.57,0.58, -3.57, 1.44, 2.75])
# 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, 2)
print("\nResult:\n",gfg_data)
Output:
Array:
[-1.57 0.58 -3.57 1.44 2.75]
Dimensions:
1
Result:
[[ 1.000000e+00 -1.570000e+00 3.197350e+00]
[ 1.000000e+00 5.800000e-01 4.600000e-03]
[ 1.000000e+00 -3.570000e+00 1.861735e+01]
[ 1.000000e+00 1.440000e+00 2.610400e+00]
[ 1.000000e+00 2.750000e+00 1.084375e+01]]
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.57,0.58, -3.57, 1.44, 2.75,
-8.97,7.45,-0.56,-4.74,3.33])
# 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.57 0.58 -3.57 1.44 2.75 -8.97 7.45 -0.56 -4.74 3.33]
Dimensions:
1
Result:
[[ 1.00000000e+00 -1.57000000e+00 3.19735000e+00 -7.31973250e+00
1.77129525e+01 -4.42010179e+01]
[ 1.00000000e+00 5.80000000e-01 4.60000000e-03 -3.82220000e-01
-3.91403300e-01 -1.02849045e-01]
[ 1.00000000e+00 -3.57000000e+00 1.86173500e+01 -1.08393232e+02
6.63223708e+02 -4.17516096e+03]
[ 1.00000000e+00 1.44000000e+00 2.61040000e+00 5.30496000e+00
1.14106992e+01 2.53325643e+01]
[ 1.00000000e+00 2.75000000e+00 1.08437500e+01 4.78671875e+01
2.22228027e+02 1.06173499e+03]
[ 1.00000000e+00 -8.97000000e+00 1.20191350e+02 -1.79088068e+03
2.80222060e+04 -4.51013834e+05]
[ 1.00000000e+00 7.45000000e+00 8.27537500e+01 1.02255906e+03
1.32695485e+04 1.77126598e+05]
[ 1.00000000e+00 -5.60000000e-01 -2.96000000e-02 4.00960000e-01
-3.70740800e-01 5.29387264e-02]
[ 1.00000000e+00 -4.74000000e+00 3.32014000e+01 -2.59131060e+02
2.12459109e+03 -1.79197064e+04]
[ 1.00000000e+00 3.33000000e+00 1.61333500e+01 8.73200925e+01
4.96757827e+02 2.90771034e+03]]
Similar Reads
Generate a Vandermonde matrix of the Legendre series in Python using NumPy 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. ]]NumPy.legvander() To generate a pseudo Vandermonde m
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
Generate a Vandermonde matrix of the Chebyshev polynomial in Python In this article, we will be looking at the approach using various functionalities of the Numpy packages to generate a Vandermonde matrix of the Chebyshev polynomial in Python. NumPy.chebvander method To generate a Vandermonde matrix of the Chebyshev polynomial, the user needs to call the np.chebvand
3 min read
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
Evaluate a 3-D polynomial at points (x, y, z) with 4D array of coefficient using NumPy in Python In this article, we will look at how to evaluate a 3-dimensional polynomial at points (x, y, z) with a 4D array of coefficients using NumPy in Python. polynomial.polyval3d method We are using polynomial.polyval3d() function present in the NumPy module of python for the evaluation purpose of a 3-dim
2 min read