import numpy as np
from scipy.linalg import companion
try:
import executable_engineering as exe
except ImportError:
%pip install -q executable_engineering
import executable_engineering as exe
from executable_engineering import plot_polynomial_roots_companion
Explicit formulae¶
Polynomial roots are nice because we can solve for them directly. Polynomials of degree have exactly roots, including multiplicity (coincident roots). The roots may be complex, even in polynomials with real coefficients. If the root is complex (), then its complex conjugate () is also a root.
The roots of polynomials of order can be solved analytically.
E.g.:
has roots:
per the quadratic formula.
Certain polynomials of order may have analytically solvable roots but there is no such general formula per the Abel-Ruffini theorem.
Polynomial roots via the companion matrix¶
All the roots of polynomials may be found numerically by solving for the eigenvalues of the polynomial companion matrix. This is the basis for modern numerical methods which we will cover when we get to matrix eigenvalue calculations.
The companion matrix is formed by placing the negated, normalized coefficients of the polynomial in the top row, and placing 1s along the first subdiagonal. (If the polynomial’s leading coefficient is not 1, all coefficients are first divided by the leading coefficient).
For example, consider a simple monic cubic polynomial (leading coefficient is 1):
The companion matrix is constructed as:
Recovery of the original polynomial¶
To see why this works, consider the eigenvalues , which we can solve from the characteristic equation :
Expanding the determinant along the third column:
Because the characteristic polynomial of this matrix precisely recovers the original polynomial , finding the eigenvalues of the companion matrix is mathematically equivalent to finding the roots of the polynomial!
Example: Polynomial roots via the companion matrix¶
The following tools generates a random matrix of degree , shows the associated companion matrix and calculates its eigenvalues.
N.B. in many software environments, is used as the imaginary number instead of .
plot_polynomial_roots_companion(4)Polynomial:
4 3 2
8.978 x + 9.313 x + 6.168 x - 3.908 x - 8.047
Companion Matrix:
[[-1.04 -0.69 0.44 0.9 ]
[ 1. 0. 0. 0. ]
[ 0. 1. 0. 0. ]
[ 0. 0. 1. 0. ]]
Roots from eigenvalues of companion matrix:
[-0.44+1.03j -0.44-1.03j -0.92+0.j 0.77+0.j ]
Roots of a Fourier Series¶
The companion matrix method can also be cleverly adapted to find the roots of a Fourier series. The core idea is to use Euler’s formula and a change of variables to transform a finite Fourier series into a standard algebraic polynomial.
1. Express as a Complex Exponential Series Consider a finite Fourier series of degree :
Using Euler’s formula ( and ), this can be rewritten as a sum of complex exponentials:
(where the complex coefficients are derived from and ).
2. Change of Variables We introduce a new complex variable, . This means , and . Substituting this into our series yields a Laurent polynomial:
3. Convert to a Standard Polynomial To eliminate the negative exponents and turn this into a standard polynomial, we simply multiply the entire equation by (since , we know ):
We now have a standard polynomial of degree .
4. Solve and Map Back to the Original Domain Because it is a standard polynomial, we can construct its companion matrix and compute its eigenvalues to find all complex roots for .
Finally, we must map our roots back to the original variable . Since , any strictly real root will result in a root that lies exactly on the unit circle in the complex plane ().
We filter the eigenvalues for those where (allowing for minor numerical floating-point errors). The real roots of the original Fourier series are simply the angles of these complex numbers:
Summary of the companion matrix approach¶
This method has the benefits that it:
finds all the roots simultaneously
does not require initial guesses
is numerically robust thanks to robust eigenvalue algorithms
However, it is limited to appropriate scalar functions that are expressed in a linear basis:
standard polynomials (including taylor expansions!)
orthogonal polynomials (Legendre, Hermite, Laguerre,... )
Fourier series expansions