In this article, we will cover how to integrate a Legendre series over axis 0 using NumPy in Python.
polynomial.legendre.legint method
The polynomial.legendre.legint() method from the NumPy library is used to Integrate a Legendre series over axis 0 in python.The resulting series is multiplied by scl and an integration constant, k, is added at each iteration. The scaling factor is intended for usage in linear variable changes. The argument c is an array of coefficients ranging in degree from low to high along each axis, for example, [4,3,1] represents the series 4. If axis=0 is x and axis=1 is y, [[2,1],[2,1]] symbolises 2*L 0(x)*L 0(y) + 2*L 1(x)*L 0(y) + 1*L 0(x)*L 1(y) + 1*L 1(x)*L 1(y) + 1*L 1(x)*L 1(y) + 1*L
Syntax: polynomial.legendre.legint(c, m=1, scl=1, axis=0)
Parameters:
- c: array like object.
- m: int, optional value.The integration order must be positive. Standard value is 1.
- axis: optional value,int. The axis on which the integral is computed. The default value is 0.
Returns: Legendre series coefficient array of the integral.
Example 1:
Here, we will create a NumPy array and use polynomial.legendre.legint() to Integrate a Legendre series over axis 0 in python. The shape of the array is found by the .shape attribute, the dimension of the array is found by .ndim attribute, and the data type of the array is .dtype attribute. even if we don’t specify by default axis is set to ‘0’.
Python3
# import packages import numpy as np from numpy.polynomial import legendre as L # array of coefficients array1 = np.array([ 1 , 2 , 3 , 4 , 5 ]) print (array1) # shape of the array is print ( "Shape of array1 is: " , array1.shape) # dimension of the array print ( "The dimension of array1 is: " , array1.ndim) # datatype of the array print ( "Datatype of Array1 is: " , array1.dtype) # adding two hermite series along axis =0 print ( 'Integrating the legendre series : ' ) print (L.legint(array1,axis = 0 )) |
Output:
[1 2 3 4 5]
Shape of array1 is: (5,)
The dimension of array1 is: 1
Datatype of Array1 is: int64
Integrating the legendre series :
[-0.16666667 0.4 0.0952381 0.04444444 0.57142857 0.55555556]
Example 2:
In this example, we specify axis =1, which represents that we’re integrating according to columns.
Python3
# import packages import numpy as np from numpy.polynomial import legendre as L # array of coefficients array1 = np.array([[ 1 , 2 , 3 , 4 , 5 ],[ 6 , 7 , 8 , 9 , 10 ]]) print (array1) # shape of the array is print ( "Shape of array1 is: " , array1.shape) # dimension of the array print ( "The dimension of array1 is: " , array1.ndim) # datatype of the array print ( "Datatype of Array1 is: " , array1.dtype) # adding two hermite series print ( 'Integrating the legendre series : ' ) print (L.legint(array1,axis = 1 )) |
Output:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Shape of array1 is: (2, 5)
The dimension of array1 is: 2
Datatype of Array1 is: int64
Integrating the legendre series :
[[-0.16666667 0.4 0.0952381 0.04444444 0.57142857 0.55555556]
[ 0.04166667 4.4 1.04761905 0.48888889 1.28571429 1.11111111]]