Skip to content

Commit

Permalink
Made find_last_before and find_first_after faster
Browse files Browse the repository at this point in the history
Used np.searchsorted per Tom Aldcroft's suggestion
  • Loading branch information
unknown authored and unknown committed Oct 18, 2013
1 parent 16fd561 commit a71a880
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,16 @@ def find_last_before(a, b):
array b that are closest without going over the values of array a.
(Bob Barker rules. Assumes b is sorted by magnitude.)
"""
a = np.atleast_1d(np.array(a))
b = np.atleast_1d(np.array(b))
out = np.zeros(len(a), dtype='int32')
for i in range(len(a)):
out_i = np.argmin(abs(b - a[i]))
if b[out_i] > a[i]:
out_i = out_i - 1
out[i] = out_i
out = np.searchsorted(b,a,side='left') - 1
out[out==-1] = 0
return out

def find_first_after(a, b):
"""This function returns an array of length a with the indices of
array b that are closest without being less than the values of array a.
(Opposite of Bob Barker rules. Assumes b is sorted by magnitude.)
"""
a = np.atleast_1d(np.array(a))
b = np.atleast_1d(np.array(b))
out = np.zeros(len(a), dtype='int32')
for i in range(len(a)):
out_i = np.argmin(abs(b - a[i]))
if b[out_i] < a[i]:
out_i = out_i + 1
out[i] = out_i
out = np.searchsorted(b,a,side='right')
return out

def append_rss(A):
Expand Down

0 comments on commit a71a880

Please sign in to comment.