# How to find roots of polynomial in Python

In this article, we are going to write the code to** find the roots of the Polynomial in Python**. Before that, we will learn some basic theories about Polynomial which will help us to understand the codes.

**Polynomial: **Polynomial comes from two words poly means “many” and nomial means “terms”.

Polynomials are the combinations of variable (x,y,z,etc.), constant(1, 2,-29,1/2,etc.), exponent i.e power of variable (such as 5 in x^5 etc. but only 0,1,2,…) which are combined by addition, subtraction, multiplication, division, except not division by a variable (like 2/x).

Let us consider an example, x+15 in this ‘x’ is called **variable**.

Power of ‘x’, i.e. 2 is called the **exponent/order/degree.**

Multiple of ‘x’, i.e. 1,2 is called the **coefficient**.

The term ‘2’ is called **constant**.

Monomials items i.e x^2, 2x, 15 are called **terms**.

**Roots Of The Polynomials: **Roots of the polynomials are defined as the values of the variable which evaluates the polynomials to the zero.

**Degree Of The Polynomial:** The degree of the polynomial is defined as the highest power of the variable of a polynomial.

To find the roots of a polynomial in math, we use the formula. Let’s learn with an example,

Let consider the polynomial, **ax^2+bx+c. ** The roots of this equation is,

## Finding The Roots Of The Polynomial in Python

Program to find the roots of the polynomial, x^2+2x+3. We can find the roots, co-efficient, highest order of the polynomial, changing the variable of the polynomial using numpy module in python.

**Steps: step 1: **line 1, Importing the numpy module as np.

*line 3, Storing the polynomial co-efficient in variable ‘p’.*

**step 2:***line 5, Printing the polynomial with the highest order.*

**step 3:**import numpy as np p = np.poly1d([1, 2, 3]) print(p)

**Output: **

2 1 x + 2 x + 3

* step 4: *line 7, Finding the roots of the polynomial and storing in the variable ‘rootsp’.

*: line 9, Printing the roots of the polynomial.*

**step 5**import numpy as np p = np.poly1d([1, 2, 3]) print(p) rootsp = p.r print("\nRoots of Polynomials is :", rootsp)

Output:

2 1 x + 2 x + 3 Roots of Polynomials is : [-1.+1.41421356j -1.-1.41421356j]

* step 6: *line 11, Evaluating the polynomial at x=2.

import numpy as np p = np.poly1d([1, 2, 3]) print(p) rootsp = p.r print("\nRoots of Polynomials is :", rootsp) print("\nEvaluating polynomial at x=2:)", p(2))

**Output:**

2 1 x + 2 x + 3 Roots of Polynomials is : [-1.+1.41421356j -1.-1.41421356j] Evaluating polynomial at x=2: 4.25

* step 7: *line 13, Finding the co-efficient of polynomial.

import numpy as np p = np.poly1d([1, 2, 3]) print(p) rootsp = p.r print("\nRoots of Polynomials is :", rootsp) print("\nEvaluating polynomial at x=2:)", p(2)) print("\nCo-efficient of polynomial:", p.c)

**Output:**

2 1 x + 2 x + 3 Roots of Polynomials is : [-1.+1.41421356j -1.-1.41421356j] Evaluating polynomial at x=2: 4.25 Co-efficient of polynomial: [1 2 3]

* step 8: *We can also change the variable of the polynomial, which shown inline 3.

import numpy as np p = np.poly1d([1, 2, 3], variable= 'z') print(p) rootsp = p.r print("\nRoots of Polynomials is :", rootsp) print("\nEvaluating polynomial at x=2:",p(0.5)) print("\nCo-efficient of polynomial:", p.c)

**Output:**

2 1 z + 2 z + 3 Roots of Polynomials is : [-1.+1.41421356j -1.-1.41421356j] Evaluating polynomial at x=2: 4.25 Co-efficient of polynomial: [1 2 3]

## Basic Arthematic Operation on Polynomial

#importing the module import numpy as np p = np.poly1d([5,4,5,6]) print(p) # Multiplication of the two polynomials print("\nmultiplication of the polynomials is :\n", p*p) # Squaring the polynomials print("\nSquaring the polynomial:\n", p**2) #Squaring the individual co-efficient Csquare = np.square(p) print("\nSquare of the co-efficient is:\n", Csquare)

**Output:**

3 2 5 x + 4 x + 5 x + 6 multiplication of the polynomials is : 6 5 4 3 2 25 x + 40 x + 66 x + 100 x + 73 x + 60 x + 36 Squaring the polynomial: 6 5 4 3 2 25 x + 40 x + 66 x + 100 x + 73 x + 60 x + 36 Square of the co-efficient is : [25 16 25 36]

Also read:

## Leave a Reply