[SciPy-Dev] Comments on optimize.newton function

Charles R Harris charlesr.harris at gmail.com
Sat May 21 19:25:19 EDT 2011


On Sat, May 21, 2011 at 4:24 PM, Gökhan Sever <gokhansever at gmail.com> wrote:

> Hello,
>
> Could we add "tolf" argument into the newton function signature?
>
> I am guessing this should match the tolf argument in IDL newton (
> http://star.pst.qub.ac.uk/idl/NEWTON.html)
>
> TOLF: Set the convergence criterion on the function values. The default
> value is 1.0 x 10-4.
>
>
> def newton(func, x0, fprime=None, args=(), tol=1.48e-8, maxiter=50, tolf=
> 1.e-4)
>
> In second method part of the newton function (from
> https://github.com/scipy/scipy/blob/master/scipy/optimize/zeros.py)
>
>
>         # Secant method
>
>         p0 = x0
>
>         if x0 >= 0:
>
>             p1 = x0*(1 + 1e-4) + 1e-4
>
>         else:
>
>             p1 = x0*(1 + 1e-4) - 1e-4
>
> without increasing the tolf or (1e-4) in these statements I can't get a proper root solution for my function.
>
>
>
> The reason that I am experiencing with newton is because fsolve seems slower comparing to the newton for scalar root finding for a given function.
>
> Consider this example run Sage v4.6.1 notebook:
>
>
> %cython
> cpdef double myfunc(double x):
>     return x**3 + 2*x - 1
>
>
> timeit('scipy.optimize.newton(myfunc,1)')
>
> 625 loops, best of 3: 22.1 µs per loop
>
>
> timeit('scipy.optimize.fsolve(myfunc,1)')
>
> 625 loops, best of 3: 86.5 µs per loop
>
>
> I am also experimenting to Cythonize the Newton secant method, which the
> Cython written version shows significant speed-ups comparing to the Python
> version. Without going any further, I would like to know if there is any
> Cythonized code around for newton or any other approach to make fsolve
> faster?
>
> Thanks.
>
>
>
You could probably adapt one of the other 1d zero finders, say ritter, just
ignore all the fancy stuff for the bounding interval and such.

I don't much like the stopping criterion in newton either and ftol would
probably help, but it might be worth thinking about overstepping and looking
for a sign change. Or something like that that would give more assurance
that a zero was at hand. Note that there is also a pull request for using
the second derivative as well as the first.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20110521/9fcee863/attachment.html>


More information about the SciPy-Dev mailing list