Skip to content

Commit b7b2901

Browse files
authored
Add an example for sorter keyword in dpnp.searchsorted (#2301)
The PR proposes to clarify documentation of `dpnp.searchsorted` and to add an example with `sorter` keyword.
1 parent f97b64c commit b7b2901

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

dpnp/dpnp_iface_searching.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ def argmax(a, axis=None, out=None, *, keepdims=False):
7676
axis : {None, int}, optional
7777
By default, the index is into the flattened array, otherwise along
7878
the specified axis.
79+
7980
Default: ``None``.
8081
out : {None, dpnp.ndarray, usm_ndarray}, optional
8182
If provided, the result will be inserted into this array. It should be
8283
of the appropriate shape and dtype.
84+
8385
Default: ``None``.
8486
keepdims : {None, bool}, optional
8587
If this is set to ``True``, the axes which are reduced are left in the
8688
result as dimensions with size one. With this option, the result will
8789
broadcast correctly against the array.
90+
8891
Default: ``False``.
8992
9093
Returns
@@ -165,15 +168,18 @@ def argmin(a, axis=None, out=None, *, keepdims=False):
165168
axis : {None, int}, optional
166169
By default, the index is into the flattened array, otherwise along
167170
the specified axis.
171+
168172
Default: ``None``.
169173
out : {None, dpnp.ndarray, usm_ndarray}, optional
170174
If provided, the result will be inserted into this array. It should be
171175
of the appropriate shape and dtype.
176+
172177
Default: ``None``.
173178
keepdims : {None, bool}, optional
174179
If this is set to ``True``, the axes which are reduced are left in the
175180
result as dimensions with size one. With this option, the result will
176181
broadcast correctly against the array.
182+
177183
Default: ``False``.
178184
179185
Returns
@@ -315,13 +321,15 @@ def searchsorted(a, v, side="left", sorter=None):
315321
side : {"left", "right"}, optional
316322
If ``"left"``, the index of the first suitable location found is given.
317323
If ``"right"``, return the last such index. If there is no suitable
318-
index, return either 0 or N (where N is the length of `a`).
324+
index, return either ``0`` or ``N`` (where ``N`` is the length of `a`).
325+
319326
Default: ``"left"``.
320-
sorter : {dpnp.ndarray, usm_ndarray}, optional
321-
Optional 1-D array of integer indices that sort array a into ascending
322-
order. They are typically the result of :obj:`dpnp.argsort`.
327+
sorter : {None, dpnp.ndarray, usm_ndarray}, optional
328+
Optional 1-D array of integer indices that sort array `a` into ascending
329+
order. They are typically the result of :py:func:`dpnp.argsort`.
323330
Out of bound index values of `sorter` array are treated using
324331
``"wrap"`` mode documented in :py:func:`dpnp.take`.
332+
325333
Default: ``None``.
326334
327335
Returns
@@ -338,7 +346,7 @@ def searchsorted(a, v, side="left", sorter=None):
338346
Examples
339347
--------
340348
>>> import dpnp as np
341-
>>> a = np.array([11,12,13,14,15])
349+
>>> a = np.array([11, 12, 13, 14, 15])
342350
>>> np.searchsorted(a, 13)
343351
array(2)
344352
>>> np.searchsorted(a, 13, side='right')
@@ -347,6 +355,19 @@ def searchsorted(a, v, side="left", sorter=None):
347355
>>> np.searchsorted(a, v)
348356
array([0, 5, 1, 2])
349357
358+
When `sorter` is used, the returned indices refer to the sorted
359+
array of `a` and not `a` itself:
360+
361+
>>> a = np.array([40, 10, 20, 30])
362+
>>> sorter = np.argsort(a)
363+
>>> sorter
364+
array([1, 2, 3, 0]) # Indices that would sort the array 'a'
365+
>>> result = np.searchsorted(a, 25, sorter=sorter)
366+
>>> result
367+
array(2)
368+
>>> a[sorter[result]]
369+
array(30) # The element at index 2 of the sorted array is 30
370+
350371
"""
351372

352373
usm_a = dpnp.get_usm_ndarray(a)
@@ -374,16 +395,20 @@ def where(condition, x=None, y=None, /, *, order="K", out=None):
374395
----------
375396
condition : {dpnp.ndarray, usm_ndarray}
376397
When ``True``, yield `x`, otherwise yield `y`.
377-
x, y : {dpnp.ndarray, usm_ndarray, scalar}, optional
398+
x, y : {None, dpnp.ndarray, usm_ndarray, scalar}, optional
378399
Values from which to choose. `x`, `y` and `condition` need to be
379400
broadcastable to some shape.
401+
402+
Default: ``None``.
380403
order : {"K", "C", "F", "A"}, optional
381404
Memory layout of the new output array, if keyword `out` is ``None``.
405+
382406
Default: ``"K"``.
383407
out : {None, dpnp.ndarray, usm_ndarray}, optional
384408
The array into which the result is written. The data type of `out` must
385409
match the expected shape and the expected data type of the result.
386410
If ``None`` then a new array is returned.
411+
387412
Default: ``None``.
388413
389414
Returns

0 commit comments

Comments
 (0)