Top: |P(x)| at each root as the method found it (circle), after polishing (diamond), and the error bound of plain double evaluation (dash). A method has to stop near that dash; polishing evaluates P(x) as if in twice double precision and continues below it. Values that are exactly 0 are drawn on the bottom line. Bottom: how far polishing moved each root, relative to |z|, compared with one ulp; the number of correct digits of the method's root is about \(-\log_{10}\) of that value.
The cluster plot (only shown when roots were grouped) is magnified around the final root. It shows the method's approximations, their Newton circles (each contains at least one root, and they overlap, which is why the approximations were grouped), the centroid where polishing started, the Newton steps on \(P^{(m-1)}\), and the verified \(m\)-fold root. Use the dropdown to switch between clusters.
How far can each root be trusted? The stored coefficients carry relative errors of about one unit roundoff \(u=2^{-53}\). Every point inside the contour marked \(k\) is an exact root of some polynomial whose coefficients differ from the given ones by a relative \(10^{k}\). A small region around a root means the root is well determined; a large region means a tiny change in the coefficients can move the root far. The condition number of a simple root is \(\kappa=\sum|a_k||z|^k/(|z|\,|P'(z)|)\), and it can lose about \(\log_{10}\kappa\) of its 16 digits; an \(m\)-fold root moves about \((m!\,u\sum|a_k||z|^k/|P^{(m)}(z)|)^{1/m}\). The values for each root are listed in the Results tab. Try Wilkinson's polynomial (x-1)(x-2)...(x-20).
Click Compare Methods to run all methods on the current polynomial. Time is the average over repeated solves. Correct digits are for each method's worst root before polishing, compared with the most accurate method's roots after polishing. Iterations are not directly comparable between methods: Durand-Kerner and Aberth-Ehrlich update all roots in each iteration, the Eigenvalue method counts QR iterations, and Graeffe counts root squaring steps (including restarts after a shift). The table is added to the Results tab.
Polynomial root finder
This polynomial solver finds all the
real and
complex roots of a polynomial of any degree with
real or
complex coefficients, \(a_nx^n+a_{n-1}x^{n-1}+\dots+a_1x+a_0\), with any of 14 methods.
Everything runs in your browser in IEEE754 double precision.
Entering a polynomial
- Type the polynomial as you would write it, e.g. x^3-3x+4, and press Find Roots. x^3 stands for x3. The terms can be in any order: -1.2+3.5x^3+4.5x-1.2E-1x is just as valid.
- Numbers can have an exponent with e or E: 120, 1.20e2, 12E+1 and 1200E-1 are all 120.
- Complex coefficients are written with i, in parentheses where needed: (3-i4)x^3-2x^2-i4x-3.
- Products, quotients, powers and parentheses are expanded: (x-1)(x-2)^3 or (1+2*3x^4-5x^2)^3. Note that expanding a product rounds the coefficients to double precision; for large coefficients (above \(2^{53}\approx 9\cdot10^{15}\)) the stored polynomial then differs slightly from the one you typed, see the Root sensitivity report.
- Test and Test Complex enter an example polynomial with real and with complex coefficients. Shift asks for a value c and prints the coefficients of P(x+c).
Methods and options
- Method selects one of 14 methods: Newton (default), Halley, Ostrowski multi-point, Householder, Jenkins-Traub, Laguerre, Eigenvalue, Durand-Kerner, Aberth-Ehrlich, Chebyshev, Ostrowski square root, Arithmetic Mean Newton, Steffensen and Tangent Graeffe.
- Compare Methods runs all 14 methods on the polynomial and reports time, iterations per root, correct digits of the worst root and the largest backward error. See the Plot Comparison tab.
- Polish roots (on by default) refines every root on the original polynomial in twice the working precision and detects multiple roots. See the Why Polishing tab.
- Verbose Mode prints every iteration step, including the safeguards of the method. It is the best way to see how a method approaches a root.
- Display Iteration trail prints and plots the iterates of every root, the iteration statistics and the error polynomial (the polynomial rebuilt from the roots minus the original).
- Display: Auto scaling scales the axes of the plots independently.
The results
The Results tab lists the roots X1, X2, ..., the polishing report, and a
Root sensitivity report that says for each root how many digits the coefficients determine: a root with condition number \(\kappa\) can lose about \(\log_{10}\kappa\) of its 16 digits, and an \(m\)-fold root keeps only about \(16/m\). The plot tabs show the roots in the complex plane, \(|P|\) and the order of convergence along the iterations, \(|P(z)|\) as a 3D surface and as contours, the effect of polishing, the pseudozero sets of the polynomial (Plot Sensitivity) and the comparison of all methods.
Background
The methods, the stopping criteria, polishing and the test results are described in the paper
Practical Implementation of Polynomial Root Finders at
www.hvks.com/Numerical/papers.html.
Email: hve@hvks.com if you have any questions.
This version has been tested with current versions of Edge, Chrome, Firefox and Safari.
The default method: Newton's method as designed by Madsen
Let the polynomial be \(P(x)=a_nx^n+a_{n-1}x^{n-1}+\dots+a_1x+a_0\) with real or complex coefficients. The solver finds all roots of \(P(x)=0\).
By default it uses Newton's method in the form developed by Kaj Madsen [K. Madsen, A root-finding algorithm based on Newton's method, BIT 13 (1973) 71-75], which is fast, reliable and handles the classical problems of Newton's method: poor starting points, saddle points and multiple roots.
The same framework is used by the other Newton-type methods (Halley, Householder, Chebyshev, Ostrowski square root, Laguerre, Ostrowski multi-point, Arithmetic Mean Newton and Steffensen).
The roots are found one at a time. Each root is divided out of the polynomial (deflation) and the search continues on the polynomial of one degree lower; the last one or two roots are found directly with the quadratic formula.
- Starting point. The search starts inside a circle that contains no roots, so that the roots tend to be found in increasing order of modulus. Composite deflation, which combines forward and backward deflation, keeps the deflation stable whatever the order.
- Stage 1. Far from a root every step is checked: if \(|P|\) increases, the step is shortened or rotated; if it decreases, longer steps \(m\cdot dz\), \(m=2,3,\dots\) are tried in the same direction. This gives quadratic convergence also to a root of multiplicity \(m\), without knowing \(m\) in advance.
- Stage 2. When the iteration is known to converge, plain steps of the method are taken until the stopping criterion is met.
- Stopping criterion. The iteration stops when \(|P(z)|\) is no larger than a bound on its own rounding error, so the root is as accurate as the evaluation allows: Kahan's bound at a real point, Adams' bound for real coefficients at a complex point [D. Adams, Comm. ACM 10 (1967) 655-658], and a running error bound for complex coefficients.
Check
Verbose Mode to see the stages, the trial steps and the stopping criterion at work.
Root polishing: refining the roots after any method
When the
Polish roots option is checked (the default), every root found by the selected method is refined before it is shown. It works the same way after all 14 methods, and it needs only ordinary IEEE754 double precision: no interval or arbitrary precision arithmetic.
Polishing is not a separate root finder. The selected method must first find all the roots approximately; polishing then takes them the last few digits to the limit that double precision allows.
Why polish the roots?
- Deflation errors. Methods that find one root at a time divide each root out of the polynomial and continue on the smaller, deflated polynomial. Rounding errors in the deflation make the later roots slightly less accurate. Polishing refines every root against the original polynomial, so these errors disappear.
- Rounding noise. Near a root, the computed value of \(P(x)\) in double precision is mostly rounding noise, so any method has to stop when \(|P(x)|\) reaches that noise level. Polishing evaluates \(P(x)\) more accurately, which lets it go further.
- Multiple roots. At a root of multiplicity \(m\), ordinary double precision only gives about \(16/m\) correct digits: about 8 digits for a double root, 5 for a triple root. Polishing recognises multiple roots and returns them to full accuracy.
How it works
- Accurate evaluation. \(P(x)\) is evaluated with the compensated Horner scheme. Each multiplication and addition in Horner's method is done with an error-free transformation (Knuth's TwoSum and Dekker's TwoProduct), which gives the rounded result together with its exact rounding error. The errors are added up in a second Horner recurrence and applied as a correction at the end. The result is as accurate as if \(P(x)\) had been computed in twice double precision (about 32 digits) and then rounded to double [Graillat, Langlois & Louvet 2009].
- Newton steps on the original polynomial. Each root is improved by Newton's method \(x_{k+1}=x_k-P(x_k)/P'(x_k)\), with both \(P\) and \(P'\) evaluated in the accurate way. A step is only kept if it makes \(|P(x)|\) smaller, and a root may never move more than half way towards its nearest neighbour, so it can never jump to another root. Usually one to three steps are needed. The iteration stops when \(|P(x)|\) is below the error bound of the accurate evaluation. A real root of a polynomial with real coefficients stays exactly on the real axis.
- Detecting multiple roots. For each root the radius \(r=n\,|P(x)|/|P'(x)|\) is computed, where \(n\) is the degree. A circle with this radius around \(x\) always contains at least one root of \(P\). Roots whose circles overlap are grouped into a cluster. Well separated simple roots have small circles and stay on their own; the \(m\) approximations of an \(m\)-fold root have overlapping circles and form a cluster of size \(m\).
- Refining a multiple root. An \(m\)-fold root of \(P\) is a simple root of the derivative \(P^{(m-1)}\). Newton's method is therefore applied to \(P^{(m-1)}\), starting from the average of the \(m\) approximations, where it converges quickly and to full accuracy.
- Verification. The result is only accepted as an \(m\)-fold root if \(P, P', \dots, P^{(m-1)}\) all vanish there to within a few units of rounding error. Otherwise the cluster is treated as close but distinct roots, and each of them is polished on its own. This way two different roots that merely lie close together are not merged, unless they are so close that the polynomial cannot tell them apart within rounding error.
Reading the polishing report
After the solutions, the result area lists one line per root, in the same order X1, X2, ... as the solutions, for example:
X1=1 |P| 9.0e-12 -> 0.0e+0 radius<=0.0e+0
X2=2 |P| 2.5e-12 -> 0.0e+0 multiplicity 2, backward error 0.0u
- |P| a -> b is \(|P(x)|\) at the root found by the method, and at the polished root.
- radius<=r (simple roots) is the radius of a circle around the polished root that contains the exact root of the polynomial. It is usually below one unit in the last place of the root, and 0 when \(P(x)\) is exactly zero, as for integer roots.
- multiplicity m, backward error w u means the root was verified as an \(m\)-fold root; \(w\) is how far the derivatives are from zero, in units \(u=2^{-53}\) of rounding error.
- in a cluster of m (not a verified multiple root) means the roots were close together but did not pass the verification, so each was polished as a simple root.
With polishing switched on, the reconstructed polynomial and the error polynomial (shown with
Display Iteration trail) are also computed from the polished roots. Verbose mode adds the number of Newton steps used for each root.
Limitations
- Polishing finds the roots of the polynomial as it is stored in double precision. Coefficients such as 0.1 or 1/3 are already rounded when they are read, and coefficients larger than \(2^{53}\approx 9\cdot10^{15}\) cannot all be stored exactly.
- For badly conditioned polynomials this matters a lot. In Wilkinson's polynomial \((x-1)(x-2)\cdots(x-20)\), five of the coefficients are rounded when stored, and that alone moves the root at 13 by about \(6\cdot10^{-4}\). Polishing returns the roots of the stored polynomial, which is the best any method can do in double precision.
- The error bounds are computed in ordinary rounding, so the radius is a very good estimate rather than a mathematical proof.
Uncheck
Polish roots to see the roots exactly as the selected method found them.
References: S. Graillat, P. Langlois, N. Louvet: Algorithms for accurate, validated and fast polynomial evaluation, Japan J. Indust. Appl. Math. 26 (2009) 191-214. S. Graillat: Accurate simple zeros of polynomials in floating point arithmetic, Comput. Math. Appl. 56 (2008) 1114-1120. T.J. Dekker: A floating-point technique for extending the available precision, Numer. Math. 18 (1971) 224-242.