Add one Legendre series to another in Python

To add one Legendre series to another, use the polynomial.legendre.legadd() method in Python NumPy. The method returns an array representing the Legendre series of their sum.

The legadd() function adds 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.legadd(c1, c2)

Parameters:

  • c1, c2 ? 1-D arrays of Legendre series coefficients ordered from low to high

Returns: Array representing the Legendre series of their sum

Example

Let's create two Legendre series and add them together ?

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:", c1.dtype)
print("Array2 datatype:", 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)

# Add the Legendre series
result = L.legadd(c1, c2)
print("\nResult (sum):")
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 (sum):
[6. 6. 6.]

How It Works

The addition is performed element-wise on the coefficient arrays. Each coefficient position represents a term in the Legendre polynomial series:

  • First series: 2*P_0 + 3*P_1 + 4*P_2
  • Second series: 4*P_0 + 3*P_1 + 2*P_2
  • Sum: (2+4)*P_0 + (3+3)*P_1 + (4+2)*P_2 = 6*P_0 + 6*P_1 + 6*P_2

Different Length Arrays

The function can handle arrays of different lengths by extending the shorter array with zeros ?

import numpy as np
from numpy.polynomial import legendre as L

# Create arrays of different lengths
c1 = np.array([1, 2])      # 1*P_0 + 2*P_1
c2 = np.array([3, 4, 5])   # 3*P_0 + 4*P_1 + 5*P_2

result = L.legadd(c1, c2)
print("Result:", result)
Result: [4. 6. 5.]

Conclusion

The legadd() function provides a convenient way to add Legendre polynomial series by performing element-wise addition of coefficients. It automatically handles arrays of different lengths and maintains the polynomial structure.

Updated on: 2026-03-26T20:38:43+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements