Open In App

Find the roots of the polynomials using NumPy

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




# 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




# 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:

Syntax: numpy.poly1d(arr, root, var):

Let’s see some examples:

Example 1: Find the roots of polynomial x2 +2x + 1




# 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




# 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]


Article Tags :