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
Selected Reading
Differentiate a Legendre series and set the derivatives in Python
To differentiate a Legendre series in Python, use the numpy.polynomial.legendre.legder() method. This function returns the Legendre series coefficients differentiated m times along the specified axis.
Syntax
numpy.polynomial.legendre.legder(c, m=1, 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: Number of derivatives taken (must be non-negative, default: 1)
- scl: Scalar multiplier for each differentiation (default: 1)
- axis: Axis over which the derivative is taken (default: 0)
Example
Let's differentiate a Legendre series with different derivative orders:
import numpy as np
from numpy.polynomial import legendre as L
# Create an array of coefficients
c = np.array([1, 2, 3, 4])
# Display the original coefficients
print("Original coefficients:", c)
# First derivative
print("First derivative:", L.legder(c, 1))
# Second derivative
print("Second derivative:", L.legder(c, 2))
# Third derivative
print("Third derivative:", L.legder(c, 3))
Original coefficients: [1 2 3 4] First derivative: [ 6. 20. 42.] Second derivative: [ 60. 252.] Third derivative: [840.]
Using Scale Parameter
The scl parameter multiplies each differentiation by a scalar value:
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([1, 2, 3, 4])
# Differentiate with scale factor of 2
result_scaled = L.legder(c, m=2, scl=2)
print("Second derivative with scale=2:", result_scaled)
# Compare with normal second derivative
result_normal = L.legder(c, m=2)
print("Second derivative without scaling:", result_normal)
print("Scale factor applied:", result_scaled / result_normal)
Second derivative with scale=2: [ 240. 1008.] Second derivative without scaling: [ 60. 252.] Scale factor applied: [4. 4.]
Multidimensional Arrays
For multidimensional coefficient arrays, specify the axis for differentiation:
import numpy as np
from numpy.polynomial import legendre as L
# Create a 2D coefficient array
c_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D coefficients:\n", c_2d)
# Differentiate along axis 0
result_axis0 = L.legder(c_2d, axis=0)
print("Derivative along axis 0:\n", result_axis0)
# Differentiate along axis 1
result_axis1 = L.legder(c_2d, axis=1)
print("Derivative along axis 1:\n", result_axis1)
2D coefficients: [[1 2 3] [4 5 6]] Derivative along axis 0: [[ 9. 15. 21.]] Derivative along axis 1: [[ 6. 20.] [15. 50.]]
Conclusion
Use numpy.polynomial.legendre.legder() to differentiate Legendre series efficiently. The method supports multiple derivatives, scaling factors, and multidimensional arrays for complex polynomial operations.
Advertisements
