Arrays are heterogeneous data structures whose elements are indexed by integer sequences of fixed length. The length of a valid index sequence is the rank or the number of dimensions of an array. The shape of an array consists of bounds for each index.
The lower bound b
and the upper bound e
of a dimension are
exact integers with (<=
. A valid index along the
dimension is an exact integer b
e
)k
that satisfies both
(<=
and b
k
)(<
.
The length of the array along the dimension is the difference
k
e
)(-
.
The size of an array is the product of the lengths of its dimensions.e
b
)
A shape is specified as an even number of exact integers. These are alternately the lower and upper bounds for the dimensions of an array.
Returns a shape. The sequence
bound
... must consist of an even number of exact integers that are pairwise not decreasing. Each pair gives the lower and upper bound of a dimension. If the shape is used to specify the dimensions of an array andbound
... is the sequenceb0
e0
...bk
ek
... ofn
pairs of bounds, then a valid index to the array is any sequencej0
...jk
... ofn
exact integers where eachjk
satisfies(<=
andbk
jk
)(<
.jk
ek
)The shape of a
d
-dimensional array is ad
* 2 array where the element atk 0
contains the lower bound for an index along dimensionk
and the element atk 1
contains the corresponding upper bound, wherek
satisfies(<= 0
andk
)(<
.k
d
)
Function: make-array
shape
obj
Returns a newly allocated array whose shape is given by
shape
. Ifobj
is provided, then each element is initialized to it. Otherwise the initial contents of each element is unspecified. The array does not retain a reference toshape
.
Returns a new array whose shape is given by
shape
and the initial contents of the elements areobj
... in row major order. The array does not retain a reference toshape
.
Returns the number of dimensions of
array
.(array-rank (make-array (shape 1 2 3 4)))Returns 2.
Function: array-ref
array
k
...
Function: array-ref
array
index
Returns the contents of the element of
array
at indexk
.... The sequencek
... must be a valid index toarray
. In the second form,index
must be either a vector or a 0-based 1-dimensional array containingk
....(array-ref (array (shape 0 2 0 3) 'uno 'dos 'tres 'cuatro 'cinco 'seis) 1 0)Returns
cuatro
.(let ((a (array (shape 4 7 1 2) 3 1 4))) (list (array-ref a 4 1) (array-ref a (vector 5 1)) (array-ref a (array (shape 0 2) 6 1))))Returns
(3 1 4)
.
Function: array-set!
array
k
...
obj
Function: array-set!
array
index
obj
Stores
obj
in the element ofarray
at indexk
.... Returns the void value. The sequencek
... must be a valid index toarray
. In the second form,index
must be either a vector or a 0-based 1-dimensional array containingk
....(let ((a (make-array (shape 4 5 4 5 4 5)))) (array-set! a 4 4 4 "huuhkaja") (array-ref a 4 4 4))Returns
"huuhkaja"
.
Function: share-array
array
shape
proc
Returns a new array of
shape
shape that shares elements ofarray
throughproc
. The procedureproc
must implement an affine function that returns indices ofarray
when given indices of the array returned byshare-array
. The array does not retain a reference toshape
.(define i_4 (let* ((i (make-array (shape 0 4 0 4) 0)) (d (share-array i (shape 0 4) (lambda (k) (values k k))))) (do ((k 0 (+ k 1))) ((= k 4)) (array-set! d k 1)) i))Note: the affinity requirement for
proc
means that each value must be a sum of multiples of the arguments passed toproc
, plus a constant.Implementation note: arrays have to maintain an internal index mapping from indices
k1
...kd
to a single index into a backing vector; the composition of this mapping andproc
can be recognised as(
by setting each index in turn to 1 and others to 0, and all to 0 for the constant term; the composition can then be compiled away, together with any complexity that the user introduced in their procedure.+ n0
(*n1
k1
) ... (*nd
kd
))
Multi-dimensional arrays are specified
by SRFI-25.
In Kawa, a one-dimensional array whose lower bound is 0 is also a sequence.
Furthermore, if such an array is simple (not created share-array
)
it will be implemented using a <vector>
.
Uniform vectors and strings are also arrays in Kawa.
For example:
(share-array (f64vector 1.0 2.0 3.0 4.0 5.0 6.0) (shape 0 2 0 3) (lambda (i j) (+ (* 2 i) j)))
evaluates to a two-dimensionsal array of <double>
:
#2a((1.0 2.0 3.0) (3.0 4.0 5.0))