The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

Arrays


Arrays

Topics

Terminology

Arrays and elements

An array is a collection of (scalar) data, all of the same type, whose individual elements are arranged in a regular pattern.

Arrays properties

Example

Specifications

Examples

The following are equivalent:

INTEGER, DIMENSION(6) :: a

REAL, DIMENSION(0:9) :: b

LOGICAL, DIMENSION(2,2) :: yes_no

INTEGER :: a(6)

REAL :: b(0:9)

LOGICAL :: yes_no(2,2)

Further examples:

INTEGER, DIMENSION(8) :: x, y

REAL :: alpha(1:3), beta(4:9)

REAL, DIMENSION(0:5,12:45,6) :: data

CHARACTER(len=10) :: names(25)

TYPE(point)

REAL :: position(3)

END TYPE(point)

TYPE(point) :: object(10)

Array sections

General

Individual elements and sections of an array are uniquely identified through subscripts (one per rank).

Each array subscript is either:

array( index [...] )

Elements

REAL, DIMENSION(8) :: a

INTEGER, DIMENSION(5,4) :: b

Sections

REAL, DIMENSION(8) :: a

INTEGER, DIMENSION(5,4) :: b

Vector subscripts

(/ list /)

REAL, DIMENSION(9) :: a

INTEGER, DIMENSION(3) :: random

...

random = (/6,3,8/)

a( random ) = 0.0

a( (/7,8,9/) ) =

1.2

REAL, DIMENSION(5) :: a

INTEGER, DIMENSION(3) :: list

...

list = (/2,3,2/)

a(list) = (/ 1.1,1.2,1.3 /) !illegal

Array storage

REAL, DIMENSION(3,5) :: a

Array assignment

Whole array assignment

REAL, DIMENSION(100) :: a, b, c

REAL :: d(10,10)=0.0

...

b = 2*a+4 !array expression

a = 2.0 !scalar broadcast

c = b*a !element*element

...

c = d !illegal

Array section assignment

REAL, DIMENSION(10) :: alpha, beta

REAL :: gamma(20)

...

alpha(1:5) = 2.0

alpha(1:10:2) = beta(1:5)/6

alpha(2:10) = alpha(1:9)

...

gamma(11:20) = beta

Elemental intrinsic procedures

REAL :: num

REAL, DIMENSION(3,3) :: a

INTEGER :: length(5)

CHARACTER(len=10) :: c(5)

...

x = SQRT( num ) !single variable

a = SQRT( a ) !all elements

...

!find string length for each element

!no trailing blanks

length = LEN( TRIM( c ) )

Zero-sized arrays

INTEGER :: a(5)=(/1,2,1,1,3/)

...

a( 1:COUNT(a==1) ) = 0

!new a(0,0,0,1,3)

...

a( 1:COUNT(a==4) ) = 0

!unchanged a(0,0,0,1,3)

Initialising arrays

Constructors

Reshape

DATA statement

DATA variable / list /...

INTEGER :: a(4), b(2,2), c(10)

...

DATA a/4,3,2,1/ !by value

...

DATA a/4*0/ !whole array

...

DATA b(1,:)/0,0/ !by section

DATA b(2,:)/1,1/

...

DATA (c(i),i=1,10,2)/5*1/

DATA (c(i),i=2,10,2)/5*2/

WHERE

Statement

The WHERE statement allows a single array assignment only if a logical condition is true.

WHERE( condition ) statement

INTEGER :: a(2,3,4)

...

WHERE( a<0 ) a=0

WHERE( 3*a>10 ) a=99

Construct

The WHERE construct allows array assignment(s) only if a logical condition is true, and alternative array assignment(s) if false.

WHERE( condition )

block1

[ELSEWHERE

block2]

ENDWHERE

INTEGER :: b(8,8)

WHERE( b<=0 )

b = 0

ELSEWHERE

b = 1/b

ENDWHERE

Array intrinsic functions

List

Example of reduction

ALL( condition, [DIM] )

LOGICAL :: test, test2(2), test3(3)

REAL, DIMENSION(3,2) :: a

DATA a/5,9,6,10,8,12/

...

test = ALL( a>5 ) !false

test2 = ALL( a>5, DIM=1 ) !f,t,t

test3 = ALL( a>5, DIM=2 ) !f,t

Example of inquiry

SIZE( array, [DIM] )

REAL, DIMENSION(3,2) :: a

...

num = SIZE( a ) !num=6

num = SIZE( a, DIM=1 ) !num=2

num = SIZE( a, DIM=2 ) !num=3

Example of construction

SPREAD( array, DIM, NCOPIES )

REAL, DIMENSION(3) :: a=(/2,3,4/)

REAL, DIMENSION(3,3) :: b, c

...

b = SPREAD( a, DIM=1, NCOPIES=3 )

c = SPREAD( a, DIM=2, NCOPIES=3 )

Example of location

MAXLOC( array [, MASK] )

REAL :: a(5)

a = (/2.0,8.0,5.0,3.0,4.0/)

...

num = MAXLOC( a ) !num=2

num = MAXLOC( a, MASK=a<5 ) !num=5

num = MAXLOC( a(2:4) ) !num=1

List


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker