Skip to content

Commit 7c09df3

Browse files
committed
feat: add isin to the specification
Closes: #854
1 parent 4ddeaca commit 7c09df3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

spec/draft/API_specification/set_functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Objects in API
1818
:toctree: generated
1919
:template: method.rst
2020

21+
isin
2122
unique_all
2223
unique_counts
2324
unique_inverse

src/array_api_stubs/_draft/set_functions.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
1-
__all__ = ["unique_all", "unique_counts", "unique_inverse", "unique_values"]
1+
__all__ = ["isin", "unique_all", "unique_counts", "unique_inverse", "unique_values"]
22

33

44
from ._types import Tuple, array
55

66

7+
def isin(
8+
x1: Union[array, int, float, complex, bool],
9+
x2: Union[array, int, float, complex, bool],
10+
/,
11+
*,
12+
invert: bool = False,
13+
) -> array:
14+
"""
15+
Tests whether each element in ``x1`` is in ``x2``.
16+
17+
Parameters
18+
----------
19+
x1: Union[array, int, float, complex, bool]
20+
first input array. **May** have any data type.
21+
x2: Union[array, int, float, complex, bool]
22+
second input array. **May** have any data type.
23+
invert: bool
24+
boolean indicating whether to invert the test criterion. If ``True``, the function **must** test whether each element in ``x1`` is *not* in ``x2``. If ``False``, the function **must** test whether each element in ``x1`` is in ``x2``. Default: ``False``.
25+
26+
Returns
27+
-------
28+
out: array
29+
an array containing element-wise test results. The returned array **must** have the same shape as ``x1`` and **must** have a boolean data type.
30+
31+
Notes
32+
-----
33+
34+
- At least one of ``x1`` or ``x2`` **must** be an array.
35+
36+
- If an element in ``x1`` is in ``x1``, the corresponding element in the output array **must** be ``True``; otherwise, the corresponding element in the output array **must** be ``False``.
37+
38+
- Testing whether an element in ``x1`` corresponds to an element in ``x2`` **should** be determined based on value equality (see :func:`~array_api.equal`). For input arrays having floating-point data types, value-based equality implies the following behavior.
39+
40+
- As ``nan`` values compare as ``False``, if an element in ``x1`` is ``nan`` and ``invert`` is ``False``, the corresponding element in the returned array **should** be ``False``. Otherwise, if an element in ``x1`` is ``nan`` and ``invert`` is ``True``, the corresponding element in the returned array **should** be ``True``.
41+
- As complex floating-point values having at least one ``nan`` component compare as ``False``, if an element in ``x1`` is a complex floating-point value having one or more ``nan`` components and ``invert`` is ``False``, the corresponding element in the returned array **should** be ``False``. Otherwise, if an element in ``x1`` is a complex floating-point value having one or more ``nan`` components and ``invert`` is ``True``, the corresponding element in the returned array **should** be ``True``.
42+
- As ``-0`` and ``+0`` compare as ``True``, if an element in ``x1`` is ``±0`` and ``x2`` contains at least one element which is ``±0``
43+
44+
- if ``invert`` is ``False``, the corresponding element in the returned array **should** be ``True``.
45+
- if ``invert`` is ``True``, the corresponding element in the returned array **should** be ``False``.
46+
47+
- Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is unspecified and thus implementation-defined.
48+
"""
49+
50+
751
def unique_all(x: array, /) -> Tuple[array, array, array, array]:
852
"""
953
Returns the unique elements of an input array ``x``, the first occurring indices for each unique element in ``x``, the indices from the set of unique elements that reconstruct ``x``, and the corresponding counts for each unique element in ``x``.

0 commit comments

Comments
 (0)