![]() |
VOOZH | about |
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
Output:
[-1. -1.]
Example 2: Find the roots of the polynomial x3 +3 x2 + 2x +1
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
Output:
[-1. -1.]
Example 2: Find the roots of the polynomial x3 +3 x2 + 2x +1
Output:
[-2.32471796+0.j -0.33764102+0.56227951j -0.33764102-0.56227951j]