4.2 Array Sections

next up previous
Next: 4.3 Dynamic Arrays Up: 4 Data Parallelism Previous: 4.1 Array Operations

4.2 Array Sections


A portion of an array containing more than one element is called an array section. Often an array operation is needed on an array section, not the entire array. The earlier example of normalizing the pivot row of a matrix is a case in point. In this example it was exactly one row of the matrix that was of interest in the computation, not the whole array. In this case the array section is one row of a two-dimensional array; in general virtually any rectilinear subset of an array can be an array section and hence an object that can be used in array operations. An array section may be of any dimensionality up to and including the dimensionality of the array on which the section is being defined.

An array element, which is a scalar, is of course specified by the array name and a subscript value for each dimension. The general form for this is the familiar

array-name ( subscript-1, subscript-2, subscript-3, ...)
where the number of subscripts is the dimensionality of the array and each subscript is a scalar integer expression, or scalar subscript for short. An array section is specified by replacing at least one scalar subscript by a ``section subscript". A section subscript is a sequence of scalar subscript values for that dimension, and thus a section subscript may be thought of (and constructed as) a one-dimensional array of subscript values, called a vector subscript. If (only) one scalar subscript is replaced by a vector subscript the result is a one-dimensional array section; if two scalar subscripts are replaced by vector subscripts the result is a two-dimensional array section, and so on. An array section has dimensionality equal to the number of vector subscripts it has.

It's time for an example. Consider the 5x6 array as shown.Three sections of are shown in bold: the entire second column (a one- dimensional section), the 2x2 upper right hand corner of (a two-dimensional section), and the last half of the fifth row of (a one-dimensional section).

Note that all of the vector subscripts in these examples could be written with implied-do constructs:

The implied-do form is more extensible and, for large sections, considerable more compact than explicit lists. Implied-do constructs are also useful for regularly-spaced but noncontiguous vector subscripts. For example,

Q((/(k,k=1,5,2)/),2) = Q((/1,3,5/),2) = (/11,45,56/)
The implied-do form is common enough that a more readable shorthand notation, called a ``triplet subscript", is also provided for the indexed-do control triplet.

A triplet subscript is just the indexed-do control values, separated by colons rather than commas, with the last one (the increment or stride value) optional. Thus using triplet notation the above four examples may be written (much more clearly!) as:

A further simplification is provided in that if the initial triplet value is omitted the lower bound of that dimension is assumed, and if the second triplet value is omitted the upper bound of that dimension is assumed. Thus the most compact way of expressing these four sections is:

The form Q(:,:) is a section that is in fact the entire array , which explains an example early in this section. (Note that Q(:,:) also has the form of an assumed-shape dummy argument declaration; there is no ambiguity, however, because if this notation appears in an array expression it always represents an array section.)

Returning to the more general form of vector subscripts, though the above examples employ array constructors, any one-dimensional array expression is permitted. The only requirement is that the value of each element of the vector subscript be a valid subscript value for that dimension. A common form for vector subscripts is a one-dimensional integer array name (or section), whose element values have been previously established. This form is extremely useful for indirect access, such as indexing into a table; e.g., table elements may be retrieved (or set) by subscripting the table array with an array containing the desired table index values.

A couple of final examples will complete this introduction to array sections. Again, for purposes of explicitness, array constructors will be used, but in practice more concise one-dimensional array names or sections are more likely. First, an array section need not be as easily depictable graphically as the examples above. For the array defined above, consider the section

This represents the array section

This section can be used in any array expression in which a 3x2 array object is valid. It may also appear on the left hand side of an array assignment, in which case the (1,1) element of the right hand side expression value gets assigned to , the (3,2) value of the right hand side gets assigned to , and so on.

A vector subscript may contain more elements than the size of that array dimension. In this case there are duplicate values, since all of the values must be within the array dimension range. Indeed, subscript values may be duplicated in a vector subscript even if the size of the vector is less than the array dimension (the only requirement is that the subscript values must be within range). Both of these cases are illustrated in the following example, which specifies a 7x4 section from the elements of .

Note that rows one and five of this section are identical, as are rows three and six and columns two and three. Many elements of therefore appear twice in this array section and two elements, and , each appear four times. Array sections with multiple appearances of a given parent array element are perfectly legitimate array operands in array expressions, but such sections must not appear on the left hand side of array assignments.

next up previous
Next: 4.3 Dynamic Arrays Up: 4 Data Parallelism Previous: 4.1 Array Operations