Saturday, November 16, 2024
Google search engine
HomeLanguagesGenerate a Monic Polynomial with given Complex roots in Python

Generate a Monic Polynomial with given Complex roots in Python

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

1x^n + c_{n}x^{n-1} + ...+c_{1}x +c_{0}x^{0} = 0

If a polynomial highest degree coefficient is 1. i.e called a monic polynomial.

For example:

 x + 7 = 0 \\ x^5-4x^3+2x+5 =0  

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: 

0=(x-1)(x-0)(x+1) \\ 0=x(x-1)(x+1) \\0= x(x^2-1)\\ 0= x^3-x=0 \\0= 0\cdot x^0-1\cdot x^1+0\cdot x^2 + 1\cdot x^3

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 0+1\cdot i, 0-1\cdot i   then the equation will be :

0=(x-(0+i))(x-(0-i)) \\ 0=x^2 -(0+i)x - (0-i)x + (0+i)(0-i) \\ 0=x^2 +(-i+i)x + (0-i^2)\\0= x^2 +0x +1 \\ 0= (1+0i)\cdot x^0 + (0+0i)\cdot x^1 + (1+0i)\cdot x^2

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 2+3\cdot i, -2-3\cdot i   then the equation will be :

0=(x-(2+3i))(x-(-2-3i))) \\ 0=(x-(2+3i))(x+(2+3i))) \\ 0=x^2 -(2+3i)^2 \\ 0= x^2 -(4+2\cdot2\cdot 3i +(3i)^2) \\ 0 = x^2  -(4 + 12i  + 9(i^2)) \\ 0 = x^2  -(12i +4 + 9(-1)) \\ 0 = x^2  -(12i +4 - 9) \\ 0 = x^2 + 5 -12i \\ 0= (5-12i)\cdot x^0 + (0+0i)\cdot x^1 + (1+0i)\cdot x^2

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 0= (5-12j)\cdot x^0 + (0+0j)\cdot x^1 + (1+0j)\cdot x^2  which can be simplified as x^2 +5 -12j= 0  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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments