a with elements of type
A is declared as:
Multidimensional arrays, with any number of dimensions, are declared as:
b:B[_,_]; // two-dimensional array c:C[_,_,_]; // three-dimensional array, etc
The rightmost dimension is the innermost (fastest moving) in the memory layout. Two-dimensional arrays that represent matrices are therefore in row-major order.
The size of the array may be given in the square brackets when it is declared, in place of
Alternatively, arrays can be initialized using a sequence of element values in square brackets:
a:Real[_] <- [0.0, 1.0, 2.0];
It is possible to nest these sequences to construct higher-dimensional arrays:
A:Real[_,_] <- [[1.0, 0.0], [0.0, 1.0]];
Arrays are sliced with square brackets. To select the element of
b at row 2 and column 6, use:
This returns a single element of type
B. To select the range of elements of
b at row 2 and columns 5 to 8, use:
This returns a vector of type
In the context of array slicing, the term index denotes a single index, as in
6 above; while the term range denotes a pair of indices separated by
.., as in
Indices reduce the number of dimensions in the result; they do not create singleton dimensions. Revisiting the previous example for emphasis, the result is of type
B[_] with size 4, not of type
B[_,_] with size 1 by 4. When a singleton dimension is desired, use a singleton range that starts and ends at the same index:
Arrays are resized by assignment, e.g.
a:A; d:A; d <- a;
d is now a copy of
a, with size 4. Its previous value is discarded.
When slicing an array on the left side of an assignment, suggesting a view of the existing array, sizes must match on the left and right:
d[1..2] <- a[1..2]; // OK! Both left and right have size 2 d[1..2] <- a; // ERROR! Left has size 2, right has size 4
Assignment may be used to resize an array, but not to change its number of dimensions. The number of dimensions of an array is a fundamental part of its type.
Sequences can be assigned to arrays:
x <- [a, b, c]; x <- [[a, b, c], [d, e, f]];
But arrays cannot be assigned to sequences, as sequences are read-only.