Skip to content

Commit d299407

Browse files
committed
feat: add support for integer array indexing
Closes: data-apis#669 Ref: https://numpy.org/neps/nep-0021-advanced-indexing.html
1 parent 287b834 commit d299407

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

spec/draft/API_specification/indexing.rst

+46
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,46 @@ 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+
An array must support indexing a one-dimensional array by a one-dimensional integer array according to the following rules. Let ``A`` be a one-dimensional array with shape ``S1 = (n)``, and let ``J`` be a one-dimensional integer array with ``S2 = (k,)``.
186+
187+
- Each integer index element in ``J`` must satisfy the rules stated above for indexing a single-axis (see :ref:`indexing-single-axis`). Namely,
188+
189+
- Nonnegative indices must start at ``0`` (i.e., zero-based indexing).
190+
- **Valid** nonnegative indices must reside on the half-open interval ``[0, n)``.
191+
- Negative indices must count backward from the last index along an array dimension, starting from ``-1`` (i.e., negative-one-based indexing, where ``-1`` refers to the last index along an array dimension).
192+
- **Valid** negative indices must reside on the closed interval ``[-n, -1]``.
193+
- A negative index ``j`` is related to a zero-based nonnegative index ``i`` via ``i = n+j``.
194+
195+
.. note::
196+
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
197+
198+
- Providing duplicate integer index elements in ``J`` must result in the duplication of the corresponding elements of ``A`` in the resulting array.
199+
200+
- The result of providing a valid integer index for each element in ``J`` must be an array having shape ``S3 = (k,)``.
201+
202+
An array must support indexing an array having more than one dimension by an indexing tuple which includes only integers and one-dimensional integer arrays according to the following rules. Let ``A`` be an ``N``-dimensional array with shape ``S1 = (s1, ..., sM, ..., sN)`` and ``T`` be a tuple ``(t1, ..., tM, ..., tN)`` having length ``N`` and containing only valid integer and one-dimensional integer array indices.
203+
204+
.. note::
205+
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
206+
207+
- Providing an integer tuple element ``tk`` having value ``k`` must be equivalent to providing a zero-dimensional integer array ``K`` containing ``k``.
208+
209+
- If ``T`` consists of only integers and zero-dimensional integer arrays, the result must be equivalent to indexing multiple axes using integer indices (see :ref:`indexing-multi-axis`).
210+
211+
- When ``T`` contains a one-dimensional integer array ``J`` having shape ``S2 = (m,)``, where ``m`` is greater than or equal to the number of elements in any other one-dimensional integer array in ``T``, each element of ``T`` must be broadcast to the same shape as ``J``.
212+
213+
- An ``IndexError`` exception must be raised if any element of ``T`` is not broadcast-compatible with ``J`` (see :ref:`broadcasting`).
214+
215+
- After broadcasting, ``T`` must be equivalent to a tuple ``U = (u1, ..., uM, ..., uN)`` containing only one-dimensional integer arrays having shape ``S2``.
216+
217+
- Let ``v_i`` be the tuple formed by the integer indices ``u1[i], u2[i], ..., uM[i], ..., uN[i]``. When applying the indexing tuple ``U`` to ``A``, the resulting array must be a one-dimensional array containing each element ``A[v_i]`` for ``i`` on the half-open interval ``[0, m)``.
218+
219+
.. note::
220+
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.
221+
176222
Boolean Array Indexing
177223
----------------------
178224

0 commit comments

Comments
 (0)