[SciPy-user] Bug in optimize.newton

Martin Wiechert martin.wiechert at gmx.de
Wed Aug 4 04:01:05 CDT 2004


Hi,

I've been bitten by a bug in the snippet from optimize.newton below 
(yesterday's CVS).

I think, the actual bug is in line 317, which should read

            if abs(p-p1) < tol:

instead of

            if abs(p-p0) < tol:

This will typically only cause one superfluous iteration, which is no problem.
But consider the following situation:
alter_numeric () has been called.
So, if one passes, say, a Float32 0d array as initial value, this will coerce 
all other scalars into Float32 0d arrays. So if you happen to get identical 
p's for two consecutive iterations, this will go unnoticed, because the 
ZeroDivisionError is not raised (rather the next p is nan), and line 317 
doesn't compare the right p's.


    else: # Secant method
        p0 = x0
        p1 = x0*(1+1e-4)
        q0 = apply(func,(p0,)+args)
        q1 = apply(func,(p1,)+args)
        for iter in range(maxiter):
            try:                
                p = p1 - q1*(p1-p0)/(q1-q0)
            except ZeroDivisionError:
                if p1 != p0:
                    print "Tolerance of %g reached" % (p1-p0)
                return (p1+p0)/2.0
            if abs(p-p0) < tol:
                return p
            p0 = p1
            q0 = q1
            p1 = p
            q1 = apply(func,(p1,)+args)

Regards,
Martin Wiechert.



More information about the SciPy-user mailing list