[SciPy-User] find zero
Alan G Isaac
alan.isaac at gmail.com
Fri May 30 08:15:33 EDT 2014
On 5/30/2014 5:35 AM, nicky van foreest wrote:
> I am trying to find a zero in an array V that is positive at the left some index i, say, and negative at the right of i.
The insertion point is
1+np.argmax(np.diff(np.sign(a)) != 0)
but a simple bisection algorithm would be much faster for large arrays.
Without testing::
def bisect(a, i1, i2):
if a[i1]*a[i2] > 0:
raise ValueError("Sign changing interval required.")
while abs(i1-i2) > 1:
midpt = (i1+i2) // 2
if a[midpt]*a[i1] > 0:
i1 = midpt
else:
i2 = midpt
return max(i1,i2)
fwiw,
Alan Isaac
More information about the SciPy-User
mailing list