@@ -34,8 +34,8 @@ public final class Indices {
34
34
* single element and therefore is excluded from the computation of the rank.
35
35
*
36
36
* <p>For example, given a 3D matrix on the axis [x, y, z], if
37
- * {@code matrix.slice(all(), at(0), at(0)}, then the rank of the returned slice is 1 and its
38
- * number of elements is {@code x.numElements()}
37
+ * {@code matrix.slice(all(), at(0), at(0)}, then the rank of the returned slice is 1 and its number of elements is
38
+ * {@code x.numElements()}
39
39
*
40
40
* @param coord coordinate of the element on the indexed axis
41
41
* @return index
@@ -65,12 +65,12 @@ public static Index at(NdArray<? extends Number> coord) {
65
65
* A coordinate that selects a specific element on a given dimension.
66
66
*
67
67
* <p>When this index is applied to a given dimension, the dimension is resolved as a
68
- * single element and therefore, if {@code keepDim} is false, is excluded from the computation of the rank.
69
- * If {@code} keepDim is true, the dimension is collapsed down to one element.
68
+ * single element and therefore, if {@code keepDim} is false, is excluded from the computation of the rank. If {@code}
69
+ * keepDim is true, the dimension is collapsed down to one element.
70
70
*
71
71
* <p>For example, given a 3D matrix on the axis [x, y, z], if
72
- * {@code matrix.slice(all(), at(0), at(0)}, then the rank of the returned slice is 1 and its
73
- * number of elements is {@code x.numElements()}
72
+ * {@code matrix.slice(all(), at(0), at(0)}, then the rank of the returned slice is 1 and its number of elements is
73
+ * {@code x.numElements()}
74
74
*
75
75
* @param coord coordinate of the element on the indexed axis
76
76
* @param keepDim whether to remove the dimension.
@@ -89,8 +89,8 @@ public static Index at(long coord, boolean keepDim) {
89
89
* If {@code} keepDim is true, the dimension is collapsed down to one element instead of being removed.
90
90
*
91
91
* @param coord scalar indicating the coordinate of the element on the indexed axis
92
- * @return index
93
92
* @param keepDim whether to remove the dimension.
93
+ * @return index
94
94
* @throws IllegalRankException if {@code coord} is not a scalar (rank 0)
95
95
*/
96
96
public static Index at (NdArray <? extends Number > coord , boolean keepDim ) {
@@ -149,29 +149,27 @@ public static Index seq(NdArray<? extends Number> coords) {
149
149
}
150
150
151
151
/**
152
- * An index that returns only elements found at an even position in the
153
- * original dimension.
152
+ * An index that returns only elements found at an even position in the original dimension.
154
153
*
155
154
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and n is even,
156
155
* {@code even()} returns x<sub>0</sub>, x<sub>2</sub>, ..., x<sub>n-2</sub>
157
156
*
158
157
* @return index
159
158
*/
160
159
public static Index even () {
161
- return slice ( null , null , 2 );
160
+ return step ( 2 );
162
161
}
163
162
164
163
/**
165
- * An index that returns only elements found at an odd position in the
166
- * original dimension.
164
+ * An index that returns only elements found at an odd position in the original dimension.
167
165
*
168
166
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and n is even,
169
167
* {@code odd()} returns x<sub>1</sub>, x<sub>3</sub>, ..., x<sub>n-1</sub>
170
168
*
171
169
* @return index
172
170
*/
173
171
public static Index odd () {
174
- return slice ( 1 , null , 2 );
172
+ return sliceFrom ( 1 , 2 );
175
173
}
176
174
177
175
/**
@@ -180,16 +178,15 @@ public static Index odd() {
180
178
* <p>For example, given a vector with {@code n} elements on the {@code x} axis,
181
179
* {@code step(k)} returns x<sub>0</sub>, x<sub>k</sub>, x<sub>k*2</sub>, ...
182
180
*
183
- * @param stepLength the number of elements between each steps
181
+ * @param stride the number of elements between each steps
184
182
* @return index
185
183
*/
186
- public static Index step (long stepLength ) {
187
- return slice ( null , null , stepLength );
184
+ public static Index step (long stride ) {
185
+ return new Step ( stride );
188
186
}
189
187
190
188
/**
191
- * An index that returns only elements on a given dimension starting at a
192
- * specific coordinate.
189
+ * An index that returns only elements on a given dimension starting at a specific coordinate.
193
190
*
194
191
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and {@code n > k},
195
192
* {@code from(k)} returns x<sub>k</sub>, x<sub>k+1</sub>, ..., x<sub>n-1</sub>
@@ -198,42 +195,40 @@ public static Index step(long stepLength) {
198
195
* @return index
199
196
*/
200
197
public static Index sliceFrom (long start ) {
201
- return slice (start , null );
198
+ return sliceFrom (start , 1 );
202
199
}
203
200
204
201
/**
205
- * An index that returns only elements on a given dimension up to a
206
- * specific coordinate .
202
+ * An index that returns only elements on a given dimension starting at a specific coordinate, using the given
203
+ * stride .
207
204
*
208
205
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and {@code n > k},
209
- * {@code to (k)} returns x<sub>0 </sub>, x<sub>1</sub>, ..., x<sub>k </sub>
206
+ * {@code from (k)} returns x<sub>k </sub>, x<sub>k+ 1</sub>, ..., x<sub>n-1 </sub>
210
207
*
211
- * @param end coordinate of the last element of the sequence (exclusive)
208
+ * @param start coordinate of the first element of the sequence
209
+ * @param stride the stride to use
212
210
* @return index
211
+ * @see #slice(long, long, long)
213
212
*/
214
- public static Index sliceTo (long end ) {
215
- return slice ( null , end );
213
+ public static Index sliceFrom (long start , long stride ) {
214
+ return new SliceFrom ( start , stride );
216
215
}
217
216
218
217
/**
219
- * An index that returns only elements on a given dimension starting at a
220
- * specific coordinate, using the given stride.
218
+ * An index that returns only elements on a given dimension up to a specific coordinate.
221
219
*
222
220
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and {@code n > k},
223
- * {@code from (k)} returns x<sub>k </sub>, x<sub>k+ 1</sub>, ..., x<sub>n-1 </sub>
221
+ * {@code to (k)} returns x<sub>0 </sub>, x<sub>1</sub>, ..., x<sub>k </sub>
224
222
*
225
- * @param start coordinate of the first element of the sequence
226
- * @param stride the stride to use
223
+ * @param end coordinate of the last element of the sequence (exclusive)
227
224
* @return index
228
- * @see #slice(long, long, long)
229
225
*/
230
- public static Index sliceFrom (long start , long stride ) {
231
- return slice ( start , null , stride );
226
+ public static Index sliceTo (long end ) {
227
+ return sliceTo ( end , 1 );
232
228
}
233
229
234
230
/**
235
- * An index that returns only elements on a given dimension up to a
236
- * specific coordinate, using the given stride.
231
+ * An index that returns only elements on a given dimension up to a specific coordinate, using the given stride.
237
232
*
238
233
* <p>For example, given a vector with {@code n} elements on the {@code x} axis, and {@code n > k},
239
234
* {@code to(k)} returns x<sub>0</sub>, x<sub>1</sub>, ..., x<sub>k</sub>
@@ -244,7 +239,7 @@ public static Index sliceFrom(long start, long stride) {
244
239
* @see #slice(long, long, long)
245
240
*/
246
241
public static Index sliceTo (long end , long stride ) {
247
- return slice ( null , end , stride );
242
+ return new SliceTo ( end , stride );
248
243
}
249
244
250
245
/**
@@ -272,16 +267,15 @@ public static Index range(long start, long end) {
272
267
public static Index flip () {
273
268
return slice (null , null , -1 );
274
269
}
275
-
270
+
276
271
/**
277
- * An index that returns elements according to an hyperslab defined by {@code start},
278
- * {@code stride}, {@code count}, {@code block}. See {@link Hyperslab}.
279
- *
272
+ * An index that returns elements according to an hyperslab defined by {@code start}, {@code stride}, {@code count},
273
+ * {@code block}. See {@link Hyperslab}.
274
+ *
280
275
* @param start Starting location for the hyperslab.
281
276
* @param stride The number of elements to separate each element or block to be selected.
282
277
* @param count The number of elements or blocks to select along the dimension.
283
278
* @param block The size of the block selected from the dimension.
284
- *
285
279
* @return index
286
280
*/
287
281
public static Index hyperslab (long start , long stride , long count , long block ) {
@@ -293,123 +287,87 @@ public static Index hyperslab(long start, long stride, long count, long block) {
293
287
*
294
288
* @return index
295
289
*/
296
- public static Index newAxis (){
290
+ public static Index newAxis () {
297
291
return NewAxis .INSTANCE ;
298
292
}
299
293
300
294
/**
301
- * An index that expands to fill all available source dimensions.
302
- * Works the same as Python's {@code ...}.
303
- * @see #expand()
295
+ * An index that expands to fill all available source dimensions. Works the same as Python's {@code ...}.
296
+ *
304
297
* @return index
298
+ * @see #expand()
305
299
*/
306
- public static Index ellipsis (){
300
+ public static Index ellipsis () {
307
301
return Ellipsis .INSTANCE ;
308
302
}
309
303
310
304
/**
311
- * An index that expands to fill all available source dimensions.
312
- * Works the same as Python's {@code ...}.
305
+ * An index that expands to fill all available source dimensions. Works the same as Python's {@code ...}.
313
306
*
314
307
* @return index
315
308
*/
316
- public static Index expand (){
309
+ public static Index expand () {
317
310
return ellipsis ();
318
311
}
319
312
320
313
/**
321
- * An index that returns elements between {@code start} and {@code end}.
322
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
314
+ * An index that returns elements between {@code start} and {@code end}. If {@code start} or {@code end} is {@code
315
+ * null}, starts or ends at the beginning or the end, respectively.
323
316
* <p>
324
317
* Analogous to Python's {@code :} slice syntax.
325
318
*
326
319
* @return index
327
320
*/
328
- public static Index slice (Long start , Long end ){
321
+ public static Index slice (long start , long end ) {
329
322
return slice (start , end , 1 );
330
323
}
331
324
332
325
/**
333
- * An index that returns elements between {@code start} and {@code end}.
334
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
326
+ * An index that returns every {@code stride}-th element between {@code start} and {@code end}. If {@code start} or
327
+ * {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
335
328
* <p>
336
329
* Analogous to Python's {@code :} slice syntax.
337
330
*
338
331
* @return index
339
332
*/
340
- public static Index slice (long start , Long end ){
341
- return slice (start , end , 1 );
342
- }
343
-
344
- /**
345
- * An index that returns elements between {@code start} and {@code end}.
346
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
347
- * <p>
348
- * Analogous to Python's {@code :} slice syntax.
349
- *
350
- * @return index
351
- */
352
- public static Index slice (Long start , long end ){
353
- return slice (start , end , 1 );
333
+ public static Index slice (long start , long end , long stride ) {
334
+ return new Slice (start , end , stride );
354
335
}
355
336
356
337
/**
357
- * An index that returns elements between {@code start} and {@code end}.
358
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
338
+ * An index that returns elements between {@code start} and {@code end}. If {@code start} or {@code end} is {@code
339
+ * null}, starts or ends at the beginning or the end, respectively.
359
340
* <p>
360
341
* Analogous to Python's {@code :} slice syntax.
361
342
*
362
343
* @return index
363
344
*/
364
- public static Index slice (long start , long end ){
345
+ public static Index slice (Long start , Long end ) {
365
346
return slice (start , end , 1 );
366
347
}
367
348
368
349
/**
369
- * An index that returns every {@code stride}-th element between {@code start} and {@code end}.
370
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
350
+ * An index that returns every {@code stride}-th element between {@code start} and {@code end}. If {@code start} or
351
+ * {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
371
352
* <p>
372
353
* Analogous to Python's {@code :} slice syntax.
373
354
*
374
355
* @return index
375
356
*/
376
- public static Index slice (Long start , Long end , long stride ){
377
- return new Slice (start , end , stride );
378
- }
379
-
380
- /**
381
- * An index that returns every {@code stride}-th element between {@code start} and {@code end}.
382
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
383
- * <p>
384
- * Analogous to Python's {@code :} slice syntax.
385
- *
386
- * @return index
387
- */
388
- public static Index slice (long start , Long end , long stride ){
389
- return new Slice (start , end , stride );
390
- }
357
+ public static Index slice (Long start , Long end , long stride ) {
358
+ if (start == null && end == null ) {
359
+ if (stride == 1 ) {
360
+ return Indices .all ();
361
+ } else {
362
+ return Indices .step (stride );
363
+ }
364
+ } else if (start == null ) {
365
+ return Indices .sliceTo (end , stride );
366
+ } else if (end == null ) {
367
+ return Indices .sliceFrom (start , stride );
368
+ }
391
369
392
- /**
393
- * An index that returns every {@code stride}-th element between {@code start} and {@code end}.
394
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
395
- * <p>
396
- * Analogous to Python's {@code :} slice syntax.
397
- *
398
- * @return index
399
- */
400
- public static Index slice (Long start , long end , long stride ){
401
- return new Slice (start , end , stride );
370
+ return slice (start .longValue (), end .longValue (), stride );
402
371
}
403
372
404
- /**
405
- * An index that returns every {@code stride}-th element between {@code start} and {@code end}.
406
- * If {@code start} or {@code end} is {@code null}, starts or ends at the beginning or the end, respectively.
407
- * <p>
408
- * Analogous to Python's {@code :} slice syntax.
409
- *
410
- * @return index
411
- */
412
- public static Index slice (long start , long end , long stride ){
413
- return new Slice (start , end , stride );
414
- }
415
373
}
0 commit comments