In Fortran, any variable or expression is either scalar, or has a non-zero rank. In the second case we are dealing with an array--the rank is the number of dimensions to the array.
REAL A(5, 10), B(5, 10), C(5, 10)declares A, B and C as rank-2 arrays (of real numbers). Their extent in the first dimension is 5, and in the second dimension is 10. The vector of extents associated with an array is called the shape of the array. A, B and C all have shape . Two arrays are conformable if they have the same shape. Any of the built-in operations of Fortran 90 can be applied directly to conforming arrays. If A, B and C are declared as above,
A + B B * C A + (B * C)are legal expressions. Their results are arrays of the same shape as the operands.
B * Cstands for the table of values
C + Drepresents
Array expressions (and variables) can also be formed by taking a sections of a named array object. An array section is built from some subset of the elements of an array object--those associated with a selected subset of the index range attached to the object. The simplest array section is formed by subscripting an array by colon-separated range of index values. Suppose an array X is declared by
X(2:4)is a section containing the second, third and fourth elements of X. It is an array-valued expression of shape , and can be used in the same context as any other such expression. Either the lower bound or the upper bound can be omitted in the index range of a section, in which case they default to the lowest or highest values taken by the array's index. So
X(:)is a section containing the whole of X. This simple form of array section can be generalised with a ``stride''.
X(1:10:3)is an array section of size 4 containing every third element of X with indices between 1 and 10 (ie, indices 1, 4, 7, 10). Collectively ranges like
1:10:3, and the special cases
:, are all referred to as triplets.
For multi-dimensional arrays, some dimensions could be subscripted with a normal scalar expression, and some could be ``sectioned'' with triplets. The rank of the resulting array variable is the number of dimensions that are triplet-subscripted.
Naturally one can use array-valued expressions
in assignments. The simplest version has exactly the same syntax as
the scalar Fortran assignment. The expression on the right hand side
of the assignment must be conformable with the array variable on
the left hand side.
A special form of array assignment allows one to restrict the
assignment to some subset of the array variable on the left-hand-side
by specifying a mask. A mask is LOGICAL array expression,
conformable with the variable. Assignment of the
corresponding element of the array expression on the right-hand is only
performed where the mask takes value
.TRUE.. Where the mask
.FALSE., the variable is left unchanged. For example,
suppose EVEN is a logical array with indices from 1 to 10, whose
even-indexed elements are
.TRUE. and odd-indexed elements are
WHERE (EVEN) X (1:10) = X (2:11) ENDWHEREreplaces the values in the section
X(1:10)having even indices with copies of the values of the next element in the array.
The set of Fortran intrinsic functions was extended to include many array operations. Some notable inclusions are the array reduction functions including SUM and PRODUCT which add or multiply together elements of an array; MAXVAL and MINVAL which return largest or smallest elements of an array; and ANY and ALL, which take the disjunction and conjunction of the elements of a LOGICAL array. Other functions include the SPREAD function which ``adds an extra dimension'' to an array, returning an array in which values from the original array are replicated over the range of the new index; TRANSPOSE, which returns an array containing in the elements of its two dimensional argument, transposed; and shift operations which return arrays with values shifted by a constant index offset in one of the dimensions of the argument array.