Skip to content

Commit a980c7f

Browse files
committed
Implement dpnp.isin
1 parent 131c490 commit a980c7f

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

dpnp/dpnp_iface_logic.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"iscomplexobj",
7070
"isfinite",
7171
"isfortran",
72+
"isin",
7273
"isinf",
7374
"isnan",
7475
"isneginf",
@@ -1196,6 +1197,95 @@ def isfortran(a):
11961197
return a.flags.fnc
11971198

11981199

1200+
def isin(element, test_elements, assume_unique=False, invert=False):
1201+
"""
1202+
Calculates ``element in test_elements``, broadcasting over `element` only.
1203+
Returns a boolean array of the same shape as `element` that is True
1204+
where an element of `element` is in `test_elements` and False otherwise.
1205+
1206+
Parameters
1207+
----------
1208+
element : {array_like, dpnp.ndarray, usm_ndarray}
1209+
Input array.
1210+
test_elements : {array_like, dpnp.ndarray, usm_ndarray}
1211+
The values against which to test each value of `element`.
1212+
This argument is flattened if it is an array or array_like.
1213+
See notes for behavior with non-array-like parameters.
1214+
assume_unique : bool, optional
1215+
Ignored
1216+
invert : bool, optional
1217+
If True, the values in the returned array are inverted, as if
1218+
calculating `element not in test_elements`. Default is False.
1219+
``dpnp.isin(a, b, invert=True)`` is equivalent to (but faster
1220+
than) ``dpnp.invert(dpnp.isin(a, b))``.
1221+
1222+
1223+
Returns
1224+
-------
1225+
isin : dpnp.ndarray of bool dtype
1226+
Has the same shape as `element`. The values `element[isin]`
1227+
are in `test_elements`.
1228+
1229+
1230+
Examples
1231+
--------
1232+
>>> import dpnp as np
1233+
>>> element = 2*np.arange(4).reshape((2, 2))
1234+
>>> element
1235+
array([[0, 2],
1236+
[4, 6]])
1237+
>>> test_elements = [1, 2, 4, 8]
1238+
>>> mask = np.isin(element, test_elements)
1239+
>>> mask
1240+
array([[False, True],
1241+
[ True, False]])
1242+
>>> element[mask]
1243+
array([2, 4])
1244+
1245+
The indices of the matched values can be obtained with `nonzero`:
1246+
1247+
>>> np.nonzero(mask)
1248+
(array([0, 1]), array([1, 0]))
1249+
1250+
The test can also be inverted:
1251+
1252+
>>> mask = np.isin(element, test_elements, invert=True)
1253+
>>> mask
1254+
array([[ True, False],
1255+
[False, True]])
1256+
>>> element[mask]
1257+
array([0, 6])
1258+
1259+
"""
1260+
1261+
dpnp.check_supported_arrays_type(element, test_elements, scalar_type=True)
1262+
if dpnp.isscalar(element):
1263+
usm_element = dpt.asarray(
1264+
element,
1265+
sycl_queue=test_elements.sycl_queue,
1266+
usm_type=test_elements.usm_type,
1267+
)
1268+
usm_test = dpnp.get_usm_ndarray(test_elements)
1269+
elif dpnp.isscalar(test_elements):
1270+
usm_test = dpt.asarray(
1271+
test_elements,
1272+
sycl_queue=element.sycl_queue,
1273+
usm_type=element.usm_type,
1274+
)
1275+
usm_element = dpnp.get_usm_ndarray(element)
1276+
else:
1277+
usm_element = dpnp.get_usm_ndarray(element)
1278+
usm_test = dpnp.get_usm_ndarray(test_elements)
1279+
return dpnp.get_result_array(
1280+
dpt.isin(
1281+
usm_element,
1282+
usm_test,
1283+
assume_unique=assume_unique,
1284+
invert=invert,
1285+
)
1286+
)
1287+
1288+
11991289
_ISINF_DOCSTRING = """
12001290
Tests each element :math:`x_i` of the input array `x` to determine if equal to
12011291
positive or negative infinity.

0 commit comments

Comments
 (0)