Skip to content

Commit 6a3e8d5

Browse files
committed
Implement dpnp.isin
1 parent 225b0db commit 6a3e8d5

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

dpnp/dpnp_iface_logic.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,95 @@ def isfortran(a):
11661166
return a.flags.fnc
11671167

11681168

1169+
def isin(element, test_elements, assume_unique=False, invert=False):
1170+
"""
1171+
Calculates ``element in test_elements``, broadcasting over `element` only.
1172+
Returns a boolean array of the same shape as `element` that is True
1173+
where an element of `element` is in `test_elements` and False otherwise.
1174+
1175+
Parameters
1176+
----------
1177+
element : {array_like, dpnp.ndarray, usm_ndarray}
1178+
Input array.
1179+
test_elements : {array_like, dpnp.ndarray, usm_ndarray}
1180+
The values against which to test each value of `element`.
1181+
This argument is flattened if it is an array or array_like.
1182+
See notes for behavior with non-array-like parameters.
1183+
assume_unique : bool, optional
1184+
Ignored
1185+
invert : bool, optional
1186+
If True, the values in the returned array are inverted, as if
1187+
calculating `element not in test_elements`. Default is False.
1188+
``dpnp.isin(a, b, invert=True)`` is equivalent to (but faster
1189+
than) ``dpnp.invert(dpnp.isin(a, b))``.
1190+
1191+
1192+
Returns
1193+
-------
1194+
isin : dpnp.ndarray of bool dtype
1195+
Has the same shape as `element`. The values `element[isin]`
1196+
are in `test_elements`.
1197+
1198+
1199+
Examples
1200+
--------
1201+
>>> import dpnp as np
1202+
>>> element = 2*np.arange(4).reshape((2, 2))
1203+
>>> element
1204+
array([[0, 2],
1205+
[4, 6]])
1206+
>>> test_elements = [1, 2, 4, 8]
1207+
>>> mask = np.isin(element, test_elements)
1208+
>>> mask
1209+
array([[False, True],
1210+
[ True, False]])
1211+
>>> element[mask]
1212+
array([2, 4])
1213+
1214+
The indices of the matched values can be obtained with `nonzero`:
1215+
1216+
>>> np.nonzero(mask)
1217+
(array([0, 1]), array([1, 0]))
1218+
1219+
The test can also be inverted:
1220+
1221+
>>> mask = np.isin(element, test_elements, invert=True)
1222+
>>> mask
1223+
array([[ True, False],
1224+
[False, True]])
1225+
>>> element[mask]
1226+
array([0, 6])
1227+
1228+
"""
1229+
1230+
dpnp.check_supported_arrays_type(element, test_elements, scalar_type=True)
1231+
if dpnp.isscalar(element):
1232+
usm_element = dpt.asarray(
1233+
element,
1234+
sycl_queue=test_elements.sycl_queue,
1235+
usm_type=test_elements.usm_type,
1236+
)
1237+
usm_test = dpnp.get_usm_ndarray(test_elements)
1238+
elif dpnp.isscalar(test_elements):
1239+
usm_test = dpt.asarray(
1240+
test_elements,
1241+
sycl_queue=element.sycl_queue,
1242+
usm_type=element.usm_type,
1243+
)
1244+
usm_element = dpnp.get_usm_ndarray(element)
1245+
else:
1246+
usm_element = dpnp.get_usm_ndarray(element)
1247+
usm_test = dpnp.get_usm_ndarray(test_elements)
1248+
return dpnp.get_result_array(
1249+
dpt.isin(
1250+
usm_element,
1251+
usm_test,
1252+
assume_unique=assume_unique,
1253+
invert=invert,
1254+
)
1255+
)
1256+
1257+
11691258
_ISINF_DOCSTRING = """
11701259
Tests each element :math:`x_i` of the input array `x` to determine if equal to
11711260
positive or negative infinity.

0 commit comments

Comments
 (0)