Generate a Monic Polynomial with given Complex roots in Python
Last Updated :
26 Apr, 2025
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
[Tex]1x^n + c_{n}x^{n-1} + …+c_{1}x +c_{0}x^{0} = 0[/Tex]
If a polynomial highest degree coefficient is 1. i.e called a monic polynomial.
For example:
[Tex]x + 7 = 0 \\ x^5-4x^3+2x+5 =0 [/Tex]
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:
[Tex]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[/Tex]
Python3
from numpy.polynomial import polynomial
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 [Tex]0+1\cdot i, 0-1\cdot i [/Tex] then the equation will be :
[Tex]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[/Tex]
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 [Tex]2+3\cdot i, -2-3\cdot i [/Tex] then the equation will be :
[Tex]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[/Tex]
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 [Tex]0= (5-12j)\cdot x^0 + (0+0j)\cdot x^1 + (1+0j)\cdot x^2 [/Tex] which can be simplified as [Tex]x^2 +5 -12j= 0 [/Tex] 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.
Similar Reads
Generate a Legendre series with given complex roots in Python
In this article, we will cover how to generate the Legendre series with given complex roots in Python using NumPy. ExampleInput: (4+5j) Output: [9.33333333-40.j 0. +0.j 0.66666667 +0.j] Explanation: An array of legendre series.legendre.legfromroots method In python, the Legendre module provides many
2 min read
Generate Chebyshev series with given complex roots using NumPy in Python
In this article, we will cover how to generate the Chebyshev series with given complex roots in Python using NumPy. Example Input: (4+5j) Output: [9.5-40.j 0. +0.j 0.5 +0.j] Explanation: An array of Chebyshev series.chebyshev.chebfromroots() method In python, the Chebyshev module provides many funct
2 min read
Generate a Legendre series with given roots in Python
In this article, we will see how to generate a Legendre series with given roots in Python. Legendre classIn python, the Legendre module provides many functions like legfromroots to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Legendre
2 min read
Generate a Hermite_e series with given roots using NumPy in Python
In this article, we will cover how to raise a Hermite_e series to power in Python using NumPy. hermite_e.hermefromroots() function We use the hermite_e.hermefromroots() function present in the NumPy module of python to construct a Hermite_e series with the given roots. A 1-D array of coefficients is
2 min read
Convert a Hermite series to a polynomial in Python
In this article, we will be looking at the step-wise procedure to convert a Hermite series to a polynomial in Python. NumPy.herm2poly method To convert a Hermite series to a polynomial, use the np.herm2poly() function from the Numpy package of python. Convert the given array representing the coeffic
2 min read
Convert a polynomial to Hermite_e series using NumPy in Python
In this article, we will cover how to convert a polynomial to Hermite_e series using NumPy in Python. hermite_e.poly2herme method We use the hermite_e.poly2herme() function present in the NumPy module of python to convert a polynomial to a Hermite series. Here we need to convert an array of polynomi
2 min read
Generate a Vandermonde matrix of the Chebyshev polynomial in Python
In this article, we will be looking at the approach using various functionalities of the Numpy packages to generate a Vandermonde matrix of the Chebyshev polynomial in Python. NumPy.chebvander method To generate a Vandermonde matrix of the Chebyshev polynomial, the user needs to call the np.chebvand
3 min read
Compute the roots of a Legendre series in Python-NumPy
In this article, we will discuss how to compute the roots of a Legendre series in python. legendre.legroots method In python, the Legendre module provides many functions like legdre to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Lege
2 min read
How to Find Complex Zeros of a Polynomial Function
To find complex zeros of a polynomial function, use the following steps: factor the polynomial, apply the quadratic formula if necessary, and solve for the roots. If the discriminant is negative, the roots will be complex. Complex roots occur in conjugate pairs. What are Polynomial Functions?A polyn
6 min read
Differentiate a polynomial and set the derivatives in Python-NumPy
In this article, we will cover how to differentiate a polynomial and set the derivatives in Python. numpy.polynomial.polynomial.polyder method The Numpy library provides the numpy.polynomial.polynomial.polyder() method to differentiate a polynomial and set the derivatives. The polynomial coefficient
3 min read