Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to solve long polynomial with lots of different powers

I'm looking for the fastest solution, x, to this polynomial equation:

Let m be an element in set M.

sum over all m {a_m * x^(b_m) - c_m * x^(b_m - 1)} = 0, where a_m, b_m, c_m are all different for each m. The set M has ~15-20 elements.

If the solution is > 4, it will return 4. If the solution is < 0, it will return 0. What is the fastest way to do this? Doing it numerically?

I would prefer a solution in python, and other languages only if it's very beneficial to switch.

Note this is the derivative of an objective function. I am just trying to maximize the objective function, so if there's a better way to do it aside from solving this polynomial, that would work too! The solution should be fairly fast, as I am trying to solve many of these objective functions.

like image 603
Popcorn Avatar asked Nov 05 '22 09:11

Popcorn


1 Answers

If you're only looking for one root and not all roots, you can use Newton's Method, which I expect is reasonably fast for the polynomials you've described.

let f(x) = sum over all m {a*x^(b) - c*x^(b-1)}

then f'(x), the derivative of f(x), is the sum over all m {(a*b)*x^(b-1) - (c*(b-1))*x^(b-2)}.

def newton(f, fprime, firstguess, epsilon):
    x = firstguess
    while abs(f(x)) > epsilon:
        x = x - (f(x) / fprime(x))
    return x

This will return an approximate root to your polynomial. If it's not accurate enough, pass in a smaller epsilon until it is accurate enough.

Note that this function may diverge, and run forever, or throw a ZeroDivisionError. Handle with caution.

like image 81
Kevin Avatar answered Nov 09 '22 07:11

Kevin