Javatpoint Logo
Javatpoint Logo

Generate a Legendre Series with Given Roots in Python

Introduction:

In this tutorial, we are discussing how to generate a Legendre series with given roots in Python. Legendre series is a module in Python. This module provides many kinds of functions. One of the functions of the Legendre module is legfromroots, which is used to perform the Legendre series for the calculus and arithmetic operations. Legfromroots is one of the main functions, which is provided by the Legendre module in Python. This function generates the Legendre series as a one-dimensional or 1D array. The legfromroots method also accepts the roots of a tuple. If the roots of a tuple are very complex, then the value of the array is also complex, and if all the value is real, then the array is also real.

Syntax of the Legendre series in Python

The syntax of the Legendre series in Python is given below -

[Here, n is represented as the number of roots in a polynomial]

Return Value of the Legendre Series in Python

The return value of the Legendre series in Python is a 1D array.

Steps of the Legendre Series in Python

The steps of the Legendre series in Python are given below -

Step 1:

Firstly, import the required library in Python. The example is given below -

Step 2:

For generating the Legendre series in Python, we use the legformroots method. The example is given below -

Step 3:

For getting the data types in Python, we use a code, which is given below -

Step 4:

For getting the shapes in Python, we use a code, which is given below -

Code:

Now we give the program code the generate a Legendre series with given roots in Python. It is the basic code for implementing the Legendre series in Python. We write the code in visual studio code, and the program is given in below -

Output:

Now we compile the above program and run it in any manner. After running the program, we found the output, and this output is given below -

The result is
[ 0. 
-3.4 0. 0.4]
The data type is
 float64
The shape is
 (4,)

Code 2:

Now we give the program code the generate a Legendre series with given roots in Python. In this code, we pass the real roots using the Legendre series in Python. We write the code in visual studio code, and the program is given in below -

Output:

Now we compile the above program and run it in any manner. After running the program, we found the output, and this output is given below -

The Legendre Series is: [ 4.33333333 -4. 0.66666667]

Code 3:

Now we give the program code the generate a Legendre series with given roots in Python. In this code, we pass the complex roots using the cmath package in the Legendre series in Python. We write the code in visual studio code, and the program is given in below -

Output:

Now we compile the above program and run it in any manner. After running the program, we found the output, and this output is given below -

The Legendre Series for complex root is: [ 2.33333333+4.j -3. -2.j 0.66666667+0.j]

Conclusion:

In this tutorial, we are discussing how to generate a Legendre series with given roots in Python. We also share the syntax and return value of the Legendre series in Python. Here, we also learn the steps of the Legendre series in Python. Legfromroots is one of the main functions, which is provided by the Legendre module in Python. We also share some program codes for better understanding. Here we know the use of the cmath package in Python.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA