Open In App

Generate a Legendre series with given complex roots in Python

Last Updated : 22 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to generate the Legendre series with given complex roots in Python using NumPy.

Example

Input: (4+5j)
Output: [9.33333333-40.j 0.   +0.j 0.66666667 +0.j]
Explanation: An array of legendre series.

legendre.legfromroots method

In python, the Legendre module provides many functions like legfromroots to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Legendre class that returns an array of coefficients. This method is used to generate the Legendre series which is available in the NumPy module in python. Below is the syntax of the legfromroots method.

  • If all roots are real then the output will be a real array,
  • If any of the roots are complex, the output is a complex array.

Syntax: legendre.legfromroots((-my_value, my_value))

Parameter:

  • my_value: is the complex number.

Return: 1-D array of coefficients.

Example 1:

In this example, we will create a complex number using a complex function, which will return an array of legendary roots. 

Python3




# import legendre
from numpy.polynomial import legendre 
  
# create a complex variable
my_value = complex(4,5)
  
# display value
print("Complex value: ", my_value)
  
# generate legendary roots
print("legendary roots: ", legendre.legfromroots((-my_value, my_value)))


Output:

Complex value:  (4+5j)

legendary roots:  [9.33333333-40.j 0.         +0.j 0.66666667 +0.j]

Example 2:

In this example, we will create a complex variable -> 45+4j and generate legendary roots. We can also get the shape and dimensions of the resultant array using dim and shape functions.

Python3




# import legendre
from numpy.polynomial import legendre 
  
# create a complex variable
my_value = complex(45,4)
  
# display value
print("Complex value: ", my_value)
  
# generate legendary roots
print("legendary roots: ", legendre.legfromroots(
  (-my_value, my_value)))
  
# get the dimensions
print("Dimensions: ", legendre.legfromroots(
  (-my_value, my_value)).ndim)
  
# get the shape
print("shape: ",legendre.legfromroots(
  (-my_value, my_value)).shape)


Output:

Complex value:  (45+4j)

legendary roots:  [-2.00866667e+03-360.j  0.00000000e+00  +0.j  6.66666667e-01  +0.j]

Dimensions:  1

shape:  (3,)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads