Author: Matti Picus <[email protected]>
Branch: numpy-searchsorted
Changeset: r70754:3ec459c1d040
Date: 2014-04-18 15:34 +0300
http://bitbucket.org/pypy/pypy/changeset/3ec459c1d040/
Log: do error checking, add app-level searchsort, which needs getitem().
diff --git a/pypy/module/micronumpy/app_numpy.py
b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -22,3 +22,34 @@
arr[j] = i
i += step
return arr
+
+# How to call this from descr_searchsorted??
+def searchsort(space, arr, v, side, result):
+ def left_find_index(a, val):
+ imin = 0
+ imax = a.size
+ while imin < imax:
+ imid = imin + ((imax - imin) >> 1)
+ if a[imid] <= val:
+ imin = imid +1
+ else:
+ imax = imid
+ return imin
+ def right_find_index(a, val):
+ imin = 0
+ imax = a.size
+ while imin < imax:
+ imid = imin + ((imax - imin) >> 1)
+ if a[imid] < val:
+ imin = imid +1
+ else:
+ imax = imid
+ return imin
+ if side == 'l':
+ func = left_find_index
+ else:
+ func = right_find_index
+ for i in range(v.get_size()):
+ result[i] = func(self, v[i])
+ return result
+
diff --git a/pypy/module/micronumpy/ndarray.py
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -693,9 +693,32 @@
loop.round(space, self, calc_dtype, self.get_shape(), decimals, out)
return out
- def descr_searchsorted(self, space, w_v, w_side='left'):
- raise OperationError(space.w_NotImplementedError, space.wrap(
- "searchsorted not implemented yet"))
+ @unwrap_spec(side=str, w_sorter=WrappedDefault(None))
+ def descr_searchsorted(self, space, w_v, side='left', w_sorter=None):
+ from pypy.module.micronumpy.sort import searchsort
+ if not space.is_none(w_sorter):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ 'sorter not supported in searchsort'))
+ if not side or len(side) < 1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "expected nonempty string for keyword 'side'"))
+ elif side[0] == 'l' or side[0] == 'L':
+ side = 'l'
+ elif side[0] == 'r' or side[0] == 'R':
+ side = 'r'
+ else:
+ raise oefmt(space.w_ValueError,
+ "'%s' is an invalid value for keyword 'side'", side)
+ ret = W_NDimArray.from_shape(space, self.get_shape(),
+ descriptor.get_dtype_cache(space).w_longdtype)
+ if len(self.get_shape()) > 1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "a must be a 1-d array"))
+ v = convert_to_array(space, w_v)
+ if len(v.get_shape()) >1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "v must be a 1-d array-like"))
+ return searchsort(self, space, v, side, ret)
def descr_setasflat(self, space, w_v):
raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -1351,6 +1374,7 @@
dot = interp2app(W_NDimArray.descr_dot),
var = interp2app(W_NDimArray.descr_var),
std = interp2app(W_NDimArray.descr_std),
+ searchsorted = interp2app(W_NDimArray.descr_searchsorted),
cumsum = interp2app(W_NDimArray.descr_cumsum),
cumprod = interp2app(W_NDimArray.descr_cumprod),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit