Skip to content

Commit a03a16d

Browse files
Merge pull request #1601 from IntelPython/document-out-of-bound-sorter-in-searchsorter
Documented wrapping behavior of out of bound values of sorter
2 parents cb7f18b + dd36894 commit a03a16d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

dpctl/tensor/_searchsorted.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def searchsorted(
2222
*,
2323
side: Literal["left", "right"] = "left",
2424
sorter: Union[usm_ndarray, None] = None,
25-
):
25+
) -> usm_ndarray:
2626
"""searchsorted(x1, x2, side='left', sorter=None)
2727
2828
Finds the indices into `x1` such that, if the corresponding elements
@@ -50,6 +50,8 @@ def searchsorted(
5050
sorter (Optional[usm_ndarray]):
5151
array of indices that sort `x1` in ascending order. The array must
5252
have the same shape as `x1` and have an integral data type.
53+
Out of bound index values of `sorter` array are treated using
54+
`"wrap"` mode documented in :py:func:`dpctl.tensor.take`.
5355
Default: `None`.
5456
"""
5557
if not isinstance(x1, usm_ndarray):

dpctl/tests/test_usm_ndarray_searchsorted.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,18 @@ def test_pw_linear_interpolation_example():
340340
exp = dpt.vecdot(vals[1:] + vals[:-1], bins[1:] - bins[:-1]) / 2
341341

342342
assert dpt.abs(av - exp) < 0.1
343+
344+
345+
def test_out_of_bound_sorter_values():
346+
get_queue_or_skip()
347+
348+
x = dpt.asarray([1, 2, 0], dtype="i4")
349+
n = x.shape[0]
350+
351+
# use out-of-bounds indices in sorter
352+
sorter = dpt.asarray([2, 0 - n, 1 - n], dtype="i8")
353+
354+
x2 = dpt.arange(3, dtype=x.dtype)
355+
p = dpt.searchsorted(x, x2, sorter=sorter)
356+
# verify that they were applied with mode="wrap"
357+
assert dpt.all(p == dpt.arange(3, dtype=p.dtype))

0 commit comments

Comments
 (0)