Skip to content

Commit 0498721

Browse files
kgryteev-br
andauthored
feat: add support for integer array indexing
PR-URL: #900 Closes: #669 Co-authored-by: Evgeni Burovski <[email protected]> Reviewed-by: Evgeni Burovski <[email protected]> Reviewed-by: Stephan Hoyer Reviewed-by: Sebastian Berg Reviewed-by: Aaron Meurer
1 parent 287b834 commit 0498721

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

spec/draft/API_specification/indexing.rst

+45
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Indexing
77

88
A conforming implementation of the array API standard must adhere to the following conventions.
99

10+
11+
.. _indexing-single-axis:
12+
1013
Single-axis Indexing
1114
--------------------
1215

@@ -121,6 +124,9 @@ The behavior outside of these bounds is unspecified.
121124
.. note::
122125
*Rationale: this is consistent with bounds checking for integer indexing; the behavior of out-of-bounds indices is left unspecified. Implementations may choose to clip (consistent with Python* ``list`` *slicing semantics), raise an exception, return junk values, or some other behavior depending on device requirements and performance considerations.*
123126

127+
128+
.. _indexing-multi-axis:
129+
124130
Multi-axis Indexing
125131
-------------------
126132

@@ -173,6 +179,45 @@ Multi-dimensional arrays must extend the concept of single-axis indexing to mult
173179

174180
*Rationale: this is consistent with bounds-checking for single-axis indexing. An implementation may choose to set the axis (dimension) size of the result array to* ``0`` *, raise an exception, return junk values, or some other behavior depending on device requirements and performance considerations.*
175181

182+
Integer Array Indexing
183+
----------------------
184+
185+
.. note::
186+
Integer array indexing, as described in this specification, is a reduced subset of "vectorized indexing" semantics, as implemented in libraries such as NumPy. In vectorized indexing, integers and integer arrays are broadcasted to integer arrays having a common shape before being "zipped" together to form a list of index coordinates. This form of indexing diverges from the multi-axis indexing semantics described above (see :ref:`indexing-multi-axis`) where each element of an indexing tuple comprised of integers and slices independently indexes a particular axis. This latter form of indexing is commonly referred to as "orthogonal indexing" and is the default form of indexing outside of Python in languages such as Julia and MATLAB.
187+
188+
An array must support indexing by an indexing tuple which contains only integers and integer arrays according to the following rules. Let ``A`` be an ``N``-dimensional array with shape ``S1``. Let ``T`` be a tuple ``(t1, t2, ..., tN)`` having length ``N``. Let ``tk`` be an individual element of ``T``.
189+
190+
.. note::
191+
This specification does not currently address indexing tuples which combine slices and integer arrays. Behavior for such indexing tuples is left unspecified and thus implementation-defined. This may be revisited in a future revision of this standard.
192+
193+
.. note::
194+
This specification does not currently address indexing tuples which include array-like elements, such as Python lists, tuples, and other sequences. Behavior when indexing an array using array-like elements is left unspecified and thus implementation-defined.
195+
196+
- If ``tk`` is an integer array, ``tk`` should have the default array index data type (see :ref:`data-type-defaults`).
197+
198+
.. note::
199+
Conforming implementations of this standard may support integer arrays having other integer data types; however, consumers of this standard should be aware that integer arrays having uncommon array index data types such as ``int8`` and ``uint8`` may not be widely supported as index arrays across conforming array libraries. To dynamically resolve the default array index data type, including for that of the current device context, use the inspection API ``default_dtypes()``.
200+
201+
- Providing a zero-dimensional integer array ``tk`` containing an integer index must be equivalent to providing an integer index having the value ``int(tk)``. Conversely, each integer index ``tk`` must be equivalent to a zero-dimensional integer array containing the same value and be treated as such, including shape inference and broadcasting. Accordingly, if ``T`` consists of only integers and zero-dimensional integer arrays, the result must be equivalent to indexing multiple axes using integer indices. For example, if ``A`` is a two-dimensional array, ``T`` is the tuple ``(i, J)``, ``i`` is a valid integer index, and ``J`` is a zero-dimensional array containing a valid integer index ``j``, the result of ``A[T]`` must be equivalent to ``A[(i,j)]`` (see :ref:`indexing-multi-axis`).
202+
203+
- If ``tk`` is an integer array, each element in ``tk`` must independently satisfy the rules stated above for indexing a single-axis with an integer index (see :ref:`indexing-single-axis`).
204+
205+
.. note::
206+
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
207+
208+
- If ``tk`` is an integer array containing duplicate valid integer indices, the result must include the corresponding elements of ``A`` with the same duplication.
209+
210+
..
211+
TODO: once setitem semantics are determined, insert the following note: Given the assignment operation ``x[T] = y[...]``, if ``T`` contains an integer array having duplicate indices, the order in which elements in ``y`` are assigned to the corresponding element(s) in ``x`` is unspecified and thus implementation-defined.
212+
213+
- If ``T`` contains at least one non-zero-dimensional integer array, all elements of ``T`` must be broadcast against each other to determine a common shape ``S2 = (s1, s2, ..., sN)`` according to standard broadcasting rules (see :ref:`broadcasting`). If one or more elements in ``T`` are not broadcast-compatible with the others, an exception must be raised.
214+
215+
- After broadcasting elements of ``T`` to a common shape ``S2``, the resulting tuple ``U = (u1, u2, ..., uN)`` must only contain integer arrays having shape ``S2`` (i.e., ``u1 = broadcast_to(t1, S2)``, ``u2 = broadcast_to(t2, S2)``, et cetera).
216+
217+
- Each element in ``U`` must specify a multi-dimensional index ``v_i = (u1[i], u2[i], ..., uN[i])``, where ``i`` ranges over ``S2``. The result of ``A[U]`` must be constructed by gathering elements from ``A`` at each coordinate tuple ``v_i``. For example, let ``A`` have shape ``(4,4)`` and ``U`` contain integer arrays equivalent to ``([0,1], [2,3])``, with ``u1 = [0,1]`` and ``u2 = [2,3]``. The resulting coordinate tuples must be ``(0,2)`` and ``(1,3)``, respectively, and the resulting array must have shape ``(2,)`` and contain elements ``A[(0,2)]`` and ``A[(1,3)]``.
218+
219+
- The result of ``A[U]`` must be an array having the broadcasted shape ``S2``.
220+
176221
Boolean Array Indexing
177222
----------------------
178223

src/array_api_stubs/_draft/array_object.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -610,30 +610,31 @@ def __getitem__(
610610
slice,
611611
ellipsis,
612612
None,
613-
Tuple[Union[int, slice, ellipsis, None], ...],
613+
Tuple[Union[int, slice, ellipsis, array, None], ...],
614614
array,
615615
],
616616
/,
617617
) -> array:
618618
"""
619619
Returns ``self[key]``.
620620
621-
See :ref:`indexing` for details on supported indexing semantics.
622-
623621
Parameters
624622
----------
625623
self: array
626624
array instance.
627-
key: Union[int, slice, ellipsis, None, Tuple[Union[int, slice, ellipsis, None], ...], array]
625+
key: Union[int, slice, ellipsis, None, Tuple[Union[int, slice, ellipsis, array, None], ...], array]
628626
index key.
629627
630628
Returns
631629
-------
632630
out: array
633631
an array containing the accessed value(s). The returned array must have the same data type as ``self``.
634632
635-
.. note::
636-
When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
633+
Notes
634+
-----
635+
636+
- See :ref:`indexing` for details on supported indexing semantics.
637+
- When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
637638
638639
"""
639640

@@ -1081,28 +1082,31 @@ def __rshift__(self: array, other: Union[int, array], /) -> array:
10811082
def __setitem__(
10821083
self: array,
10831084
key: Union[
1084-
int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], array
1085+
int, slice, ellipsis, Tuple[Union[int, slice, ellipsis, array], ...], array
10851086
],
10861087
value: Union[int, float, complex, bool, array],
10871088
/,
10881089
) -> None:
10891090
"""
10901091
Sets ``self[key]`` to ``value``.
10911092
1092-
See :ref:`indexing` for details on supported indexing semantics.
1093-
10941093
Parameters
10951094
----------
10961095
self: array
10971096
array instance.
1098-
key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], array]
1097+
key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis, array], ...], array]
10991098
index key.
11001099
value: Union[int, float, complex, bool, array]
11011100
value(s) to set. Must be compatible with ``self[key]`` (see :ref:`broadcasting`).
11021101
11031102
Notes
11041103
-----
11051104
1105+
- See :ref:`indexing` for details on supported indexing semantics.
1106+
1107+
.. note::
1108+
Indexing semantics when ``key`` is an integer array or a tuple of integers and integer arrays is currently unspecified and thus implementation-defined. This will be revisited in a future revision of this standard.
1109+
11061110
- Setting array values must not affect the data type of ``self``.
11071111
- When ``value`` is a Python scalar (i.e., ``int``, ``float``, ``complex``, ``bool``), behavior must follow specification guidance on mixing arrays with Python scalars (see :ref:`type-promotion`).
11081112
- When ``value`` is an ``array`` of a different data type than ``self``, how values are cast to the data type of ``self`` is implementation defined.

0 commit comments

Comments
 (0)