Open In App

Generate a Vandermonde matrix of the Legendre series in Python using NumPy

Improve
Improve
Like Article
Like
Save
Share
Report

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



Last Updated : 26 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads