6.4 Slicing
One common array transformation is slicing: extracting sub-arrays by picking rows from each axis independently.
(Sequenceof Integer): pick rows from an axis by index.
Slice-Dots: preserve remaining adjacent axes
Integer: remove an axis by replacing it with one of its rows.
Slice-New-Axis: insert an axis of a given length.
Create Slice objects using :: and Slice-New-Axis objects using ::new. There is only one Slice-Dots object, namely ::....
When slicing an array with n axes, unless a list of slice specifications contains ::..., it must contain exactly n slice specifications.
> (define arr (build-array #(2 3 4) (λ: ([js : Indexes]) (string-append* (map number->string (vector->list js))))))
> arr eval:44:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: arr
6.4.1 (Sequenceof Integer): pick rows
> (array-slice-ref arr (list '(0 1) '(0 1 2) '(0 1 2 3))) eval:45:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 3
> (array-slice-ref arr (list '(1 0) '(0 1 2) '(0 1 2 3))) eval:46:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 3
> (array-slice-ref arr (list '(0 1) '(0 2) '(0 2))) eval:47:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 2
> (array-slice-ref arr (list '(0 1) '(0 1 2) '())) eval:48:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ()
> (array-slice-ref arr (list '(0 1) '(0 1 2) '(0 0 1 2 2 3))) eval:49:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 3
> (array-slice-ref arr (list '(1 0) '(0 1 2) (in-range 0 4 2))) eval:50:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 2
> (define ds (array-shape arr)) eval:51:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: arr
> (array-slice-ref arr (list (in-range (vector-ref ds 0)) (in-range (vector-ref ds 1)) (in-range (vector-ref ds 2)))) eval:52:0: ds20: unbound identifier;
also, no #%top syntax transformer is bound
in: ds20
6.4.2 Slice: pick rows in a length-aware way
As a slice specification, a Slice object acts like the sequence object returned by in-range, but either start or end may be #f.
If start is #f, it is interpreted as the first valid axis index in the direction of step. If end is #f, it is interpreted as the last valid axis index in the direction of step.
> (array-slice-ref arr (list (::) (::) (::))) eval:53:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::
> (array-slice-ref arr (list (::) (::) (:: #f #f -1))) eval:54:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: -1
> (array-slice-ref arr (list (::) (::) (:: 2 #f 1))) eval:55:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 1
> (array-slice-ref arr (list (::) (::) (:: 1 #f 2))) eval:56:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 2
6.4.3 Slice-Dots: preserve remaining axes
As a slice specification, a Slice-Dots object represents any number of leftover, adjacent axes, and preserves them all.
> (array-slice-ref arr (list ::... (:: 1 #f 2))) eval:57:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 2
> (array-slice-ref arr (list '(0) ::...)) eval:58:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list ::... '(1) ::...)) eval:59:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list ::... '(1))) eval:60:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 1
> (array-slice-ref arr (list ::... '(1) '(1) '(1))) eval:61:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 1
> (array-slice-ref arr (list '(1) ::... '(1) '(1))) eval:62:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 1
> (array-slice-ref arr (list '(1) '(1) ::... '(1))) eval:63:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: 1
> (array-slice-ref arr (list '(1) '(1) '(1) ::...)) eval:64:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
6.4.4 Integer: remove an axis
All of the slice specifications so far preserve the dimensions of the array. Removing an axis can be done by using an integer as a slice specification.
> (array-slice-ref arr (list 0 ::...)) eval:65:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list (::) 1 ::...)) eval:66:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list ::... 1 (::))) eval:67:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::
6.4.5 Slice-New-Axis: add an axis
As a slice specification, (::new dk) inserts dk into the resulting array’s shape, in the corresponding axis position. The new axis has length dk, which must be nonnegative.
> (array-slice-ref arr (list (::new) ::...)) eval:68:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list (::new 2) ::...)) eval:69:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...
> (array-slice-ref arr (list (::) (::new 0) ::...)) eval:70:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: arr19
in: ::...