
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Compute the roots of a polynomial in Python
To compute the roots of a polynomials, use the polynomial.polyroots() method in Python Numpy. The method returns an array of the roots of the polynomial. If all the roots are real, then out is also real, otherwise it is complex. The parameter, c is a 1-D array of polynomial coefficients.
The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the power series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton’s method.
Steps
At first, import the required libraries -
from numpy.polynomial import polynomial as P
To compute the roots of a polynomials, use the polynomial.polyroots() method in Python Numpy −
print("Result (roots of a polynomial)...\n",P.polyroots((-1,0,1)))
Get the datatype −
print("\nType...\n",P.polyroots((-1,0,1)).dtype)
Get the shape −
print("\nShape...\n",P.polyroots((-1,0,1)).shape)
Example
from numpy.polynomial import polynomial as P # To compute the roots of a polynomials, use the polynomial.polyroots() method in Python Numpy. # The method returns an array of the roots of the polynomial. If all the roots are real, then out is also real, otherwise it is complex. # The parameter, c is a 1-D array of polynomial coefficients. print("Result (roots of a polynomial)...\n",P.polyroots((-1,0,1))) # Get the datatype print("\nType...\n",P.polyroots((-1,0,1)).dtype) # Get the shape print("\nShape...\n",P.polyroots((-1,0,1)).shape)
Output
Result (roots of a polynomial)... [-1. 1.] Type... float64 Shape... (2,)
- Related Articles
- Compute the roots of a polynomial with given complex roots in Python
- Compute the roots of a Laguerre series in Python
- Compute the roots of a Hermite series in Python
- Compute the roots of a Chebyshev series in Python
- Compute the roots of a Legendre series in Python
- Compute the roots of a Hermite_e series in Python
- Compute the roots of a Laguerre series with given complex roots in Python
- Compute the roots of a Hermite series with given complex roots in Python
- Compute the roots of a Chebyshev series with given complex roots in Python
- Compute the roots of a Legendre series with given complex roots in Python
- Compute the roots of a Hermite_e series with given complex roots in Python
- Python program to compute a Polynomial Equation
- Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List
- Generate a monic polynomial with given roots in Python
- Generate a monic polynomial with given complex roots in Python
