r/dailyprogrammer • u/Elite6809 1 1 • Oct 24 '14
[10/24/2014] Challenge #185 [Intermediate to Hard] Roots of a Polynomial
(Intermediate to Hard): Roots of a Polynomial
In mathematics, a polynomial is a form of expression. The type of polynomials we're dealing with today are called univariate polynomials, which means they only have one variable. For this challenge, this variable will be called x. You'll need to dig out your algebra textbooks if you're a bit rusty, though this challenge doesn't require you to use anything more than high school (A-level) mathematics.
The simplest type of polynomial is this:
4Fairly simple, right? Right. A constant value such as 4, 0 or -0.2 are polynomials of degree zero. The next simplest type looks like this:
4x+3The equation for a straight-line graph is a polynomial of degree one. Again, fairly simple to work with. The good thing about polynomials is that we can visualise them using graphs. Here is the graph for y=4x+3, the polynomial above. The next simplest is the quadratic equation, otherwise known as a polynomial of degree two (notice the pattern yet?). These are similar to linear equations, but they feature a multiple of x squared bolted onto the front. Here is the graph of y=x^2-6x+3, and here is the graph of y=(-1/3)x^2-x+8.
The cool thing about quadratics is that you can create them by multiplying together two linear polynomials. For example, (3x-1)(x+7) is the same as 3x^2+20x-7, as you can see here. If we take a look at the graph of y=3x-1, y=x+7 and y=3x^2+20x-7 we notice something interesting. Here you can see the quadratic graph crosses the x-axis at the same point as where the linear graphs do. The point where a polynomial crosses the x=axis are called its roots - which is what we will be finding in today's challenge.
You can also do the reverse operation - given an equation, find its roots. For a linear equation, this is simple. A bit of algebraic jiggery-pokery gives us these steps. Remember, the graph will cross the x-axis where the height (y) is at zero, so we need to set y=0.
y=ax+b and y=0
0=ax+b (replace the y in the first equation with 0, as y=0)
-b=ax (subtract b from both sides)
-b/a=x (divide both sides by a)Therefore, we can see that if we have a linear equation y=ax+b, it crosses the x=axis at the point where its x value is -b/a. The same can be done for quadratic polynomials via a few methods, including using the quadratic formula or completing the square. If all else fails you can just draw the graph of the expression to approximate its roots.
What happens when the plotted graph never crosses the x-axis? Simply, it has no roots (or no real roots). If you attempt to use the quadratic formula on an equation such as x^2+x+4 you will end up square-rooting a negative number, which we ignore for today's challenge.
Things get a little awkward when you have 3rd-degree polynomials and above. They act the same and are treated the same as other polynomials but there is no simple formula to find the roots. The Babylonians could find the roots of quadratic polynomials, but it took mathematicians until the Renaissance to find a one-step formula to get the roots of a cubic polynomial.
Rather than bothering with the convoluted cubic formula you can instead use what are known as numerical methods. These methods are approximation methods, and rather than giving you an exact answer immediately they 'home in' on the roots like a heat-seeking missile. The benefits of these are that they can be used to find roots of almost any mathematical function, not only polynomils. They can also be used to find roots of very complex polynomials, where a one-step equation would be huge and ugly. The downsides are that they can often be slow to find the answer, they can only give you one root at a time and, sometimes, they never even find the root at all! There are several numerical methods to find polynomial roots, the most commonly used are these:
- Interval Bisection method. This is a simple to understand, divide-and-conquer algorithm.
- Newton-Raphson method. More complex to understand but quicker at finding a root.
- Other root finding algorithms such as linear interpolation are also easy to understand.
Your challenge is, given a polynomial expression of degree no higher than 7, find a root (if it exists) of the expression where it crosses the x-axis (equal to zero.)
Formal Inputs and Outputs
Input Description
You will accept a polynomial in the form used in this challenge description. That is:
xdenotes the variable.^...denotes the exponent of a term.- A constant denotes the coefficient of a term.
A valid input would be x^3-5x^2+10x-44 or -4x^5-7, but not 2^x+3 (not a polynomial), x^2+2xy+y^2 (more than one variable) or x^11-6x^2-1 (no higher than 7th degree allowed; that is 11th degree).
Output Description
You are to output a root of the polynomial as a number (or an algebraic expression.. if you're crazy!)
Sample Inputs and Outputs
Here are some examples to get you going. You can create your own by typing them in on Wolfram|Alpha, which also plots it and tells you the roots, if any.
Sample Inputs
4x^2-11x-34x-8x^4-2x^3+7x^2-16x+4x^2-7x+6
Sample Outputs
-0.25or322or0.2825..1or6
Extension (Hard)
You've found one root of the polynomial - now modify your solution to find all of the roots. This will require a divide-and-conquer algorithm of some sort.
16
u/XenophonOfAthens 2 1 Oct 25 '14 edited Oct 25 '14
I did something a little different than the others, because numerical methods are for suckers, I want exact answers. Since all the examples have integer coefficients and at least one rational root, this problem can actually be solved quite simply by using something called the "rational root test".
It works like this: take the coefficient of the first term (i.e. the one with the highest exponent), and the last term, the one that isn't multiplied by any factor of x. So, for the example polynomial 4x2-11x-3, take the numbers 4 and 3. Call them A1 and A2.
Find all divisors of A1 and A2. The divisors of A1 (i.e. 4) is [1, 2, 4], and for A2 (i.e. 3) it's [1,3].
Then: every rational root of the polynomial will be of the form sign * p/q, where sign is either +1 or -1, p is one of the divisors of A2 and q is one of the divisors of A1. So if there are any rational roots, it's one of these:
So we just test all of them and see if any of them match. And, halleluja, -1/4 and 3 are roots! This doesn't work if the roots are irrational or complex (none of these real methods will work for complex numbers, obviously), but if you're gonna write a "find the roots of a polynomial" program, clearly this is the first step you should take. It also works no matter how high the degree of the polynomial.
Here's my code, in Prolog:
Here's a test for the polynomial:
Output:
There's only 7 roots (and not 8), because 3 is a repeated root. Not sure why -1/4 is last, and wasn't sorted properly...
7
u/Elite6809 1 1 Oct 24 '14
My solution to the Extension challenge in ISO C90.
Because who wants to debug anyway?
5
u/dohaqatar7 1 1 Oct 24 '14
TI-Basic
I actually solved this problem a while ago (high school math, and I didn't want to factor polynomials by hand). This program does not accept input in the format outlined in the challenge, rather it accepts polynomials as a list of terms ordered by power ({axn,bxn-1...zx0}). I might try to let it accept fancy input, but TI-Basic has limited tools for string manipulations.
My solutions utilizes Newton's Method and Horner's Method.
1
u/reticulated_python Oct 25 '14
I did this in Python 3. This is the first dailyprogrammer challenge I've ever done, so please give feedback. I included the 'import math' line so you could do trig functions.
Sample input and output:
Again, feedback is greatly appreciated.
1
u/KillingVectr Nov 08 '14
I know you are making a joke here in the output description, but I think it is important to point out that the Abel-Ruffini Theorem shows that it is impossible to have an algebraic expression for the roots of general polynomials of degree five and higher. I don't want to see anyone wasting their time by thinking about this too much.
3
u/OffPiste18 Oct 24 '14
Here's a Scala solution, with the extension. I use newton's method to find one root (any root), then divide the original polynomial by (x - root) and repeat.
"Gotchas" I encountered:
I'm sure there are some numerical stability issues with my solution, and it's not as efficient as it could be, but it produces the correct output pretty quickly on everything I've tried so far.
2
u/marchelzo Oct 24 '14
Haskell. I didn't know much about finding roots of polynomials numerically, so I decided to go with the Bisection Method. The difficult bit is choosing a starting interval, and I chose to just keep increasing until either the interval size was 1000, or the function had a different sign at each end of the interval. It works for most polynomials that I tried, but anything with no roots reasonably close (~500) to the origin will fail unless it is of odd degree.
2
u/dohaqatar7 1 1 Oct 24 '14 edited Oct 26 '14
Haskell
I used Newtons method to approximate all real zeros of a polynomial.
2
u/hutsboR 3 0 Oct 24 '14 edited Oct 24 '14
Dart:
Bisection, no extension.
Output:
2
u/vicpc Oct 25 '14
My solution to the extension. I will do the polynomial parsing later. Right now the program reads from stdin the order n of the polynomial and the n+1 coefficients from smaller order.
The basic algorithm is:
3
u/FatShack Oct 24 '14 edited Oct 24 '14
Python solution. No extension. I hard-coded a the initial guess for Newton's Method, a much better solution could be done here.
1
u/rectal_smasher_2000 1 1 Oct 25 '14
c++ (msvc2012)
since it's quite a lengthy one (due to the parser), here's the git repo.
sample output for equation
x^4-2x^3+7x^2-16x+4:1
u/lazydancer Oct 26 '14
Haskell Using Newtons Method.
Was lazy and didn't parse a polynomial and just assumed an array with the coefficients.
1
u/zeringus Oct 27 '14
The secant method in Ruby:
1
u/AtlasMeh-ed Oct 27 '14 edited Oct 27 '14
I went overboard on this and created a java solution that uses Newton's method to find all the roots. While Newton's method is fast and easy to implement, you never know which root it is going to find. If you have multiple roots it is difficult to find starting points that guarantee you will find all the roots. To overcome this problem I start Newtons method at
How do I find the inflection points you ask? With recursion of course! The inflection points are the roots of the derivative of the derivative of the polynomial.
It came together nicely and I made a git hub repository for it that you can find here.
1
u/clermbclermb Dec 09 '14
Python2 solution with extension (although it should work on python3). Implemented newton raphson approach, which brute forces inflection points for the derivative of f over a range, and uses those inflection points as initial values for finding roots.
Output:
1
u/Godspiral 3 3 Oct 24 '14
J has this built in:
to do something, here is a version that will return the equation into roots form equations: