Compute the roots of a Legendre series with given complex roots in Python

To compute the roots of a Legendre series with complex coefficients, use the polynomial.legendre.legroots() method in Python. The method returns an array containing the roots of the series. If all roots are real, the output is real; otherwise it returns complex values.

Syntax

numpy.polynomial.legendre.legroots(c)

Parameters

The parameter c is a 1-D array of Legendre series coefficients ordered from low to high degree.

Example

Let's compute the roots of a Legendre series with complex coefficients ?

from numpy.polynomial import legendre as L

# Define complex unit
j = complex(0, 1)

# Compute the roots of a Legendre series with complex coefficients
coefficients = (-j, j)
roots = L.legroots(coefficients)

print("Coefficients:", coefficients)
print("Roots:", roots)
print("Data type:", roots.dtype)
print("Shape:", roots.shape)
Coefficients: (-1j, 1j)
Roots: [1.+0.j]
Data type: complex128
Shape: (1,)

How It Works

The Legendre series with coefficients (-j, j) represents the polynomial -j + j*P?(x), where P?(x) = x is the first Legendre polynomial. This simplifies to -j + j*x = j(x - 1), which has a root at x = 1.

Example with Real Coefficients

When all coefficients are real, the roots can also be real ?

from numpy.polynomial import legendre as L

# Legendre series with real coefficients
real_coefficients = [-1, 1]
real_roots = L.legroots(real_coefficients)

print("Real coefficients:", real_coefficients)
print("Roots:", real_roots)
print("Data type:", real_roots.dtype)
Real coefficients: [-1, 1]
Roots: [1.]
Data type: float64

Conclusion

Use numpy.polynomial.legendre.legroots() to find roots of Legendre series. The function automatically handles both real and complex coefficients, returning the appropriate data type based on the input.

Updated on: 2026-03-26T21:02:24+05:30

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements