Find the roots of the polynomials using NumPy
Last Updated : 05 Sep, 2020
In this article, let's discuss how to find the roots of a polynomial of a NumPy array. It can be found using various methods, let's see them in detail.
Method 1: Using np.roots()
This function returns the roots of a polynomial with coefficients given in p. The coefficients of the polynomial are to be put in an array in the respective order.
For example, if the polynomial is x2 +3x + 1, then the array will be [1, 3, 1]
Syntax : numpy.roots(p)
Parameters :
p : [array_like] Rank-1 array of polynomial coefficients.
Return : [ndarray] An array containing the roots of the polynomial.
Let's see some examples:
Example 1: Find the roots of polynomial x2 +2x + 1
Python3 # import numpy library import numpy as np # Enter the coefficients of the poly in the array coeff = [1, 2, 1] print(np.roots(coeff))
Output:
[-1. -1.]
Example 2: Find the roots of the polynomial x3 +3 x2 + 2x +1
Python3 # import numpy library import numpy as np # Enter the coefficients of the poly # in the array coeff = [1, 3, 2, 1] print(np.roots(coeff))
Output:
[-2.32471796+0.j -0.33764102+0.56227951j -0.33764102-0.56227951j]
Method 2: Using np.poly1D()
This function helps to define a polynomial function. It makes it easy to apply “natural operations” on polynomials. The coefficients of the polynomial are to be put in an array in the respective order.
For example, for the polynomial x2 +3x + 1, the array will be [1, 3, 1]
Approach:
- Apply function np.poly1D() on the array and store it in a variable.
- Find the roots by multiplying the variable by roots or r(in-built keyword) and print the result to get the roots of the given polynomial
Syntax: numpy.poly1d(arr, root, var):
Let's see some examples:
Example 1: Find the roots of polynomial x2 +2x + 1
Python3 # import numpy library import numpy as np # enter the coefficients of poly # in the array p = np.poly1d([1, 2, 1]) # multiplying by r(or roots) to # get the roots root_of_poly = p.r print(root_of_poly)
Output:
[-1. -1.]
Example 2: Find the roots of the polynomial x3 +3 x2 + 2x +1
Python3 # import numpy library import numpy as np # enter the coefficients of poly # in the array p = np.poly1d([1, 3, 2, 1]) # multiplying by r(or roots) to get # the roots root_of_poly = p.r print(root_of_poly)
Output:
[-2.32471796+0.j -0.33764102+0.56227951j -0.33764102-0.56227951j]
Similar Reads
How to add one polynomial to another using NumPy in Python? In this article, Let's see how to add one polynomial to another. Two polynomials are given as input and the result is the addition of two polynomials. The polynomial p(x) = C3 x2 + C2 x + C1  is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}. Let take two polynomials p(x) a
2 min read
How to subtract one polynomial to another using NumPy in Python? In this article, let's discuss how to subtract one polynomial to another. Two polynomials are given as input and the result is the subtraction of two polynomials. The polynomial p(x) = C3 x2 + C2 x + C1 Â is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}.Let take two polynom
2 min read
Compute the roots of a Chebyshev series using NumPy in Python This article will discuss how to compute the roots of a Chebyshev series in NumPy using Python. Chebyshev polynomials are important in approximation theory these are never formally generated. Only the coefficients are required for all calculations. If we want to compute the roots of a polynomial we
2 min read
Convert a Polynomial to Hermite Series Using NumPy In this article, we will discuss how to convert a Polynomial to Hermite series using NumPy. Example: Polynomial: [4 2 3 6] Coefficients of the converted equivalent Hermite series : [5.5 Â 5.5 Â 0.75 0.75] To convert a polynomial to Hermite series we need to use numpy methods hermite.poly2herm(). We ca
2 min read
How to divide a polynomial to another using NumPy in Python? In this article, we will make a NumPy program to divide one polynomial to another. Two polynomials are given as input and the result is the quotient and remainder of the division. The polynomial p(x) = C3 x2 + C2 x + C1 Â is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}.Let
2 min read