next up previous contents index
Next: Checking Subscripts Up: Multiarrays Previous: Some Semantic Issues   Contents   Index


One of the most interesting features of multiarrays is the possiblity of forming a section of a multiarray. This is an idea that will be familiar to Fortran programmers. A section (of some parent multiarray) is a multiarray in its own right whose elements are a subset of the elements of the parent. In other words it is a subarray.

A section expression is a generalized subscripting operation, in which the subscripts may select some range of the allowed indexes of the parent multiarray. A range of indices is represented by a term call a triplet. In HPJava a triplet has one of three forms:

$l$\verb$ : $$u$\verb$ : $$s$ \\

Here the symbols $l$, $u$ and $s$ stand for integer expressions. Any of the three kinds of triplet can appear as a subscript in a multiarray section expression. The first form, a single colon, means ``all allowed index values'' for the parent array. The second form means ``all index values between $l$ and $u$, inclusive'' (the set is empty if $u$ is less than $l$). In the third form $s$ is a step value. If $s$ is positive, the index range includes all values $l$, $l +
s$, $l + 2s$, ..., less than or equal to $u$. If $s$ is negative, the index range includes all values $l$, $l +
s$, $l + 2s$, ..., greater than or equal to $u$.

The subscripts of a multiarray section expression can be any mixture of triplet subscripts and scalar subscripts1.4. A scalar subscript is in an ordinary integer expression, just like the subscript in an array element reference. Double brackets rear their head again, mainly to make the distinction between sections and element references very clear. Here are some examples--we assume a is a rank-2 multiarray:

\begin{minipage}[t]{\linewidth}\small\begin{verbatim}a [[...
...[ :, 1]]
a [[0:3, : ]]
a [[0, 3]]\end{verbatim}\end{minipage}\end{displaymath}

The first section evaluates to a rank-1 multiarray, containing the first row of $a$. The second evaluates to a rank-1 multiarray containing the second column of $a$. The third evaluates to a rank-2 multiarray with extent 4 in the first dimension: it contains the first 4 rows of $a$. The last section evaluates to a rank-0 multiarray containing the single element $a [0, 3]$. Rank-0 multiarrays are slightly exotic animals with a few interesting uses, but we won't discuss them any more here.

A section is a normal expression in the HPJava language, and it can appear anywhere in an HPJava program that any other kind of multiarray-valued expression can appear. It is particularly common to see multiarray sections appearing as argument to methods. So, for example, this statement:

...i + B - 1, j : j + B - 1]], a, b) ;\end{verbatim}\end{minipage}\end{displaymath}

presumably assigns the matrix product of a and b to a B by B block of the elements of c, starting at position i, j.

The elements of an array section are variables; they are aliases (alternative names) for the elements of the parent array...if you modify an element of the section, then the corresponding element of the parent array is also changed. Note, however, that the array section expression is not itself a variable: an array section expression cannot appear on the left-hand-side of an assignment. So this:

\begin{minipage}[t]{\linewidth}\small\begin{verbatim}a [[:, :]] = b ; // ERROR\end{verbatim}\end{minipage}\end{displaymath}

is illegal (this is different to Fortran).

next up previous contents index
Next: Checking Subscripts Up: Multiarrays Previous: Some Semantic Issues   Contents   Index
Bryan Carpenter 2003-04-15