6.11 Indexing and Slicing
procedure
(array-ref arr js) → A
arr : (Array A) js : In-Indexes
procedure
(array-set! arr js value) → Void
arr : (Settable-Array A) js : In-Indexes value : A
procedure
(array-indexes-ref arr idxs) → (Array A)
arr : (Array A) idxs : (Array In-Indexes)
> (define arr (array #[#[1 2] #[10 20]])) > (define idxs (array #['#(0 0) '#(1 1)])) > (array-indexes-ref arr idxs)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 20])
> (build-array (array-shape idxs) (λ: ([js : Indexes]) (array-ref arr (array-ref idxs js))))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 20])
procedure
(array-indexes-set! arr idxs vals) → Void
arr : (Settable-Array A) idxs : (Array In-Indexes) vals : (Array A)
> (define arr (mutable-array #[#[1 2] #[10 20]])) > (define idxs (array #['#(0 0) '#(1 1)])) > (array-indexes-set! arr idxs (array -1)) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[#[-1 2] #[10 -1]])
procedure
(array-slice-ref arr specs) → (Array A)
arr : (Array A) specs : (Listof Slice-Spec)
procedure
(array-slice-set! arr specs vals) → Void
arr : (Settable-Array A) specs : (Listof Slice-Spec) vals : (Array A)
(let ([idxs (array-slice-ref (indexes-array (array-shape arr)) specs)]) (array-indexes-set! arr idxs vals))
> (define arr (array->mutable-array (axis-index-array #(5 5) 1))) > (array-slice-set! arr (list (:: 1 #f 2) (::)) (array 1)) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array
#[#[0 1 2 3 4]
#[1 1 1 1 1]
#[0 1 2 3 4]
#[1 1 1 1 1]
#[0 1 2 3 4]])
> (array-slice-set! arr (list (::) (:: 1 #f 2)) (array-scale (array-slice-ref arr (list (::) (:: 1 #f 2))) -1)) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.10/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array
#[#[0 -1 2 -3 4]
#[1 -1 1 -1 1]
#[0 -1 2 -3 4]
#[1 -1 1 -1 1]
#[0 -1 2 -3 4]])
syntax
A (Sequenceof Integer) slice specification causes array-slice-ref to pick rows from an axis. An Integer slice specification causes array-slice-ref to remove an axis by replacing it with one of its rows.
See Slicing for an extended example.
syntax
procedure
end : (U #f Integer) = #f (:: start end [step]) → Slice start : (U #f Integer) end : (U #f Integer) step : Integer = 1
procedure
v : Any
procedure
(slice-start s) → (U #f Fixnum)
s : Slice
procedure
s : Slice
procedure
(slice-step s) → Fixnum
s : Slice
array-slice-ref interprets a Slice like an in-range sequence object. When start or end is #f, it is interpreted as an axis-length-dependent endpoint.
This is used internally by array-slice-ref to interpret a Slice object as a sequence of indexes.
syntax
value
procedure
(slice-dots? v) → Boolean
v : Any
syntax
procedure
(::new [dk]) → Slice-New-Axis
dk : Integer = 1
procedure
(slice-new-axis? v) → Boolean
v : Any
procedure
(slice-new-axis-length s) → Index
s : Slice-New-Axis