Arrays

An array consists of a number of elements of the same type. An element is identified by the name of the array together with one or more indexes, one for each dimension of the array.

 

Declaration

INTEGER ar(0:9)
REAL beta(1:9,1:10)

ar is a one dimensional array of INTEGERs with index in the range from 0 to 9.

beta is a two dimensional array of REALs with first index in the range from 1 to 9 and second index in the range from 1 to 10.

A particular element of ar is represented by ar(expr) where expr is an integer expression. Similarly an element of the two dimensional array beta is represented by beta(expr1,expr2).

If the lower bound for an index is 1 (unity) then it may be omitted. For example:

REAL beta(9,10)

The rank of an array or scalar is the number of dimensions. A scalar has rank zero.

The shape of an array or scalar is a list of integers, one for each dimension, giving the number of elements along each dimension in turn. ar has shape (10). beta has shape (9,10). A scalar has shape ( ).

 

Assignment

Individual elements can be assigned values just like simple variables:

beta(3,4)=1.0

In addition, every element of an array can be assigned a particular value:

beta=0.0

One array can be copied, element by element, into another provided that the two have the same shape.

beta=gamma

Assignment is permissible if the array on the left is conformable to the object on the right. Two objects are conformable if they have the same shape or if one is a scalar.

Any intrinsic function that is classed as elemental can be used in an array expression to carry out assignment on an element by element basis:

REAL a(10),b(10),c(10)
 ! Values are assigned to the elements of a, b, and c
a=2.0*a+b*ABS(c)

 

Constructor

An array constructor is used to build an array from a list of values between "(/" and "/)". For example, given a REAL array x with shape (4):

x = (/ 1.4, 0.5, 1.0, 1.5 /)

All the components must have the same type and type attributes.

The rank of an array constructor is one. An intrinsic function called RESHAPE can be used to convert to rank greater than one.

 

WHERE statement and construct

Element by element array assignments (A = B) can be made conditional by using a WHERE statement or WHERE construct. The two forms are analogous to the IF statement and the IF construct.

The form of the WHERE statement is:

WHERE(mask_expression) array_assignment

For example:

WHERE (a>0.0) a=1.0/a ! a is a known REAL array

 

The form of the WHERE construct is:

WHERE(mask_expression)

  array_assignments

[ ELSEWHERE

  array_assignments ]

END WHERE

For example:

WHERE (b/=0)
  a=a/b ! a and b are known INTEGER arrays
ELSEWHERE
  a=0
END WHERE

The ELSEWHERE section is optional.

If the ELSEWHERE section consists of one WHERE construct, then ELSE and WHERE can be reduced to ELSEWHERE provided that one of the associated ENDWHEREs is deleted.

Like other constructs, WHERE constructs can be named. Optionally, the same name may be used after ELSEWHERE and after ELSEWHERE (mask_expression).

 

Intrinsic functions

SUM(a)   sums the elements an array.
DOT_PRODUCT(a,b)  forms an inner product.
MATMUL(a,b)   forms a matrix product.
MAXLOC(a)   returns the location of the largest value (also MINLOC).
MAXVAL(a)  returns the largest value (also MINVAL).

 

More�

 

The following more advanced topics are not described here:

Triplet notation

Vector subscripts

The FORALL construct

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited