-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorderedTableSearch.py
More file actions
222 lines (156 loc) · 5.55 KB
/
orderedTableSearch.py
File metadata and controls
222 lines (156 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# coding: utf-8
# In[1]:
import numpy as np
import numba as nb
# In[2]:
@nb.jit(nopython=True)
def locate(x, grid, btm = 0, up = None):
if up is None:
up = len(grid)-1
"""
from Numerical Repipes 2nd ed.
input
x: a value to be evaluated
grid: a monotonically ordered grid
return grid number 0,1,....,len(grid) -2
"""
N = len(grid)
if grid[N-1] > grid[0]:
if x <= grid[0]:
return 0
elif x >= grid[N-1]:
return N-2
elif grid[N-1] < grid[0]:
if x <= grid[N-2]:
return N-2
elif x >= grid[0]:
return 0
else:
raise Exception('locate: the table does not look ordered or has NaNs')
# print('error: grid[N-1] == grid[0]')
#Golden search
mid = 0
while up - btm > 1: #if not done
mid = int((up+btm)/2) #math.floor?
# if (grid[N-1] > grid[0]) == (x > grid[mid]):
if (grid[N-1] > grid[0]) == (x > grid[mid]):
btm = mid
else:
up = mid
if up - btm < 1:
raise Exception('locate: error: up - btm < 1')
# print('locate, error: up - btm < 1, up = ', up, ', btm = ', btm, '.')
# print('error: up - btm < 1')
return btm
@nb.jit(nopython=True)
def hunt(x, grid, init_btm):
N = len(grid)
ascnd = (grid[N-1] > grid[0])
btm = init_btm
up = 0#None does not work for numba
if init_btm < 0 or init_btm > N-2:
return locate(x,grid)
else:
inc = 1 #increment
if (x > grid[btm]) is ascnd:
while True:
up = btm + inc
#print('up: ',up)
if up > N-2:
up = N-1
#print('up: ',up)
break
elif (x > grid[up]) is ascnd:
btm = up
inc = inc + inc
else:
break
else:
up = btm
#print('up: ',up)
while True:
btm = up - inc
#print('btm: ',btm)
if btm < 0:
btm = 0
#print('btm: ',btm)
break
elif (x < grid[btm]) is ascnd:
up = btm
inc = inc + inc
#print('up: ',up)
else:
break
#print('btm: ',btm)
#print('up: ',up)
return locate(x, grid, btm, up)
#@nb.jit#(nopython=True)#hasattr is not compatible with numba
#this should accept an array-like with len == 1
@nb.jit(nopython=True)
def locate_on_grids(xvals, grid, init_btm = 0):
M = len(xvals)
ans = np.zeros(M, dtype = np.int64)
ans[0]= hunt(xvals[0], grid, init_btm)
for ix in range(1,M):
ans[ix] = hunt(xvals[ix], grid, ans[ix-1])
# ans[ix] = locate(xvals[ix], grid, ans[ix-1]) #this was a typo. cant set btm = ans[ix-1]
# i donno why but it is faster
return ans
# # @nb.generated_jit(nopython=True)
# @nb.jit(nopython=True)
# def locate_grid(xvals, grid, init_btm = 0, return_nparray = False):
# if isinstance(xvals, nb.types.Float) or isinstance(xvals, nb.types.Integer) : #if xvals is scalar
# #here, locate is converted into np.array
# #if you need just an interger, use locate instead.
# return lambda xvals, grid, init_btm, return_nparray: np.array(locate(xvals, grid))
# #if return_nparray is True:
# # return lambda xvals, grid, init_btm, return_nparray: np.array(locate(xvals, grid))
# #else:
# # return lambda xvals, grid, init_btm, return_nparray: locate(xvals, grid)
# else: #arraylike #maybe I should check this is arraylike
# return lambda xvals, grid, init_btm, return_nparray: locate_on_grids(xvals, grid, init_btm)
# @nb.generated_jit(nopython=True)
@nb.jit(nopython=True)
def locate_grid(xvals, grid, init_btm = 0, return_nparray = False):
if isinstance(xvals, float) or isinstance(xvals, int) : #if xvals is scalar
#here, locate is converted into np.array
#if you need just an interger, use locate instead.
return np.array(locate(xvals, grid))
#if return_nparray is True:
# return lambda xvals, grid, init_btm, return_nparray: np.array(locate(xvals, grid))
#else:
# return lambda xvals, grid, init_btm, return_nparray: locate(xvals, grid)
else: #arraylike #maybe I should check this is arraylike
return locate_on_grids(xvals, grid, init_btm)
# In[4]:
if __name__ == '__main__':
import time
bignodes = np.linspace(-10, 100, 1000000)
xvals = np.linspace(-1, 50, 1000000)
t1 = time.time()
hunt(6.6, bignodes, 3)
t2 = time.time()
print(' {} seconds'.format(t2 - t1))
#comapred the speed
t1 = time.time()
M = len(xvals)
ans1 = np.zeros(M, dtype = np.int64)
for ix, x in enumerate(xvals):
ans1[ix] = locate(x, bignodes)
t2 = time.time()
print(' {} seconds'.format(t2 - t1))
t1 = time.time()
M = len(xvals)
ans2 = np.zeros(M, dtype = np.int64)
ans2[0] = hunt(x, bignodes, 0)
for ix in range(M):
x = xvals[ix]
ans2[ix] = hunt(x, bignodes, ans2[ix-1]+1)
t2 = time.time()
print(' {} seconds'.format(t2 - t1))
#comapred the speed
t1 = time.time()
ans3 = locate_grid(xvals, bignodes)
t2 = time.time()
print(' {} seconds'.format(t2 - t1))
# In[ ]: