Generate a Legendre series with given complex roots in Python


To generate a Legendre series, use the polynomial.legendre.legfromroots() method in Python. The method returns a 1-D array of coefficients. If all roots are real then out is a real array, if some of the roots are complex, then out is complex even if all the coefficients in the result are real. The parameter roots are the sequence containing the roots.

Steps

At first, import the required library −

from numpy.polynomial import legendre as L

Generate a Legendre series using the polynomial.legendre.legfromroots() method in Python −

j = complex(0,1)
print("Result...\n",L.legfromroots((-j, j)))

Get the datatype −

print("\nType...\n",L.legfromroots((-j, j)).dtype)

Get the shape −

print("\nShape...\n",L.legfromroots((-j, j)).shape)

Example

from numpy.polynomial import legendre as L

# To generate a Legendre series, use the polynomial.legendre.legfromroots() method in Python
j = complex(0,1)
print("Result...\n",L.legfromroots((-j, j)))

# Get the datatype
print("\nType...\n",L.legfromroots((-j, j)).dtype)

# Get the shape
print("\nShape...\n",L.legfromroots((-j, j)).shape)

Output

Result...
   [1.33333333+0.j 0. +0.j 0.66666667+0.j]

Type...
complex128

Shape...
(3,)

Updated on: 09-Mar-2022

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements