In this article, we will cover how to evaluate the Legendre series at points x points with a multi-dimensional coefficient array in Python using NumPy.
Example
Input: [[1 2 3 4 5]
[3 4 2 6 7]] at points [4,1]
Output: [[13. 4.]
[18. 6.]
[11. 5.]
[28. 10.]
[33. 12.]]
Explanation: A multi-dimensional coefficient array.
legendre.legval method
In python, the Legendre module provides many functions like legval to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Legendre class. This method is used to generate the Legendre series and this method is available in the Legendre module in python, it returns a multi-dimensional coefficient array, Below is the syntax of the legval method.
Syntax: legendre.legval(x, c, tensor)
Parameter:
- x: a list or tuple
- c: an array of coefficients ordered
- tensor: boolean, optional
Return: new array
Example 1:
In this example, we are creating a coefficient of a multi-dimensional NumPy array with 5 elements and displaying the shape and dimensions of the array. After that, we are evaluating the Legendre series at points [4,1].
Python3
# import the numpy module import numpy # import legendre from numpy.polynomial import legendre # create array of coefficients with 5 elements each coefficients_data = numpy.array([[ 1 , 2 , 3 , 4 , 5 ], [ 3 , 4 , 2 , 6 , 7 ]]) # Display the coefficients print (coefficients_data) # get the shape print (f "\nShape of an array: {coefficients_data.shape}" ) # get the dimensions print (f "Dimension: {coefficients_data.ndim}" ) # Evaluate a legendre series at points - [4,1] print ( "\nmultidimensional legendre series" , legendre.legval([ 4 , 1 ], coefficients_data)) |
Output:
[[1 2 3 4 5] [3 4 2 6 7]] Shape of an array: (2, 5) Dimension: 2 multidimensional legendre series [[13. 4.] [18. 6.] [11. 5.] [28. 10.] [33. 12.]]
Example 2:
In this example, we are creating a coefficient of a multi-dimensional NumPy array with 3 elements and displaying the shape and dimensions of the array. After that, we are evaluating the Legendre series at points [[1,3,4,5],[3,2,6,7]].
Python3
# import the numpy module import numpy # import legendre from numpy.polynomial import legendre # create array of coefficients with 2 elements each coefficients_data = numpy.array([ 6 , 4 , 7 ]) # Display the coefficients print (coefficients_data) # get the shape print (f "\nShape of an array: {coefficients_data.shape}" ) # get the dimensions print (f "Dimension: {coefficients_data.ndim}" ) h = [[ 1 , 3 , 4 , 5 ],[ 3 , 2 , 6 , 7 ]] # Evaluate a legendre series at points - [6,4] print ( "\nmultidimensional legendre series" , legendre.legval(coefficients_data,h)) |
Output:
[6 4 7] Shape of an array: (3,) Dimension: 1 multidimensional legendre series [[19. 13. 22.] [15. 11. 17.] [40. 28. 46.] [47. 33. 54.]]