Click here to Skip to main content
Click here to Skip to main content

Polynomial Equation Solver

, 29 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Solves 1st, 2nd, 3rd and 4th degree polynominal by explicid fomulas for real coefficients and any degree by the numerical Jenkins-Traub algorithm with real and complex coefficients.

 

Introduction 

Recently I came across a situation where I needed to solve a 4th degree polynomial equation in .NET, and to my surprise I couldn't find any code written in C# or VB .NET that contained either the explicit algebraic formulas, or the numerical algorithm Jenkins-Traub. (hvowever I did find the Jenkins-Traub algorithm translated from the original Netlib site in FORTRAN and translated into C++ by Laurent Bartholdi for Real coefficients and a C version written by Henrik Vestermark. It is both of these application that are converted from C++ and C into C # and VB code by me.)   

NB: There might be a licence attached to the Jerkins-Traub algorithm for commercial use, please check this before you use it in a program that you want to sell to others.

The explicid algebraic formulas that is also implemented are nasty, and would definitely clog up your day if you ever had a need for one, so I decided to share it with you. I also have a rather lengthy story to follow it, as I have read "The equation that couldn’t be solved" by Mario Livio. The title of the book refers to the story of the insolvability of the 5th degree polynomial equation, but it also goes through the historical development of the solutions to lower degree polynomial equations.   

I won’t give any explanations of how these formulas were derived, as the formulas get quite lengthy, so long that in fact that even Tartaglia (one of the people that had a part in solving the cubic equation) had problems in remembering all the rules he had discovered.     

As for the numerical algoritm Jenkins-Traub, it has been completely translated from a C++and C version into VB.NET and C# by me, and as far as I have tested it, it seems to be working fine. It should be mentioned that the Jenkins-Traub algorithm generally uses explicid formulas for 1st and 2nd degree, while using the numerical approximation on 3rd degree and above.    

The 1st degree polynomial equation  

This is fairly easy to solve mathematically so if you are experiencing trouble here, you should probably not download the code. I am of course talking about equations on the form 2x + 3 = 7, and the simple equation is called linear equations, as they can be represented by lines when graphed (or drawn).


However the story behind equations is quite interesting, so I’m going to take you back to 2000 BC - 600 BC to  the Babylonian civilization in Mesopotamia. The word equation should be used with care in this context, as the Babylonians didn’t actually use algebra to solve these equations, but instead they ventured into length debates and logic to solve the problems. This might be a way for making math even more difficult and incomprehensible than we have ever had, espesially when dealing with higher order polynomial equations.

The result of this way of solving mathematical problems without using algebra meant that the Babylonians could not find any general patterns or formulas behind the various mathematical problems. Despite the cumbersome formulations of the mathematical problems they did manage to solve a pairs (meaning equations with both x and y as unknown) of linear equations . 


Babylonians didn't seem that keen on producing many text with the 1st degree equation, as for instance the Egyptians, as it seems that the Babylonians thought it too elementary for any detailed discussion.  In Egypt however there exists large manuscripts on the subject that presents mathematical "recipes" with the solution to some problems, as kind of a cookbook.


It should be mentioned that the Chinese collection Nine chapters on Mathematical Art (Jiu zhang suan shu), one can find the solutions to none fewer than three linear equation with three unknowns, which is quite a feat consider how cumbersome the procedure was. 

The 2nd degree polynomial equation

The origin of the famous solution to the second degree polynomial equation is actually not known, but it is known that Babylon’s actually made a great deal of detailed account on how to solve the equation, but not where they had got it from or how they discovered it. The Babylonian understood how to solve a quadratic equation on the form x^2 - x = 870 and could find the positive solution, as the solution were intended to be used in for instance land measurements and problems like that. They did however ignore equations of this type that had two positive solutions, as these were seen to be illogical solutions. This is also true about the very early Greek mathematician Euclid, as he solves the quadratic equation using geometry, and not by algebra. To compare the mathematical knowledge in the ancient world, the Egyptians on the other hand only knew how to solve equations on the form of x^2 = 4 and not how to solve x^2 + x = 4, and they could also only find the positive solution.  


The Greek civilization soon managed to resolve some of the issues with the help of the brilliant mathematician Diaphanous. He effectively advanced the way solutions are presented, as a halfway point between the Babylonian wordings of the equations to the modern way of using algebra. His book Arithmetica shows solutions to three different types of the quadratic equations, as well as the famous Diaphanous equations, which Fermats Last Theorem is an example of. Fermat in fact read Aritmethica and it was in this very book by Diaphanous that he wrote his famous last theorem in the margin. As for Diaphanous himself, we actually know very little of him, one can't even be curtain when he actually lived, except that it probably was in Alexandria in the period between A.D. 200 and 214 to 284 or 298.

With the fall of the Greek civilization the mathematical progress in the west came to a halt, and went into hibernation for nearly a millennium. The progress of mathematics now turned east, and one of the greate mathematicans of his age, Brahmagupta from India, who managed to solve some of Diaphanous equations, as well as being the first to give solutions of the 2nd degree polynomial equations that also involved negative numbers. He realized that negative numbers could be seen as “debts” and positive numbers as “fortune”, very much like an accountant of today would, and thus making a hugh breakthrough in mathematics.


The next greate step in solving equations was done by the development of algebra, and got its name from the Arab mathematician Muhammad ibn Musa al-Khwarizmi, or rather his book “Kitab al-jabr wa al-muqabalah”, referring to the word al-jabr as the basis of the modern word algebra. Al-jabr means “restoration” or “completion” which is quite fitting concerning the importance of the mathematical development this would entail. His books weren’t ground breaking in new material; instead it was the systematic treatment of the solutions to the quadratic equation that was the real genius in it. However, the complete set of solutions of the quadratic equation didn’t appear into Europe until the twelfth century in Spain.

The implementation of the quadratic formula on a computer is however not so straight forward as one would initially think. We assume the equation on the form:

 

The solution is the well known formula, and I'm ofcource thinking about this one: 

However, you'll be asking for trouble if you actually implemented the formula this way on a computer. The reason is that if the coefficients a or c are very close to zero, you could get hugh truncation errors. The coerrect implementation to find the roots on the computer are:

 

Even if the coefficients are complex the computer formula still holds, although one needs to take into account on how to take the sign of the square root:

 

In the formula above the Re stands for the real part of the solution and the asterisk is the complex conjugate of the complex number.

The 3rd and 4th degree polynomial equation

This is often referred to as the cubic or the quartic equation or function and, not surprisingly it turns up when we want to find the volume of something. The actual general equation wasn’t solved until the sixteen century, although a few special cases were solved by the Babylonians and a few more was given by the Persian poet Omar Khayyam in the twelfth century. However, there wasnt a real tangible need for the solution to the cubic equation. No one was waiting for it to be discovered, it was more of a mental challenge, sort of the Olympic games of mathematics that would determine the greates intellect of his day.


The first partial solution to the cubic equation would come from the oldest currently open university, the University of Bologna, which has been open since its establishment in 1088.  After having been presented with a problem in 1501 that involved third degree polynomial equations, Scipione del Ferro decided to work on the problem.  And around 1515 he managed to find a method for solving the cubic equations that had the form x3+mx = n. Del Ferro did not publish his result, which unfortunatly was quite normal in those days, and only told his student Antonio Fiore and his son-in-law about his discovery on his own death bed. Fiore then seem to think that the formula was his to use as he pleases from that point on, but did not publish it right away, but instead waited for the right moment to appear.


So when Niccolò Tartaglia in 1530 announced that he could solve some problems concerning the cubic equations, Fiore decided that his moment had come and challenged Tartaglia to a mathematical dispute. Each contestant would give the other one 30 problems to solve, and the loser would pay a monetary price to the winner. Each of them would have forty or fifty days to solve the problems.


By the time the problems were handed over to Tartaglia, he managed to solve all of them within a space of two hours! And Fiore could not solve any of the problems given to him and he did not know the equations on the form x3 + mx2 = n so Tartaglia won the competition.  


In 1539, after a massive presuation campain, a character by the name of Gerolamo Cardano (he actually earned his allowance by gambling when he was a student, and was known for beiing load and rude to the people around him) managed to pursvade Tartaglia to reveal the formula, under the condition that Cardano would not reveal it. However Cardano found out about del Ferro's solutions from del Ferro's son-in-law, and decided that he was not bounded by the agreement with Tartaglia, as he would present del Ferro's solution and not Tartaglia s, so he published the result in his book Ars Magna. This book is by many contemporary mathematicians considered to be the start of the modern algebra, and it involves solutions with complex numbers, although Cardano did not understand this in detail, as some part of this was from Tartaglia solutions involved squre root of a negative number . (Rafael Bombelli is often considered as the discoverer of complex numbers, as he did far more studies of the subject.) After the book was published, Tartaglia immediately posed a challenge to Cardano, which were a rather poor mathematician by the standard of Tartaglia, and he promptly refused. However, Cardano’s student, Lodovico Ferrari, send numerous public challenges to Tartaglia, which Tartaglia denied. Eventually Tartaglia was offered a job at a university, given that he would beat Ferrari in a dispute. Ferrari had discovered a general way of solving the cubic equation, which was not known to Tartaglia, he had also found the solution to the quartic equation as early as 1540, but this required the solution of the cubic equation so it wasnt published until Cardano found out about del Ferro's solutions.


Ferrari won the dispute with Tartaglia, whom left just before the first day into the dispute was over. (Ferrari was quite a character, as he had lost all his fingers on his right hand at the age of seventeen in a brawl). His career skyrocketed from this point on, though he was allegedly later poisoned by his sister and died.

The real discoverer of the third degree formula is quite complicated as you understand, although the solution to the 4th degree equation seems to be Ferrari's work alone.

The fomulas are not usually implemented on a comuter, as the solutions found by numerical technique is nearly always better with them. If you would still like to use the explicid formulas, you should implement Viete's formula that uses trigonometric functions instead of Ferraris solutions.

Higher degree polynomial equations   

After the Quartic equation was solved using algebra, many tried to solved Quintic or 5th degree polynomial equation, and they all failed. It wasn't proved until Abel found a general proof of it in 1823 that stated that you actually couldn't. The theorem is known today as the Abel–Ruffini theorem or as Abel's impossibility theorem. The reason for the double name is that Ruffini gave an incomplete proof of the theorem in 1799, which Abel didn't know about until 1826. After reading it and studing it he said that Ruffini's work was so complicated to understand that he wasn't sure if it were correct.  

It is however important to know what the proof actually means, and crutially, what it dosen't mean. Abel's proof simply states that one can not find a general solution to all the roots in a Quintic or any higher order polynominal equation by the use of algebra. Abel used a generalization of the Euler integrals to prove it, and the german mathematican Jacobi was beside himself that this discovery had gone unnoticed by the mathematical community.

One can however find the solution to 5th degree equations by the use of numerical methods (Newton-Rapson) or by the use of elliptic integrals. If one uses Évariste Galois theory one can also find out what type of solutions one would find. Évariste Galois also proved that the 5th degree polynomial equation could not be solved independently of Abel and Ruffini's work, and his proof was published posthumously in 1846

The problem with explicid formulas

There are problems with the explicid formulas that have to do with the finite storage space on the computer, which can in some instances give such a high error that it would render the calculated solutions far from the true values. In practice, solutions for the 3rd and higher is almost always better with the Jenkins-Traub algorithm or other simular techniques, than the solutions calcualted with the explicide formulas. 

There are however problems with the numerical estimation of polynomial, as any person with sufficient understanding of the algorithms behind them, easily could construct an example that would fail to converge. Take the equation (1) below: 

Given that it is a 5th degree polynomial solutions could only be found using a numerical algorithm, but we know, from the Fundemental theorem of algebra, that the equation will have exactly 5 real or complex solutions, as Gauss and others have proved. The exact solutions to the equation 1 are:  The problem with this example is that the implemented Jenkins-Traub in C # and VB would not converge (most likely to the double precition that is choosen for the storage of data, as well as the epsilon, min and max values that were "exported" from the float library in VC++). In fact, it can always be problematic for an algorithm of this kind to converge, given the number of multiple roots. The reason is that the algorithm extracts the found roots from the original equation, and with a too large numerical error it can result in that no other solutions are found. So make a note of the fact that any method which uses Newton type iterations, could have problems with multiple roots. This also apply to the circumstances were the roots are closely together (or very far apart) from each other.

Jenkins-Traub indeed uses a Newton type method to find the roots, and this could also have other problems. If a polynimial with no linear term is zero at zero, the iterative technique would fail to converge, given that both the function and its derivative is zero at zero.

There is another quite surprising thing about polynomials that have the multiple roots. it is that the derivative polynominal will have one fewer of the same roots. Take the example form equation 1, if we derivate the equation we will get:

And when we try to solve this equation using the same Jenkins-Traub algorithm as previously, we find that indeed the same roots are still there, with an additional that wan't. The solutions could easily be checked by inserting the values into the original equation (1):

The actual code has turned so complex that it is hard, if not impossible, to explain it all. Instead Im going to refer you to the procedure of the Jenkins-Traub described on Wikipedia.

It is however important to know that the Jenkins-Traub Algorithm is considered de facto way of numerically calculating the roots of a polynomial. It is extencively tested and it is also implemented in many products that are in comercial use (i.e. Matematica and others).

As for the explicid formulas, they should be used with care, and cannot be assumed to give the correct results for any real coefficients you put in, though the description given in the program should indicate if it is a recognized type, and if the output description maches the calculated roots. Several differnt kinds of mehods are employed to find the roots, among them is Ferraris equation, the depressed biquadratic equation and many others.

References   

The Wolfram site that provides some formulas used, and other links to the numerical algorithm that is used:

C++ source code:  

Books:    

Other links:  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kenneth Haugland
Engineer
Norway Norway
I hope that you like the stuff I have created and if you do wish to say thank you then a donation is always appreciated.
You can donate here[^].

Comments and Discussions

 
GeneralRe: My vote of 5 PinmemberKenneth Haugland1-Mar-13 23:52 
GeneralWell done Kenneth PinmvpEspen Harlinn28-Feb-13 0:56 
GeneralRe: Well done Kenneth PinmemberKenneth Haugland28-Feb-13 4:26 
GeneralRe: Well done Kenneth PinmvpEspen Harlinn28-Feb-13 7:10 
GeneralRe: Well done Kenneth PinmemberKenneth Haugland28-Feb-13 7:38 
GeneralWoaw!!!! Pinmemberhenrikqwerty32127-Feb-13 5:43 
GeneralRe: Woaw!!!! [modified] PinmemberKenneth Haugland27-Feb-13 7:13 
GeneralRe: Woaw!!!! PinmemberMember 820801427-Feb-13 13:28 
I wasn’t aware that the solution to a fourth degree was so involved. That's a lot of typing.
You could, of course, simplify the code greatly if you reused a few temporary variables.
GeneralRe: Woaw!!!! [modified] PinmemberKenneth Haugland28-Feb-13 4:21 
GeneralRe: Woaw!!!! Pinmembergcode12-Mar-13 17:25 
GeneralRe: Woaw!!!! PinmemberKenneth Haugland2-Mar-13 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 30 Mar 2013
Article Copyright 2013 by Kenneth Haugland
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid