6.12 Transformations
procedure
(array-transform arr ds proc) → (Array A)
arr : (Array A) ds : In-Indexes proc : (Indexes -> In-Indexes)
> (define arr (array #[#[0 1] #[2 'three]])) > (array-transform arr #(3 3) (λ: ([js : Indexes]) #(1 1)))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U 'three 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
#[#['three 'three 'three]
#['three 'three 'three]
#['three 'three 'three]])
> (define arr (index-array #(3 3))) > arr
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<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 #[#[0 1 2] #[3 4 5] #[6 7 8]])
> (array-transform arr (vector-map (λ: ([d : Index]) (* d 2)) (array-shape arr)) (λ: ([js : Indexes]) (vector-map (λ: ([j : Index]) (quotient j 2)) js)))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<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
#[#[0 0 1 1 2 2]
#[0 0 1 1 2 2]
#[3 3 4 4 5 5]
#[3 3 4 4 5 5]
#[6 6 7 7 8 8]
#[6 6 7 7 8 8]])
Almost all array transformations, including Slicing, are implemented using array-transform or its unsafe counterpart.
procedure
(array-append* arrs [k]) → (Array A)
arrs : (Listof (Array A)) k : Integer = 0
> (define arr (array #[#[0 1] #[2 3]])) > (define brr (array #[#['a 'b] #['c 'd]])) > (array-append* (list arr brr))
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes (U 'a 'b 'c 'd 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 #[#[0 1] #[2 3] #['a 'b] #['c 'd]])
> (array-append* (list arr brr) 1)
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes (U 'a 'b 'c 'd 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 #[#[0 1 'a 'b] #[2 3 'c 'd]])
> (array-append* (list arr (array 'x)))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U 'x 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 #[#[0 1] #[2 3] #['x 'x]])
procedure
(array-axis-insert arr k [dk]) → (Array A)
arr : (Array A) k : Integer dk : Integer = 1
> (define arr (array #[#[0 1] #[2 3]])) > (array-axis-insert arr 0)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[#[0 1] #[2 3]]])
> (array-axis-insert arr 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[#[0 1]] #[#[2 3]]])
> (array-axis-insert arr 2)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[#[0] #[1]] #[#[2] #[3]]])
> (array-axis-insert arr 1 2)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[#[0 1] #[0 1]] #[#[2 3] #[2 3]]])
procedure
(array-axis-ref arr k jk) → (Array A)
arr : (Array A) k : Integer jk : Integer
> (define arr (array #[#[0 1] #[2 3]])) > (array-axis-ref arr 0 0)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[0 1])
> (array-axis-ref arr 0 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[2 3])
> (array-axis-ref arr 1 0)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[0 2])
procedure
(array-axis-swap arr k0 k1) → (Array A)
arr : (Array A) k0 : Integer k1 : Integer
> (array-axis-swap (array #[#[0 1] #[2 3]]) 0 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[0 2] #[1 3]])
> (define arr (indexes-array #(2 2 2))) > arr
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<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
#[#[#['#(0 0 0) '#(0 0 1)]
#['#(0 1 0) '#(0 1 1)]]
#[#['#(1 0 0) '#(1 0 1)]
#['#(1 1 0) '#(1 1 1)]]])
> (array-axis-swap arr 0 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<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
#[#[#['#(0 0 0) '#(0 0 1)]
#['#(1 0 0) '#(1 0 1)]]
#[#['#(0 1 0) '#(0 1 1)]
#['#(1 1 0) '#(1 1 1)]]])
> (array-axis-swap arr 1 2)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<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
#[#[#['#(0 0 0) '#(0 1 0)]
#['#(0 0 1) '#(0 1 1)]]
#[#['#(1 0 0) '#(1 1 0)]
#['#(1 0 1) '#(1 1 1)]]])
procedure
(array-axis-permute arr perm) → (Array A)
arr : (Array A) perm : (Listof Integer)
The list perm represents a mapping from source axis numbers to destination axis numbers: the source is the list position, the destination is the list element. For example, the permutation '(0 1 2) is the identity permutation for three-dimensional arrays, '(1 0) swaps axes 0 and 1, and '(3 1 2 0) swaps axes 0 and 3.
The permutation must contain each integer from 0 to (- (array-dims arr) 1) exactly once.
> (array-axis-swap (array #[#[0 1] #[2 3]]) 0 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[0 2] #[1 3]])
> (array-axis-permute (array #[#[0 1] #[2 3]]) '(1 0))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[#[0 2] #[1 3]])
procedure
(array-reshape arr ds) → (Array A)
arr : (Array A) ds : In-Indexes
> (define arr (indexes-array #(2 3))) > arr
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<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 #[#['#(0 0) '#(0 1) '#(0 2)] #['#(1 0) '#(1 1) '#(1 2)]])
> (array-reshape arr #(3 2))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<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 #[#['#(0 0) '#(0 1)] #['#(0 2) '#(1 0)] #['#(1 1) '#(1 2)]])
> (array-reshape (index-array #(3 3)) #(9))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<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 #[0 1 2 3 4 5 6 7 8])
procedure
(array-flatten arr) → (Array A)
arr : (Array A)
> (array-flatten (array 10))
- : #(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 #[10])
> (array-flatten (array #[#[0 1] #[2 3]]))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 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 #[0 1 2 3])