# [SciPy-User] Vectorized newton function

Anne Archibald peridot.faceted@gmail....
Wed Apr 14 18:23:21 CDT 2010

```On 14 April 2010 18:37, Gökhan Sever <gokhansever@gmail.com> wrote:
> Hello,
>
> How can I make scipy.optimize.newton function accepting a numpy.array as its
> x0 parameter?

The short answer is, you can't. Just use a loop.

The basic problem is that this function is a univariate optimizer, so
that it adjusts the scalar x0 as needed to (try to) find a zero of
func. If func is a multivariate function, you need a multivariate root
finder like fsolve. If what you want is instead to run one-dimensional
root-finding with many different starting points, well, I'll first
point out that the dependence of the result of newton on the starting
point is complicated (in fact fractal) and nasty, so you're going to
get weird hard-to-predict results unless you carefully start each x0
near a zero. If at all possible I recommend using something like
brentq/brenth that takes a "bracket" instead of a single point. That
said, if you're looking for lots of roots, there's not really any way
around looping in python to call newton many times, but this isn't
your efficiency problem anyway: the python function you provide as
"func" will be called many times for each call of newton, and that's
where the python overhead will matter. If your goal is just to avoid
writing a for loop, you can consider using vectorize() to create the
python loop for you.

You could argue that newton should allow "parallel" root-finding by
passing an array of xs into func and the derivative and getting an
array back, but since newton is an adaptive method, this would be very
messy code.

Anne

> File:             /usr/lib/python2.6/site-packages/scipy/optimize/minpack.py
> Definition:       newton(func, x0, fprime=None, args=(), tol=1.48e-08,
> maxiter=50)
>
> x0 : float
>         An initial estimate of the zero that should be somewhere near the
>         actual zero.
>
> (Without using a for-loop)
>
> Thanks.
>
> --
> Gökhan
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
```