-
Notifications
You must be signed in to change notification settings - Fork 23
Implement dpnp.isin
#2595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Implement dpnp.isin
#2595
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -65,6 +65,7 @@ | |||||||||||||||||||||||||||||||||||||||||
"iscomplexobj", | ||||||||||||||||||||||||||||||||||||||||||
"isfinite", | ||||||||||||||||||||||||||||||||||||||||||
"isfortran", | ||||||||||||||||||||||||||||||||||||||||||
"isin", | ||||||||||||||||||||||||||||||||||||||||||
"isinf", | ||||||||||||||||||||||||||||||||||||||||||
"isnan", | ||||||||||||||||||||||||||||||||||||||||||
"isneginf", | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -1115,6 +1116,95 @@ def isfortran(a): | |||||||||||||||||||||||||||||||||||||||||
return a.flags.fnc | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
def isin(element, test_elements, assume_unique=False, invert=False): | ||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||
Calculates ``element in test_elements``, broadcasting over `element` only. | ||||||||||||||||||||||||||||||||||||||||||
Returns a boolean array of the same shape as `element` that is True | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
where an element of `element` is in `test_elements` and False otherwise. | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
Parameters | ||||||||||||||||||||||||||||||||||||||||||
---------- | ||||||||||||||||||||||||||||||||||||||||||
element : {array_like, dpnp.ndarray, usm_ndarray} | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
Input array. | ||||||||||||||||||||||||||||||||||||||||||
test_elements : {array_like, dpnp.ndarray, usm_ndarray} | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
The values against which to test each value of `element`. | ||||||||||||||||||||||||||||||||||||||||||
This argument is flattened if it is an array or array_like. | ||||||||||||||||||||||||||||||||||||||||||
See notes for behavior with non-array-like parameters. | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1131
to
+1132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no notes provided below, so the reference on it can be removed
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
assume_unique : bool, optional | ||||||||||||||||||||||||||||||||||||||||||
Ignored | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be great to add more words |
||||||||||||||||||||||||||||||||||||||||||
invert : bool, optional | ||||||||||||||||||||||||||||||||||||||||||
If True, the values in the returned array are inverted, as if | ||||||||||||||||||||||||||||||||||||||||||
calculating `element not in test_elements`. Default is False. | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1136
to
+1137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
``dpnp.isin(a, b, invert=True)`` is equivalent to (but faster | ||||||||||||||||||||||||||||||||||||||||||
than) ``dpnp.invert(dpnp.isin(a, b))``. | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
Returns | ||||||||||||||||||||||||||||||||||||||||||
------- | ||||||||||||||||||||||||||||||||||||||||||
isin : dpnp.ndarray of bool dtype | ||||||||||||||||||||||||||||||||||||||||||
Has the same shape as `element`. The values `element[isin]` | ||||||||||||||||||||||||||||||||||||||||||
are in `test_elements`. | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
Examples | ||||||||||||||||||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||||||||||||||||||
>>> import dpnp as np | ||||||||||||||||||||||||||||||||||||||||||
>>> element = 2*np.arange(4).reshape((2, 2)) | ||||||||||||||||||||||||||||||||||||||||||
>>> element | ||||||||||||||||||||||||||||||||||||||||||
array([[0, 2], | ||||||||||||||||||||||||||||||||||||||||||
[4, 6]]) | ||||||||||||||||||||||||||||||||||||||||||
>>> test_elements = [1, 2, 4, 8] | ||||||||||||||||||||||||||||||||||||||||||
>>> mask = np.isin(element, test_elements) | ||||||||||||||||||||||||||||||||||||||||||
>>> mask | ||||||||||||||||||||||||||||||||||||||||||
array([[False, True], | ||||||||||||||||||||||||||||||||||||||||||
[ True, False]]) | ||||||||||||||||||||||||||||||||||||||||||
>>> element[mask] | ||||||||||||||||||||||||||||||||||||||||||
array([2, 4]) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
The indices of the matched values can be obtained with `nonzero`: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
>>> np.nonzero(mask) | ||||||||||||||||||||||||||||||||||||||||||
(array([0, 1]), array([1, 0])) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
The test can also be inverted: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
>>> mask = np.isin(element, test_elements, invert=True) | ||||||||||||||||||||||||||||||||||||||||||
>>> mask | ||||||||||||||||||||||||||||||||||||||||||
array([[ True, False], | ||||||||||||||||||||||||||||||||||||||||||
[False, True]]) | ||||||||||||||||||||||||||||||||||||||||||
>>> element[mask] | ||||||||||||||||||||||||||||||||||||||||||
array([0, 6]) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
dpnp.check_supported_arrays_type(element, test_elements, scalar_type=True) | ||||||||||||||||||||||||||||||||||||||||||
if dpnp.isscalar(element): | ||||||||||||||||||||||||||||||||||||||||||
usm_element = dpt.asarray( | ||||||||||||||||||||||||||||||||||||||||||
element, | ||||||||||||||||||||||||||||||||||||||||||
sycl_queue=test_elements.sycl_queue, | ||||||||||||||||||||||||||||||||||||||||||
usm_type=test_elements.usm_type, | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
usm_test = dpnp.get_usm_ndarray(test_elements) | ||||||||||||||||||||||||||||||||||||||||||
elif dpnp.isscalar(test_elements): | ||||||||||||||||||||||||||||||||||||||||||
usm_test = dpt.asarray( | ||||||||||||||||||||||||||||||||||||||||||
test_elements, | ||||||||||||||||||||||||||||||||||||||||||
sycl_queue=element.sycl_queue, | ||||||||||||||||||||||||||||||||||||||||||
usm_type=element.usm_type, | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
usm_element = dpnp.get_usm_ndarray(element) | ||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||
usm_element = dpnp.get_usm_ndarray(element) | ||||||||||||||||||||||||||||||||||||||||||
usm_test = dpnp.get_usm_ndarray(test_elements) | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1181
to
+1197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
return dpnp.get_result_array( | ||||||||||||||||||||||||||||||||||||||||||
dpt.isin( | ||||||||||||||||||||||||||||||||||||||||||
usm_element, | ||||||||||||||||||||||||||||||||||||||||||
usm_test, | ||||||||||||||||||||||||||||||||||||||||||
assume_unique=assume_unique, | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dpctl does not have that keyword
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
invert=invert, | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
_ISINF_DOCSTRING = """ | ||||||||||||||||||||||||||||||||||||||||||
Tests each element :math:`x_i` of the input array `x` to determine if equal to | ||||||||||||||||||||||||||||||||||||||||||
positive or negative infinity. | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NumPy supports also
kind
keyword, but we can limit it with onlyNone
and'sort'
values and state a limitation that'table'
is not supported