Open In App

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-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]]```

NumPy.legvander()

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]]