Integrate a Legendre series and set the integration constant in Python

To integrate a Legendre series in Python, use the polynomial.legendre.legint() method from NumPy. This method integrates Legendre series coefficients and allows you to set integration constants, making it useful for solving differential equations and polynomial manipulations.

Syntax

numpy.polynomial.legendre.legint(c, m=1, k=[], lbnd=0, scl=1, axis=0)

Parameters

The legint() method accepts the following parameters:

  • c: Array of Legendre series coefficients
  • m: Order of integration (default: 1)
  • k: Integration constant(s). Use a scalar for single integration or a list for multiple integrations (default: [])
  • lbnd: Lower bound of the integral (default: 0)
  • scl: Scaling factor applied after each integration (default: 1)
  • axis: Axis over which the integral is taken (default: 0)

Basic Integration Example

Let's start with a simple example of integrating a Legendre series ?

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

# Create an array of Legendre coefficients
coefficients = np.array([1, 2, 3])

print("Original coefficients:", coefficients)

# Integrate the Legendre series
result = L.legint(coefficients)
print("Integrated series:", result)
Original coefficients: [1 2 3]
Integrated series: [0.         0.33333333 0.4        0.66666667 0.6       ]

Setting Integration Constants

You can set integration constants using the k parameter ?

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

coefficients = np.array([1, 2, 3])

# Set integration constant to 5
result_with_constant = L.legint(coefficients, k=5)
print("With integration constant k=5:")
print(result_with_constant)

# Compare with default (k=0)
result_default = L.legint(coefficients)
print("\nWith default k=0:")
print(result_default)
With integration constant k=5:
[5.         0.33333333 0.4        0.66666667 0.6       ]

With default k=0:
[0.         0.33333333 0.4        0.66666667 0.6       ]

Multiple Integration Orders

You can perform multiple integrations by setting the m parameter ?

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

coefficients = np.array([1, 2, 3])

# Single integration (m=1)
single_int = L.legint(coefficients, m=1)
print("Single integration (m=1):")
print(single_int)

# Double integration (m=2) 
double_int = L.legint(coefficients, m=2, k=[1, 2])
print("\nDouble integration (m=2) with constants [1, 2]:")
print(double_int)
Single integration (m=1):
[0.         0.33333333 0.4        0.66666667 0.6       ]

Double integration (m=2) with constants [1, 2]:
[1.         2.         0.06666667 0.13333333 0.14285714 0.17142857]

Practical Example

Here's a complete example showing array properties and integration ?

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

# Create Legendre series coefficients
coefficients = np.array([1, 2, 3])

print("Legendre Series Analysis")
print("=" * 25)
print("Coefficients:", coefficients)
print("Dimensions:", coefficients.ndim)
print("Data type:", coefficients.dtype)
print("Shape:", coefficients.shape)

# Integrate with custom constant
integrated = L.legint(coefficients, k=3)
print("\nIntegrated series (k=3):")
print(integrated)

# Show the difference integration constants make
print("\nComparison of integration constants:")
for k_val in [0, 1, 5]:
    result = L.legint(coefficients, k=k_val)
    print(f"k={k_val}: {result}")
Legendre Series Analysis
=========================
Coefficients: [1 2 3]
Dimensions: 1
Data type: int64
Shape: (3,)

Integrated series (k=3):
[3.         0.33333333 0.4        0.66666667 0.6       ]

Comparison of integration constants:
k=0: [0.         0.33333333 0.4        0.66666667 0.6       ]
k=1: [1.         0.33333333 0.4        0.66666667 0.6       ]
k=5: [5.         0.33333333 0.4        0.66666667 0.6       ]

Key Points

  • Integration increases the degree of the polynomial by 1
  • The integration constant k becomes the first coefficient of the result
  • Multiple integrations require a list of constants, one for each integration
  • The scl parameter allows scaling after each integration step

Conclusion

The legint() function provides flexible integration of Legendre series with customizable constants. Use the k parameter to set integration constants and m for multiple integration orders.

Updated on: 2026-03-26T20:58:52+05:30

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements