I’ve got a program that is supposed to find the roots for the **th hermite polynomial by using Newton’s method, but it’s taking a long time to run the program. I’m pretty new to C, so I can’t figure out where my bug is or if this is just the nature of brute forcing this problem. I’m also having issues getting accurate roots, but so far, it’s hard to find that bug because I can only run a test case every 5-10 minutes
CODE REMOVED
I’m 100% sure there’s no good reason for Newton-Raphson to take so much time. In some cases it may be problematic, because this method isn’t guaranteed to converge. But in your specific case – there should be no problem.
One thing that is clear is that you monstrously overuse recursion. Just calculating your
hermitewith n=37 is a recursion with complexity somewhat as summing 37 Fibonacci numbers, which is about 40 millions.Now, think that your
newtonmethod should invoke thehermiterepeatedly, as well ash_deriv(which is of the same order of magnitude of recursion), up until in converges up to10^-12. Sounds like tens of interations.And, not enough all this, you also manage to implement
newtonrecursively! There is really no reason in the world to so this. (was lisp/scheme your first programming language?)This is what you should do to improve the performance:
Fix your
hermite. You should calculate the 37 coefficients, this may be done recursively. Once this is done – you should use them to calculate the the value of the polynomial in a normal time.Same regarding the derivative. Just calculate the 36 coefficients.
Optionally fix your
newton. As far as I can see – you’ll not gain much of the performance: your “recursion” is an awkward loop nevertheless. However it’ll look better, and consume much less stack.Edit:
After reading the comments I took time and tried to build & run this. And, I must admit, I underestimated the problem complexity.
As turns out, the coefficients calculated by the recursive relation grow rapidly, and the round-off error seems to dominate. So that solving this problem by brute-force has unavoidable implications, and it’s not obvious that using the pre-calculated coefficients (and summing them in the straight order) yields the same result.
Nevertheless there’s a way to get rid of the ridiculous recursion without changing the calculation logic:
That is, we use the same recursive relation. It’s just in order to calculate the value of the Hermite polynomial at a given point we calculate it for all the polynomials up to n=37 iteratively, and store the results in the global array. Then its top element holds the needed result, and the derivative is also deduced from the 2nd array element from the end.
Since in Newton-Raphson algorithm at each step we need both the value and the derivative at the same point – this is done effectively.
P.S. However so far I could not come to the solution. The Newton-Raphson just doesn’t converge for the points I’ve tried to start from.
I believe for such a question a more robust method may be used, such as a median search.