6 Arrays
On this page:
top
up

6.12TransformationsπŸ”— i

procedure

( array-transform arrdsproc)(Array A)

arr:(Array A)
Returns an array with shape ds and elements taken from arr, by composing arr’s procedure with proc.

Possibly the most useless, but simplest example of proc disregards its input indexes and returns constant indexes:
> (define arr(array #[#[01]#[2'three]]))
> (array-transform arr#(33)(λ: ([js: Indexes ])#(11)))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U 'three Byte)))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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]])

Doubling an array in every dimension by duplicating elements:
> (define arr(index-array #(33)))
> arr

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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]])

arr
(vector-map (λ: ([d: Index ])(* d2))(array-shape arr))
(λ: ([js: Indexes ])
(vector-map (λ: ([j: Index ])(quotient j2))js)))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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]])

When array-strictness is #f, the above result takes little more space than the original array.

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
Appends the arrays in arrs along axis k. If the arrays’ shapes are not the same, they are broadcast first.

Examples:
> (define arr(array #[#[01]#[23]]))
> (define brr(array #[#['a'b]#['c'd]]))
> (array-append* (list arrbrr))

- : #(struct:Array

(Indexes

Index

(Boxof Boolean)

(-> Void)

(-> Indexes (U 'a 'b 'c 'd Byte)))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 arrbrr)1)

- : #(struct:Array

(Indexes

Index

(Boxof Boolean)

(-> Void)

(-> Indexes (U 'a 'b 'c 'd Byte)))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[0 1] #[2 3] #['x 'x]])

For an append-like operation that increases the dimension of the broadcast arrays, see array-list->array .

procedure

( array-axis-insert arrk[dk])(Array A)

arr:(Array A)
dk:Integer =1
Inserts an axis of length dk before axis number k, which must be no greater than the dimension of arr.

Examples:
> (define arr(array #[#[01]#[23]]))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[#[0 1] #[2 3]]])

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[#[0 1]] #[#[2 3]]])

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[#[0] #[1]] #[#[2] #[3]]])

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 arrkjk)(Array A)

arr:(Array A)
jk:Integer
Removes an axis from arr by keeping only row jk in axis k, which must be less than the dimension of arr.

Examples:
> (define arr(array #[#[01]#[23]]))
> (array-axis-ref arr00)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[0 1])

> (array-axis-ref arr01)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[2 3])

> (array-axis-ref arr10)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[0 2])

procedure

( array-axis-swap arrk0k1)(Array A)

arr:(Array A)
k0:Integer
k1:Integer
Returns an array like arr, but with axes k0 and k1 swapped. In two dimensions, this is called a transpose.

Examples:
> (array-axis-swap (array #[#[01]#[23]])01)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 #(222)))
> arr

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 arr01)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 arr12)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 arrperm)(Array A)

arr:(Array A)
perm:(Listof Integer )
Returns an array like arr, but with axes permuted according to perm.

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 '(012) is the identity permutation for three-dimensional arrays, '(10) swaps axes 0 and 1, and '(3120) swaps axes 0 and 3.

The permutation must contain each integer from 0 to (- (array-dims arr)1) exactly once.

Examples:
> (array-axis-swap (array #[#[01]#[23]])01)

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 #[#[01]#[23]])'(10))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[0 2] #[1 3]])

procedure

( array-reshape arrds)(Array A)

arr:(Array A)
Returns an array with elements in the same row-major order as arr, but with shape ds. The product of the indexes in ds must be (array-size arr).

Examples:
> (define arr(indexes-array #(23)))
> arr

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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#(32))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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 #(33))#(9))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/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)
Returns an array with shape (vector (array-size arr)), with the elements of arr in row-major order.

Examples:

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[10])

> (array-flatten (array #[#[01]#[23]]))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[0 1 2 3])

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /