Find Roots of the Polynomials Using Numpy in Python

We often solve polynomial equations in mathematics to find the roots of the equations. Have you ever wondered how to solve those mathematical equations using programming? Well, without using python, it would be a bit stressful to solve those equations. Let me tell you about an amazing library called numpy. In today’s tutorial, we will explore numPy in python and have some detailed analysis of numpy and its uses.

Introduction of Numpy in python

Numpy stands for Numerical Python. It is an open-source scientific computing library for the Python programming language. NumPy is a library of numerical routines that helps in solving scientific problems. Numpy array is a very famous package in the numpy library. It also has functions in the domain of linear algebra, Fourier transformations, and matrices.

Why to use numpy in python?

In our previous tutorial, we have learned about lists. Lists are similar to arrays in python, but it is a slower process. On the other hand, NumPy arrays are stored at one continuous location in memory, so it is straightforward to access and manipulate them very efficiently.

What are Numpy roots in Python?

Numpy root helps in finding the roots of a polynomial equation having coefficients in python. It can be found using a couple of methods. Let’s discuss them in detail. A polynomial equation is written as: –

 p[0] * xn + p[1] * x(n-1) + … + p[n-1]*x + p[n]

Method 1: Using np.roots() function in python

In this method, we will look at how to use the function of the numpy root and print the given function help of the print function in python. numpy.roots() function returns the roots of a polynomial with coefficients given in p. The coefficients of the polynomial are to be put in a numpy array in a sequence. 

Syntax

Syntax: numpy.roots(p)

Parameter

It takes the coefficients of an given polynomial.

Return Value

The function will return the roots of the polynomial.

Let’s do some code to understand.

Example 1:

Let us consider an equation: x2 + 5*x + 6

The coefficients are 1, 5 and 6.

import numpy as np
p = [1, 5, 6]
roots = np.roots(p)
print(roots)
OUTPUT: - [3.  2.] 

Explanation of the code

  1. To use the numpy library in python, we need to import it.
  2. np is the alias name of numpy.
  3. p is the list having the coefficients.
  4. To find the roots of the equation, we used np.roots passing the coefficients as the parameter.
  5. Printing the roots.

Example 2:

Now let us consider the following polynomial for a cubic equation:  x3  – 6 * x2 + 11 * x – 6 

The coefficients are 1, -6 , 11 and -6.

import numpy as np 
coeff = [1, -6, 11, -6] 
print(np.roots(coeff))
OUTPUT:- [3.  2.  1.]

Explanation of the code

  1. To use the numpy library in python, we need to import it.
  2. np is the alias name of numpy.
  3. coeff has the list of the coefficients.
  4. To find the roots of the equation, we used np.roots passing the coefficients as the parameter and printing it.

Also, Read | Numpy Square Root | Usecase Evaluation of Math Toolkit

Method 2: Using poly1D() function in python

poly1D helps us to define a polynomial function in python. We use this function as it makes it easy to apply the operations on polynomials. The coefficients of the polynomial are to be put in a numpy array in a sequence. 

Syntax

numpy.poly1d(arr, root, var)

Parameter

arr:- [array_like]  The polynomial coefficients are in the decreasing order of powers. So if the second parameter, i.e., the root, is assigned to the True value, then array values will be the roots of the polynomial equation.

root: – [bool, optional] The default value of root is False. True means polynomial roots.

var: – variables like x, y, z that we need in polynomial. The default variable is x.

Return value

poly1D returns the polynomial equation along with the operation applied on it.

for example, let the polynomial be x2 + 5 * x + 6 , then the array will be [1, 5 , 6] respectively.

import numpy as np 
p = np.poly1d([1, 5, 6]) 
root = p.r
print(p)
print(root)
OUTPUT:-  x2 + 5x +6
                    [-3.   -2.]

Explanation of the code

  1. To use the numpy library, we need to import it.
  2. np is the alias name of numpy.
  3. Entered the coefficients of a polynomial into an array.
  4. Multiplying by r to get the roots.
  5. Displaying the polynomial equation and the roots of the equation.

Also, Read | How to Calculate Square Root in Python

How numpy solves the equation of a Companion Matrix?

Python even helps us to solve a companion matrix as well with the help of numpy. The method which we use to solve this is np.legcompanion(). The np.legcompanion() will return the companion matrix .

Syntax:

np.legcompanion(c)

Parameter

c :[array_like] 1-D arrays of legendre series coefficients which is ordered from low to high.

Return Value

It returns a ndarray Companion matrix of dimensions (degree, degree).

import numpy as np 
import numpy.polynomial.legendre as npl
s = (1, 2, 3, 4, 5) 
res = npl.legcompanion(s) 
print(res) 

OUTPUT:-

[ [ 0.          0.57735027  0.         -0.30237158]
  [ 0.57735027  0.          0.51639778 -0.34914862]
  [ 0.          0.51639778  0.          0.10141851]
  [ 0.          0.          0.50709255 -0.4571428] ] 

Explanation of the code

  1. Importing numpy having alias name as np.  
  2. Importing numpy.polynomial.legendre module as npl.
  3. Storing Legendre series coefficients in the variable s.
  4. Using np.legcompanion() method.
  5. Printing the resultant companion matrix.

Conclusion

Numpy is an essential library python has. It is used in many applications for data scientists and machine learning engineers. This article has discussed a few of its features, such as root function and polynomial.legendre function.

If you have any doubts please drop a comment in the comment section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments