Tuesday, November 26, 2024
Google search engine
HomeLanguagesGenerate a Legendre series with given roots in Python

Generate a Legendre series with given roots in Python

In this article, we will see how to generate a Legendre series with given roots in Python.

Legendre class

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. legfromroots method accepts a tuple of roots and generates the Legendre series as a one-dimensional (1D) array.  If some of the roots are complex then the resulting array will be also a complex array even. Below is the syntax of legfromroots method.

Syntax: numpy.polynomial.legendre.legformroots((root1,root2,….rootn)

  • n: represents number of roots of a polynomial.

Return: 1-D array

Let’s look into a few sample codes to get a better understanding of the above-specified topics.

Example 1:

Here, we are passing real roots as parameters to legfromroots method. To generates the resultant array with real coefficient values.

Python3




# import necessary packages
import numpy.polynomial.legendre as l
  
# legfromroots() method generate legendre
# series for given roots
print("Legendre Series-", l.legfromroots((1, 2)))


Output:

Legendre Series- [ 2.33333333 -3.          0.66666667]

Example 2:

Here, we used cmath package along with the Legendre in order to define complex numbers. So the resultant array may also consist of complex coefficients.

Python3




# import necessary packages
import cmath
import numpy.polynomial.legendre as l
  
# legfromroots() method generate legendre 
# series for given roots
print("Legendre Series-", l.legfromroots((complex(1, 1), 2)))


Output:

Legendre Series- [ 2.33333333+2.j -3.        -1.j  0.66666667+0.j]

Example 3:

In this example, we are also printing its data type and its shape.

Python3




# import required packages
import numpy.polynomial.legendre as l
  
# legfromroots() method generate legendre
# series for given roots
print("Legendre Series-", l.legfromroots((1, 2, 3, 4)))
  
# Get the datatype
print("Datatype Type - ",l.legfromroots((1, 2, 3, 4)).dtype)
  
# Get the shape
print("Shape - ",l.legfromroots((1, 2, 3, 4)).shape)


Output

Legendre Series- [ 35.86666667 -56.          23.9047619   -4.           0.22857143]

Datatype Type –  float64

Shape –  (5,)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments