You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec/API_specification/indexing.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -89,12 +89,10 @@ j > i + (m-1)k
89
89
```
90
90
91
91
```{note}
92
-
93
92
For `i` on the interval `[0, n)` (where `n` is the axis size), `j` on the interval `(0, n]`, `i` less than `j`, and positive step `k`, a starting index `i` is **always** included, while the stopping index `j` is **always** excluded. This preserves `x[:i]+x[i:]` always being equal to `x`.
94
93
```
95
94
96
95
```{note}
97
-
98
96
Using a slice to index into a single array axis should select the same elements as using a slice to index a Python list of the same size.
99
97
```
100
98
@@ -113,7 +111,6 @@ Using a slice to index a single array axis must adhere to the following rules. L
113
111
- 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).
114
112
115
113
```{note}
116
-
117
114
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`.
118
115
```
119
116
@@ -126,6 +123,7 @@ The following ranges for the start and stop values of a slice must be supported.
126
123
127
124
The behavior outside of these bounds is unspecified.
128
125
126
+
```{note}
129
127
_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._
130
128
```
131
129
@@ -136,7 +134,6 @@ Multi-dimensional arrays must extend the concept of single-axis indexing to mult
136
134
- 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]`).
137
135
138
136
```{note}
139
-
140
137
In Python, `x[(exp1, exp2, ..., expN)]` is equivalent to `x[exp1, exp2, ..., expN]`; the latter is syntactic sugar for the former.
141
138
```
142
139
@@ -148,14 +145,19 @@ Multi-dimensional arrays must extend the concept of single-axis indexing to mult
148
145
149
146
- Providing a slice must retain array dimensions (i.e., the array rank must remain the same; `rank(A) == rank(A[:])`).
150
147
151
-
- If the number of provided single-axis indexing expressions is less than `N`, then `:` must be assumed for the remaining dimensions (e.g., if `A` has rank `2`, `A[2:10] == A[2:10, :]`).
148
+
- 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.
152
149
153
-
- An `IndexError` exception must be raised if the number of provided single-axis indexing expressions is greater than `N`.
150
+
- Except in the case of providing an ellipsis to index all trailing dimensions (e.g., `A[2:10, ...]`), the number of provided single-axis indexing expressions must equal `N`. For example, if `A` has rank `2`, a single-axis indexing expression must be explicitly provided for both axes (e.g., `A[2:10, :]`). An `IndexError` exception must be raised if the number of provided single-axis indexing expressions is less than `N`.
154
151
155
-
- 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.
152
+
```{note}
153
+
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
154
157
-
```{note}
155
+
To perform flat indexing, use `reshape(x, (-1,))[integer]`.
156
+
```
157
+
158
+
- An `IndexError` exception must be raised if the number of provided single-axis indexing expressions is greater than `N`.
158
159
160
+
```{note}
159
161
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.
160
162
161
163
_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._
@@ -174,7 +176,6 @@ An array must support indexing where the **sole index** is an `M`-dimensional bo
174
176
- 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)]`.
175
177
176
178
```{note}
177
-
178
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`.
179
180
```
180
181
@@ -191,6 +192,5 @@ An array must support indexing where the **sole index** is an `M`-dimensional bo
191
192
The result of an indexing operation (e.g., multi-axis indexing, boolean array indexing, etc) must be an array of the same data type as the indexed array.
192
193
193
194
```{note}
194
-
195
195
The specified return value behavior includes indexing operations which return a single value (e.g., accessing a single element within a one-dimensional array).
0 commit comments