Evaluate a Legendre series at multidimensional array of points x in Python
Last Updated :
22 Apr, 2022
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 numpy
from numpy.polynomial import legendre
coefficients_data = numpy.array([[ 1 , 2 , 3 , 4 , 5 ],
[ 3 , 4 , 2 , 6 , 7 ]])
print (coefficients_data)
print (f "\nShape of an array: {coefficients_data.shape}" )
print (f "Dimension: {coefficients_data.ndim}" )
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 numpy
from numpy.polynomial import legendre
coefficients_data = numpy.array([ 6 , 4 , 7 ])
print (coefficients_data)
print (f "\nShape of an array: {coefficients_data.shape}" )
print (f "Dimension: {coefficients_data.ndim}" )
h = [[ 1 , 3 , 4 , 5 ],[ 3 , 2 , 6 , 7 ]]
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.]]
Share your thoughts in the comments
Please Login to comment...