LehreWiki

7 Arrays

7.1 Arrays

Arrays represent a data structure for which every element has the qualities of variables of corresponding type. The same rules apply to the type declaration as for simple variables while the DIMENSION attribute has to be set. An array may have up to 7 dimensions. An array is accessed by its name; an element of the array or a sub-array is addressed by its indices (one for each dimension). The number of elements for each dimension can be specified by upper and lower boundary of the index range or by the total number of elements. An on-dimensional array is called 'vector'.

7.1.1 Declaration of arrays

Usually the declaration of arrays is carried out in the context of the type dimention while the attribute DIMENSION and the size have to be specified. If only the number of elements (N) is specified, the index range is set to 1:N

''type'', DIMENSION (d,,1,,[,...,d,,n,,]) <''attribute''>

array_name [= initial_value] [, ...]
type :: array_name (d1[,...,dn]) [= initial_value] [, ...]
type data type
d''i'' size of the i-th dimension or index-range bi1:bi2 or number of elements
b''i1'': smallest index of a dimension or blank
b''i2'': largest index of a dimension or blank
attribute s. type declaration
array_name list of names
initial_value initial values for array elements

Arrays may be declared with the DIMENSION statement (non-executable statement, FORTRAN77):

In this case the type of the array is defined by the array's name.

Please see Example 1

Please see Example 2

Please do Tasks 7.1 and 7.2

7.1.2 Properties of arrays

The elements of an array are located sequentially in the memory. For multidimensional arrays the array elements are ordered in a way that the index of the first dimension is increased fastest.

•  size

total number of elements

•  dimension

number of elements in a dimension

•  rank

number of dimensions

•  shape

rank and size of the individual dimensions.
The shape is specified in a shape-vector. It represents an one-dimensional array (vector). The number of elements corresponds to the rank while the value of each vector element is equal to the size of the corresponding dimension.

Please see Example 3

7.1.3 Array constant

FORTRAN95 permits the use of array constants. They represent an one-dimensional array (vector) and is specified by a list of values enclosed by (/ and /) marks. The elements of this vector can be defined by a list of values, by an one-dimensional (sub)-array or by implicit loops.

7.1.3 Array constructor

An array constructor represents an vector with elements other than constants. Thus the (list of) elements are derived from other variables or are calculated by use if implicit loops enclosed by (/ and /). The list of vector elements may be calculated from the implicit loop of form:

(expression, loop_variable=start_values,end_value [,increment])

Array constructors may contain nested loops.

Please see Example 4

7.1.4 Initialisation of arrays

Arrays may be initialised during the type declaration similar to the type declaration of variables. If the initialisation value is a scalar, all array elements are initialised with that value.
A direct initialisation can be carried out only for one-dimensional arrays (vectors):

REAL, DIMENSION (/ list of values or implicit list /)

For initialisation of multidimensional arrays with varying values a direct initialisation can be carried out by use of an array constructor (similar to a array constant s. above) in connection with the intrinsic function reshape:

REAL, DIMENSION (n,m) :: B = reshape( (/ list of values or implicit list /), (/ N, M/))

The function reshape reorganises the elements of a vector into a multidimensional array.
The assignment of the vector elements to the field elements is carried out according to their order in the memory.

Please see Example 5

Please do Tasks 7.3, 7.4, and 7.5

7.2 Order of array elements

The elements of an array are located sequentially in the memory. For multidimensional arrays the array elements are ordered in the way that the index of the first dimension increases fastest.

Please see Example 6

Please see Example 7

Please do Task 7.6

7.3 Access to arrays and array elements

FORTAN 95 permits the access to the whole arrays, sub-arrays and single array elements.

7.3.1 Access to array element

The access to an array element is carried out directly by specifying the elements indices :

FIELD (INDEX1 [, INDEX2, ..., INDEX7 ])

7.3.2 Access to a whole array

The access to the complete array is carried out by referencing the field name without any indices. The sequence of elements (e.g. when I/O-operations are carried out) is the same as they are stored in the memory.

Please see Example 8

7.4 Sub arrays

Sub arrays are defined by a set of indices for every component of the complete field. A set of indices can be specified by:

Index area

Addressed elements

Example
( Feld (6,6) )

Integer value

K

Elements with specified index

FELD (5,:)

Index range

M:N

Elements with index between m and n

FELD (6,3:5)

Index range with increment

M:N:K

elements with index
M + (I-1)*K for K >= 0
as long as <= N

FELD (2,3:5:2)

All elements of a dimension

:

All Elements

FELD (:,1:4)

Index array

IFELD

All elements, with index specified in array IFELD

FELD (3,IFELD)

Table 1. Definition of sub arrays

Please see Example 9

Please see Example 10

7.5 Operation with arrays or sub arrays

Arrays or sub arrays can be operands in expressions - provided all operands have the same shape (or are scalar values). Arrays and sub arrays can be used in the same way. An expression can be assigned to an array of the shape.
Arrays mayalso be arguments in function calls. Many intrinsic functions (e.g. sin()) allow array arguments (and apply for each element of the array).

Please see Example 11

Please do Tasks 7.7 and 7.8

7.6 Allocatable arrays

Allocatable arrays represent arrays for which the size must not be specified during compilation time. Their size can be set during runtime. Only the data type as well as the rank (number of dimensions) must be declared in a type declaration statement.
Allocatable arrays must have the attribute ALLOCATABLE. The rank is specified by a ':' (for each requested dimensions) in the type declaration statement.

type, DIMENSION, ALLOCABLE (:[,: ...]) <attribute> :: array_name [, ...]
type, ALLOCATABLE :: array_name (:[,: ...]) [, ...]
type data type
: specification for one dimension
attribute s. Type declaration
array_name List of names

Please see Example 12

7.5 Operation with arrays or sub arrays

Arrays or sub arrays can be operands in expressions - provided all operands have the same shape (or are scalar values). Arrays and sub arrays can be used in the same way. An expression can be assigned to an array of the shape.
Arrays mayalso be arguments in function calls. Many intrinsic functions (e.g. sin()) allow array arguments (and apply for each element of the array).

Please see Example 11

Please do Tasks 7.7 and 7.8

7.6 Allocatable arrays

Allocatable arrays represent arrays for which the size must not be specified during compilation time. Their size can be set during runtime. Only the data type as well as the rank (number of dimensions) must be declared in a type declaration statement.
Allocatable arrays must have the attribute ALLOCATABLE. The rank is specified by a ':' (for each requested dimensions) in the type declaration statement.

type, DIMENSION, ALLOCABLE (:[,: ...]) <attribute> :: array_name [, ...]
type, ALLOCATABLE :: array_name (:[,: ...]) [, ...]
type data type
: specification for one dimension
attribute s. Type declaration
array_name List of names

Please see Example 12

7.6.1 Allocating arrays

The ALLOCATE-statement assigns memory space to allocatable array by specifying the current size of the array.
General form:

The function allocated (fname) (s. below.) returns the value .TRUE. if the array fname is allocated.
The statement deallocate (fname1 [, fname2 ,...]) releases the memory allocated to the arrays.

Please see Example 13

Please see Task 7.9

7.7 Intrinsic Functions for array operation

FORTRAN95 provides a number of intrinsic functions for working with arrays.
Usage of these function may be rather complex in some cases. More detail information is available in the manuals.

Function

Parameter

Return value

Meaning

allocated

ARRAY

Logical (.TRUE. or .FALSE.)

test if array is already allocated

lbound

ARRAY [,DIM]

Integer array of dimension nb or. scalar

returns the lower boundary of the index range for array dimensions or of a single dimension of the array

shape

ARRAY

Integer, vector with nb components

returns the shape vector of the array

size

ARRAY [,DIM]

Integer

returns the number of elements of array or of a single dimension of array

ubound

ARRAY [,DIM]

Integer array of dimension nb or. scalar

returns the upper boundary of the index range for array dimensions or of a single dimension of the array

merge

ARRAY1, ARRAY2, MASK

array of dimension of ARRAY1

merges elements of two arrays depending on the (logical) value of Mask

pack

ARRAY, MASK [,ARRAY]

vector

returns a vector containing the elements of array

spread

ARRAY, DIM, ncop

array of dimension nb+1

extends the rank of array by one extends the rank of array by one

unpack

VECTOR, MASK, ARRAY

array of dimension of MASK

return an array of shape of MASK with elements from VECTOR

reshape

ARRAY, shape, [,pad] [,order]

array of dimension specified by SHAPE

returns an array of shape with elements values provided by array

cshift

ARRAY, shift [,DIM]

array of dimension of ARRAY

cycling of array elements

eoshift

ARRAY, shift [,bound] [,DIM]

array of dimension of ARRAY

shifting of elements with inserting given values into the gaps at the boundaries

transpose

ARRAY

2-dimensional array

returns the transpose of array

maxloc

ARRAY [,MASK]

Integer array of dimension nb or scalar

returns the position of the maximum value

minloc

ARRAY [,MASK]

Integer array of dimension nb or scalar

returns the position of the minimum value

all

MASK [,DIM]

Logical

returns .TRUE. if all elements of MASK have value .TRUE.

any

MASK [,DIM]

Logical

returns .TRUE. if at least one element of MASK has value .TRUE.

count

MASK [,DIM]

Integer array of dimension nb or scalar

counts the number of with value .TRUE. in array or in one dimension

maxval

ARRAY [,DIM] [,MASK]

array of dimension nb or . scalar

returns the maximum value found in array or in one dimension

minval

ARRAY [,DIM] [,MASK}

array of dimension nb or . scalar

returns the maximum value found in array or in one dimension

product

ARRAY [,DIM] [,MASK}

array of dimension nb or . scalar

returns the product of elements in array or in one dimension

sum

ARRAY [DIM] [MASK]

array of dimension n or . scalar

returns the sum of elements in array or in one dimension

matmul

ARRAY_A, ARRAY_B

Array (2-dim)

Matrix multiplication

Table 2. Intrinsic functions for arrays operations and inquiry.

7.7.1 Applying internal Functions

Some internal function working with array have an (optional) mask-argument. It permits to control whether an array element is considered by the function or whether it is ignored. This mask is represented by a logical array of the same shape as the data array provided for the function. In case a mask is applied only these array elements are considered which the corresponding mask elements have the value .true.

Please see Example 14

Please see Example 15

Please see Example 16

Please do Tasks 7.10, 7.11, and 7.12

LehreWiki: Chapter_07 (last edited 2010-03-31 08:17:27 by HansLuthardt)