In this article, we will see how can we generate a monic polynomial with given complex roots in python, for this purpose, we will use NumPy library of python, NumPy is the fundamental package for scientific computing with Python. NumPy is a general-purpose array processing package that provides tools for handling n-dimensional arrays.
What is Monic Polynomial?
A monic polynomial is a univariate ( or single-variable) polynomial, whose highest degree coefficient is equal to 1. i.e
If a polynomial highest degree coefficient is 1. i.e called a monic polynomial.
For example:
Generate a Monic Polynomial with given Complex roots
polyfromroots(roots) function of NumPy module is used to generate a monic polynomial from given roots which returns the coefficients of the polynomial, Function can be defined as below:
Syntax:
numpy.polynomial.polynomial.polyfromroots(roots) Parameters: roots: Sequence containing the roots. Returns: 1-D array of the polynomial's coefficients. If all the roots are real, then output is also real, otherwise it is complex.
Example 1: For real roots
Let the roots of the equation be -1, 0, 1 then the equation will be:
Python3
from numpy.polynomial import polynomial # x(x - 1)(x + 1) = x^3 - x poly = polynomial.polyfromroots(( - 1 , 0 , 1 )) print (poly) |
Output:
[ 0., -1., 0., 1.]
Explanation:
The roots passed in the polyfromroots() functions are -1, 0, and +1 which are roots of the polynomial x3 – x = 0 which can be written as 0.x0 + -1.x1 + 0.x2 + 1.x3 = 0 and if we focus on the coefficients of the equation they are 0. , -1. , 0. and 1. which is also the output from the above code.
Example 2: For complex roots (imaginary roots)
Let the complex roots of the equation be then the equation will be :
Python3
from numpy.polynomial import polynomial j = complex ( 0 , 1 ) print ( 'Roots :' ,j, - j) poly = polynomial.polyfromroots(( - j,j)) print ( 'polynomial:' ,poly) |
Output:
Roots : 1j (-0-1j) polynomial: [1.+0.j 0.+0.j 1.+0.j]
Explanation:
The roots passed in the polyfromroots() functions are of complex type, In mathematics, we call them imaginary roots since they are in form of a + bj where j = √-1 so, the roots passed in the function are complex(0,1) = 0+1j and -complex(0,1) = -(0+1j) = 0 – 1j, and the polyfromroots() function returns the output [1+0j, 0+0j, 1+0j] which represents the equation (1+0j)x0 + (0+0j)x1 + (1+0j)x2 = 0 which can be simplified as 1 + x2 = 0 and if we solve this equation mathematically then we get x = ±√-1 which is equal to the roots passed in the polyfromroots() function.
Example 3: For complex roots (imaginary roots)
Let the complex roots of the equation be then the equation will be :
Python3
from numpy.polynomial import polynomial j = complex ( 2 , 3 ) print ( 'Roots :' ,j, - j) poly = polynomial.polyfromroots((j, - j)) print ( 'polynomial:' ,poly) |
Output:
Roots : (2+3j) (-2-3j) polynomial: [5.-12.j 0. +0.j 1. +0.j]
Explanation:
The roots passed in the polyfromroots() functions are of complex type, In mathematics, we call them imaginary roots since they are in form of a + bj where j = √-1 so, the roots passed in the function are complex(2,3) = 2+3j and -complex(0,1) = -(2+3j) = -2 – 3j, and the polyfromroots() function returns the output [5-12j, 0+0j, 1+0j] which represents the equation which can be simplified as and if we solve this equation mathematically then we get x = 2+3j and -2-3j which is equal to the roots passed in the polyfromroots() function.