Yorick: Dimension Lists

Home Manual Packages Global Index Keywords Quick Reference |
## 2.7 Dimension Lists You should strive to write Yorick programs in such a way that you never need to refer to the lengths of array dimensions. Array dimensions are usually of no direct significance in a calculation; your programs will tend to be clearer if only the arrays themselves appear. In practice, unfortunately, you can't always get by without mentioning dimension lengths. Two functions are important: `numberof(x)` returns the total number of items in the array `x`. `dimsof(x)` returns a list consisting of the number of dimensions of the array `x`, followed by the length of each of those dimensions. Thus, if `x` were a nine-by-two-by-six array, `dimsof(x)` would return `[3,9,2,6]`, while if `x` were a scalar value, `dimsof(x)` would return `[0]`. `dimsof(x, y, z, ...)` With multiple arguments, the `dimsof` function returns the dimension list of the array `x+y+z+...`, or `[]` if the input arrays are not conformable. The `array` function (see section 2.1 Creating Arrays), and the more arcane functions `add_variable`, `add_member`, and `reshape` all have parameter lists ending with one or more "dimension list" parameters. Each parameter in a dimension list can be either a scalar integer value, representing the length of a single dimension, or a list of integers in the format returned by `dimsof` to represent zero or more dimensions. Several arguments can be used to build up a complicated dimension list:
    x1 = array(0.0, 9, 2, 6);     /* 9-by-2-by-6 array of 0.0 */
    x2 = array(0.0, [3,9,2,6]);  /* another 9-by-2-by-6 array */
    x3 = array(0.0, 9, [0], [2,2,6]);   /* ...and yet another */

    flux= array(0.0, 3, dimsof(z), numberof(groups));
In the final example, the `flux` might represent the three components of a flux vector, at each of a number of positions `z`, and for each of a number of photon energies `groups`. The first dimension of `flux` has length three, corresponding to the three components of each flux vector. The last dimension has the same length as the `groups` array. In between are zero or more dimensions -- whatever the dimensions of the array of positions `z`. By using the rubber index syntax (see section 2.3.7 Using a rubber index), you can extract meaningful slices of the `flux` array without ever needing to know how many dimensions `z` had, let alone their lengths.