6 Arrays
On this page:
::
top
up

6.11Indexing and SlicingπŸ”— i

procedure

( array-ref arrjs)A

arr:(Array A)
Returns the element of arr at position js. If any index in js is negative or not less than its corresponding axis length, array-ref raises an error.

procedure

( array-set! arrjsvalue)Void

value:A
Sets the element of arr at position js to value. If any index in js is negative or not less than its corresponding axis length, array-set! raises an error.

procedure

( array-indexes-ref arridxs)(Array A)

arr:(Array A)
idxs:(Array In-Indexes )
High-level explanation: Returns multiple elements from arr in a new array.

Lower-level explanation: Returns an array with same shape as idxs, whose elements are array-ref ’d from arr using the indexes in idxs.

Examples:
> (define arr(array #[#[12]#[1020]]))
> (define idxs(array #['#(00)'#(11)]))
> (array-indexes-ref arridxs)

- : #(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 #[1 20])

Implementation-level explanation: (array-indexes-ref arridxs) is equivalent to
(λ: ([js: Indexes ])
(array-ref arr(array-ref idxsjs))))

- : #(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 #[1 20])

but faster.

procedure

( array-indexes-set! arridxsvals)Void

idxs:(Array In-Indexes )
vals:(Array A)
Indexes arr in the same way that array-indexes-ref does, but mutates elements. If idxs and vals do not have the same shape, they are broadcast first.

Examples:
> (define arr(mutable-array #[#[12]#[1020]]))
> (define idxs(array #['#(00)'#(11)]))
> (array-indexes-set! arridxs(array -1))
> arr

- : #(struct:Mutable-Array

(Indexes

Index

(Boxof Boolean)

(-> Void)

(-> Indexes Integer)

(-> Indexes Integer Void)

(Vectorof Integer))

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

(mutable-array #[#[-1 2] #[10 -1]])

When two indexes in idxs are the same, the element at that index is mutated more than once in some unspecified order.

procedure

( array-slice-ref arrspecs)(Array A)

arr:(Array A)
specs:(Listof Slice-Spec )
Returns a transformation of arr according to the list of slice specifications specs. See Slicing for a discussion and examples.

procedure

( array-slice-set! arrspecsvals)Void

specs:(Listof Slice-Spec )
vals:(Array A)
Like array-indexes-set! , but for slice specifications. Equivalent to
(let ([idxs(array-slice-ref (indexes-array (array-shape arr))specs)])
(array-indexes-set! arridxsvals))

Examples:
> (array-slice-set! arr(list (:: 1#f2)(:: ))(array 1))
> arr

- : #(struct:Mutable-Array

(Indexes

Index

(Boxof Boolean)

(-> Void)

(-> Indexes Integer)

(-> Indexes Integer Void)

(Vectorof Integer))

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

arr(list (:: )(:: 1#f2))
(array-scale (array-slice-ref arr(list (:: )(:: 1#f2)))-1))
> arr

- : #(struct:Mutable-Array

(Indexes

Index

(Boxof Boolean)

(-> Void)

(-> Indexes Integer)

(-> Indexes Integer Void)

(Vectorof Integer))

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

When a slice specification refers to an element in arr more than once, the element is mutated more than once in some unspecified order.

syntax

Slice-Spec

The type of a slice specification. Currently defined as

(U (Sequenceof Integer )Slice Slice-Dots Integer Slice-New-Axis )

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

Slice

procedure

( :: [end])Slice

end:(U #fInteger )=#f
(:: startend[step])Slice
start:(U #fInteger )
end:(U #fInteger )
step:Integer =1

procedure

( slice? v)Boolean

v:Any

procedure

( slice-start s)(U #fFixnum )

s:Slice

procedure

( slice-end s)(U #fFixnum )

s:Slice

procedure

( slice-step s)Fixnum

s:Slice
The type of in-range -like slice specifications, its constructor, predicate, and accessors.

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.

s:Slice
dk:Index
Given a slice s and an axis length dk, returns the arguments to in-range that would produce an equivalent slice specification.

This is used internally by array-slice-ref to interpret a Slice object as a sequence of indexes.

syntax

Slice-Dots

value

::... :Slice-Dots

procedure

( slice-dots? v)Boolean

v:Any
The type of greedy, multiple-axis-preserving slice specifications, its singleton value, and predicate.

procedure

( ::new [dk])Slice-New-Axis

dk:Integer =1

procedure

( slice-new-axis? v)Boolean

v:Any
The type of slice specifications that indicate inserting a new axis, its constructor, predicate, and accessor. The axis length dk must be nonnegative.

top
up

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