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
Subtract one Legendre series from another in Python
To subtract one Legendre series from another, use the polynomial.legendre.legsub() method in Python NumPy. The method returns an array representing the Legendre series of their difference.
The function computes the difference of two Legendre series c1 - c2. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series P_0 + 2*P_1 + 3*P_2. The parameters c1 and c2 are 1-D arrays of Legendre series coefficients ordered from low to high.
Syntax
numpy.polynomial.legendre.legsub(c1, c2)
Parameters
- c1, c2 ? 1-D arrays of Legendre series coefficients ordered from low to high
Returns
Returns a 1-D array representing the coefficients of the resulting Legendre series after subtraction.
Example
Let's demonstrate how to subtract Legendre series using the legsub() method ?
import numpy as np
from numpy.polynomial import legendre as L
# Create 1-D arrays of Legendre series coefficients
c1 = np.array([2, 3, 4])
c2 = np.array([4, 3, 2])
# Display the arrays of coefficients
print("Array1:")
print(c1)
print("\nArray2:")
print(c2)
# Display the datatype
print("\nArray1 datatype:")
print(c1.dtype)
print("\nArray2 datatype:")
print(c2.dtype)
# Check the dimensions and shape
print("\nDimensions of Array1:", c1.ndim)
print("Dimensions of Array2:", c2.ndim)
print("\nShape of Array1:", c1.shape)
print("Shape of Array2:", c2.shape)
# Subtract one Legendre series from another
result = L.legsub(c1, c2)
print("\nResult (c1 - c2):")
print(result)
Array1: [2 3 4] Array2: [4 3 2] Array1 datatype: int64 Array2 datatype: int64 Dimensions of Array1: 1 Dimensions of Array2: 1 Shape of Array1: (3,) Shape of Array2: (3,) Result (c1 - c2): [-2. 0. 2.]
Different Array Sizes
The legsub() method can handle arrays of different sizes by padding the shorter array with zeros ?
import numpy as np
from numpy.polynomial import legendre as L
# Create arrays of different sizes
c1 = np.array([1, 2, 3, 4])
c2 = np.array([2, 1])
print("Array1:", c1)
print("Array2:", c2)
# Subtract series of different lengths
result = L.legsub(c1, c2)
print("\nResult (c1 - c2):")
print(result)
Array1: [1 2 3 4] Array2: [2 1] Result (c1 - c2): [-1. 1. 3. 4.]
Conclusion
The numpy.polynomial.legendre.legsub() method provides an efficient way to subtract Legendre series by performing element-wise subtraction of coefficients. It automatically handles arrays of different sizes by padding with zeros.
