Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Integrate a Legendre series over specific axis in Python
To integrate a Legendre series, use the polynomial.legendre.legint() method in Python. The method returns the Legendre series coefficients c integrated m times from lbnd along the specified axis. At each iteration, the resulting series is multiplied by scl and an integration constant k is added.
Syntax
numpy.polynomial.legendre.legint(c, m=1, k=[], lbnd=0, scl=1, axis=0)
Parameters
The method accepts the following parameters ?
- c − Array of Legendre series coefficients. If multidimensional, different axes correspond to different variables
- m − Order of integration, must be positive (Default: 1)
- k − Integration constant(s). If empty list (default), all constants are zero
- lbnd − Lower bound of the integral (Default: 0)
- scl − Scalar multiplier applied after each integration (Default: 1)
- axis − Axis over which the integral is taken (Default: 0)
Example
Let's create a multidimensional array and integrate along different axes ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a multidimensional array of coefficients
c = np.arange(4).reshape(2,2)
# Display the array
print("Our Array...\n", c)
# Check the Dimensions
print("\nDimensions of our Array...\n", c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n", c.dtype)
# Get the Shape
print("\nShape of our Array object...\n", c.shape)
# Integrate along axis 1
print("\nIntegration along axis=1...\n", L.legint(c, axis=1))
# Integrate along axis 0
print("\nIntegration along axis=0...\n", L.legint(c, axis=0))
Our Array... [[0 1] [2 3]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Integration along axis=1... [[0.16666667 0. 0.33333333] [0.5 2. 1. ]] Integration along axis=0... [[0. 0.5 ] [0.66666667 0. ] [0.66666667 1. ]]
Integration with Custom Parameters
You can also specify integration order, constants, and bounds ?
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([1, 2, 3])
# Single integration (default)
result1 = L.legint(c)
print("Single integration:\n", result1)
# Double integration with integration constant
result2 = L.legint(c, m=2, k=[1, 2])
print("\nDouble integration with constants:\n", result2)
# Integration with scaling factor
result3 = L.legint(c, scl=2, lbnd=1)
print("\nIntegration with scaling and bound:\n", result3)
Single integration: [0.33333333 0. 0.66666667 0. 1. ] Double integration with constants: [ 1.06666667 2. 0.11111111 0. 0.22222222 0. 0.33333333] Integration with scaling and bound: [-0.33333333 0. 1.33333333 0. 2. ]
Conclusion
The legint() method provides flexible Legendre series integration with control over axis, order, constants, and scaling. Use the axis parameter to integrate along specific dimensions of multidimensional coefficient arrays.
