Skip to content

Commit 6b590e0

Browse files
committed
Add review changes
1 parent ed1f7a8 commit 6b590e0

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

spec/API_specification/indexing.rst

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ To index a single array axis, an array must support standard Python indexing rul
1818

1919
- **Valid** nonnegative indices must reside on the half-open interval ``[0, n)``.
2020

21-
.. note::
22-
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
21+
.. note::
22+
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
2323

2424
- Negative indices must count backward from the last array index, starting from ``-1`` (i.e., negative-one-based indexing, where ``-1`` refers to the last array index).
2525

26-
.. note::
27-
A negative index ``j`` is equivalent to ``n-j``; the former is syntactic sugar for the latter, providing a shorthand for indexing elements that would otherwise need to be specified in terms of the axis (dimension) size.
26+
.. note::
27+
A negative index ``j`` is equivalent to ``n-j``; the former is syntactic sugar for the latter, providing a shorthand for indexing elements that would otherwise need to be specified in terms of the axis (dimension) size.
2828

2929
- **Valid** negative indices must reside on the closed interval ``[-n, -1]``.
3030

31-
.. note::
32-
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
31+
.. note::
32+
This specification does not require bounds checking. The behavior for out-of-bounds integer indices is left unspecified.
3333

3434
- A negative index ``j`` is related to a zero-based nonnegative index ``i`` via ``i = n+j``.
3535

36-
- Colons ``:`` must be used for `slices <https://docs.python.org/3/library/functions.html#slice>`_: ``start:stop:step``, where ``start`` is inclusive and ``stop`` is exclusive.
36+
- Colons ``:`` must be used for `slices <https://docs.python.org/3/library/functions.html#slice>`_: ``start:stop:step``, where ``start`` is inclusive and ``stop`` is exclusive.
3737

38-
.. note::
39-
The specification does not support returning scalar (i.e., non-array) values from operations, including indexing. In contrast to standard Python indexing rules, for any index, or combination of indices, which select a single value, the result must be a zero-dimensional array containing the selected value.
38+
.. note::
39+
The specification does not support returning scalar (i.e., non-array) values from operations, including indexing. In contrast to standard Python indexing rules, for any index, or combination of indices, which select a single value, the result must be a zero-dimensional array containing the selected value.
4040

4141
Slice Syntax
4242
~~~~~~~~~~~~
@@ -106,8 +106,8 @@ Using a slice to index a single array axis must adhere to the following rules. L
106106

107107
- Indexing via ``:`` and ``::`` must be equivalent and have defaults derived from the rules above. Both ``:`` and ``::`` indicate to select all elements along a single axis (dimension).
108108

109-
.. note::
110-
This specification does not require "clipping" out-of-bounds slice indices. This is in contrast to Python slice semantics where ``0:100`` and ``0:10`` are equivalent on a list of length ``10``.
109+
.. note::
110+
This specification does not require "clipping" out-of-bounds slice indices. This is in contrast to Python slice semantics where ``0:100`` and ``0:10`` are equivalent on a list of length ``10``.
111111

112112
The following ranges for the start and stop values of a slice must be supported. Let ``n`` be the axis (dimension) size being sliced. For a slice ``i:j:k``, the behavior specified above should be implemented for the following:
113113

@@ -128,55 +128,57 @@ Multi-dimensional arrays must extend the concept of single-axis indexing to mult
128128

129129
- Each axis may be independently indexed via single-axis indexing by providing a comma-separated sequence ("selection tuple") of single-axis indexing expressions (e.g., ``A[:, 2:10, :, 5]``).
130130

131-
.. note::
132-
In Python, ``A[(exp1, exp2, ..., expN)]`` is equivalent to ``A[exp1, exp2, ..., expN]``; the latter is syntactic sugar for the former.
131+
.. note::
132+
In Python, ``A[(exp1, exp2, ..., expN)]`` is equivalent to ``A[exp1, exp2, ..., expN]``; the latter is syntactic sugar for the former.
133133

134-
Accordingly, if ``A`` has rank ``1``, then ``A[(2:10,)]`` must be equivalent to ``A[2:10]``. If ``A`` has rank ``2``, then ``A[(2:10, :)]`` must be equivalent to ``A[2:10, :]``. And so on and so forth.
134+
Accordingly, if ``A`` has rank ``1``, then ``A[(2:10,)]`` must be equivalent to ``A[2:10]``. If ``A`` has rank ``2``, then ``A[(2:10, :)]`` must be equivalent to ``A[2:10, :]``. And so on and so forth.
135135

136136
- Providing a single nonnegative integer ``i`` as a single-axis index must index the same elements as the slice ``i:i+1``.
137137

138138
- Providing a single negative integer ``i`` as a single-axis index must index the same elements as the slice ``n+i:n+i+1``, where ``n`` is the axis (dimension) size.
139139

140-
- Providing a single integer as a single-axis index must reduce the number of array dimensions by ``1`` (i.e., the array rank should decrease by one; if ``A`` has rank ``2``, ``rank(A)-1 == rank(A[0, :])``). In particular, a selection tuple with the ``m``th element an integer (and all other entries ``:``) indexes a sub-array with rank ``N-1``.
140+
- Providing a single integer as a single-axis index must reduce the number of array dimensions by ``1`` (i.e., the array rank should decrease by one; if ``A`` has rank ``2``, ``rank(A)-1 == rank(A[0, :])``). In particular, a selection tuple with the ``m``\th element an integer (and all other entries ``:``) indexes a sub-array with rank ``N-1``.
141141

142-
.. note::
143-
When providing a single integer as a single-axis index to an array of rank ``1``, the result should be an array of rank ``0``, not a NumPy scalar. Note that this behavior differs from NumPy.
142+
.. note::
143+
When providing a single integer as a single-axis index to an array of rank ``1``, the result should be an array of rank ``0``, not a NumPy scalar. Note that this behavior differs from NumPy.
144144

145145
- Providing a slice must retain array dimensions (i.e., the array rank must remain the same; ``rank(A) == rank(A[:])``).
146146

147147
- Providing `ellipsis <https://docs.python.org/3/library/constants.html#Ellipsis>`_ must apply ``:`` to each dimension necessary to index all dimensions (e.g., if ``A`` has rank ``4``, ``A[1:, ..., 2:5] == A[1:, :, :, 2:5]``). Only a single ellipsis must be allowed. An ``IndexError`` exception must be raised if more than one ellipsis is provided.
148148

149149
- Providing an empty tuple or an ellipsis to an array of rank ``0`` must result in an array of the same rank (i.e., if ``A`` has rank ``0``, ``A == A[()]`` and ``A == A[...]``).
150150

151-
.. note::
152-
This behavior differs from NumPy where providing an empty tuple to an array of rank ``0`` returns a NumPy scalar.
151+
.. note::
152+
This behavior differs from NumPy where providing an empty tuple to an array of rank ``0`` returns a NumPy scalar.
153153

154154
- Except in the case of providing a single ellipsis (e.g., ``A[2:10, ...]`` or ``A[1:, ..., 2:5]``), the number of provided single-axis indexing expressions should equal ``N``. For example, if ``A`` has rank ``2``, a single-axis indexing expression should be explicitly provided for both axes (e.g., ``A[2:10, :]``). An ``IndexError`` exception should be raised if the number of provided single-axis indexing expressions is less than ``N``.
155155

156-
.. note::
157-
Some libraries, such as SymPy, support flat indexing (i.e., providing a single-axis indexing expression to a higher-dimensional array). That practice is not supported here.
156+
.. note::
157+
Some libraries, such as SymPy, support flat indexing (i.e., providing a single-axis indexing expression to a higher-dimensional array). That practice is not supported here.
158158

159-
To perform flat indexing, use ``reshape(x, (-1,))[integer]``.
159+
To perform flat indexing, use ``reshape(x, (-1,))[integer]``.
160160

161161
- An ``IndexError`` exception must be raised if the number of provided single-axis indexing expressions is greater than ``N``.
162162

163-
.. note::
164-
This specification leaves unspecified the behavior of providing a slice which attempts to select elements along a particular axis, but whose starting index is out-of-bounds.
163+
.. note::
164+
This specification leaves unspecified the behavior of providing a slice which attempts to select elements along a particular axis, but whose starting index is out-of-bounds.
165165

166-
*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.*
166+
*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.*
167167

168168
Boolean Array Indexing
169169
----------------------
170170

171-
.. important:: Data-dependent output shape
171+
.. admonition:: Data-dependent output shape
172+
:class: admonition important
173+
172174
For common boolean array use cases (e.g., using a dynamically-sized boolean array mask to filter the values of another array), the shape of the output array is data-dependent; hence, array libraries which build computation graphs (e.g., JAX, Dask, etc.) may find boolean array indexing difficult to implement. Accordingly, such libraries may choose to omit boolean array indexing. See :ref:`data-dependent-output-shapes` section for more details.
173175

174176
An array must support indexing where the **sole index** is an ``M``-dimensional boolean array ``B`` with shape ``S1 = (s1, ..., sM)`` according to the following rules. Let ``A`` be an ``N``-dimensional array with shape ``S2 = (s1, ..., sM, ..., sN)``.
175177

176178
- If ``N >= M``, then ``A[B]`` must replace the first ``M`` dimensions of ``A`` with a single dimension having a size equal to the number of ``True`` elements in ``B``. The values in the resulting array must be in row-major (C-style order); this is equivalent to ``A[nonzero(B)]``.
177179

178-
.. note::
179-
For example, if ``N == M == 2``, indexing ``A`` via a boolean array ``B`` will return a one-dimensional array whose size is equal to the number of ``True`` elements in ``B``.
180+
.. note::
181+
For example, if ``N == M == 2``, indexing ``A`` via a boolean array ``B`` will return a one-dimensional array whose size is equal to the number of ``True`` elements in ``B``.
180182

181183
- If ``N < M``, then an ``IndexError`` exception must be raised.
182184

0 commit comments

Comments
 (0)