Basic_comphy_ch4

Basic_comphy_ch4 - Ch. 4 Fortran 90: Array References: 1....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 Ch. 4 Fortran 90: Array References: 1. T.M.R. Ellis et al., “ Fortran 90 programming” , Chs. 7, 13 2. L. Nyhoff & S. Leestma, “ Introduction to FORTRAN 90” , Ch. 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    2 Outline 4.1 The array concept 4.2 Array initialization 4.3 Input/Output with arrays 4.4 Arrays in computation 4.5 Allocatable arrays 4.7 Multidimensional arrays 4.8 Vectors & matrices
Background image of page 2
    3 4.1 The array concept * In scientific computing, it is very common to manipulate ordered sets of values: Examples: Vectors v = v 1 i v 2 j v 3 k = v 1 , v 2 , v 3 Matrix A = { A 11 A 12 A 21 A 22 } B = { B 11 B 12 B 13 B 21 B 22 B 23 B 31 B 32 B 33 } * the elements (or components) all have the same name, but different subscript index. * Fortran provides the concept of array to deal with an ordered set of related variables. In Fortran, we use (for a one-dimensional array): v (1), v (2) , . ..., v(n) name of the array subscript expression (integer enclosed in parenthese)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    4 Array declarations Type , DIMENSION( n ) :: array_1, array_2, array_3,. ... Type = data type (eg, REAL , INTEGER ...) n (integer) = number of elements of each (one-dimensional) array in the list Examples: REAL, DIMENSION(3) :: v (an array having 3 elements) REAL, DIMENSION(10) :: a, b, c (each array has 10 elements) REAL :: a(10), b(10), c(10) (equivalent to above) REAL, DIMENSION(10) :: a, b, c(20), d ( a , b , d have 10 elements each; c has 20 elements) INTEGER, DIMENSION(20) :: m, n (each array has 20 elements (integer)) Remark: We shall discuss multi-dimensional array later.
Background image of page 4
    5 * By default, the subscripts will start at 1: REAL, DIMENSION(n) :: v => the elements are: v(1), v(2), v(3),. ..v(n) REAL, DIMENSION(-1:1) :: v (elements = v(-1) , v(0) , v(1) ) REAL, DIMENSION(0:2) :: v (elements = v(0) , v(1) , v(2) ) REAL, DIMENSION(-3:-1) :: v (elements = v(-3) , v(-2) , v(-1) ) Examples:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    6 4.2 Array initialization * An array consists of a number of elements, it is necessary to provide values for each of these elements. REAL, DIMENSION(3) :: v REAL, DIMENSION (-2:0) :: a v(1) = 0.1 ; v(2) = 0.25 ; v(3) = 1.2 a(-2) = 0.12 ; a(-1) = 0.3 ; a(0) = 1.3 Examples: REAL, DIMENSION(10) :: a INTEGER, DIMENSION(10) :: b DO i =1, 10 a(i) = REAL(i) b(i) = i END DO (one-by-one) (via a DO loop)
Background image of page 6
    7 array = (/ val_1 , val_2, . .., val_n /) Array constructor Example: REAL, DIMENSION(3) :: a INTEGER, DIMENSION(10) :: b a = (/ 1.2, 3.4, 2.5 /) b = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /) Note: Number of values must be equal to the number of elements of the array Question: What if the array has a large size (eg, 1000 elements)?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    8 (var_list, count = initial, final, step ) Recall: count-controlled DO loop DO count = initial , final , step statement-sequence END DO Implied DO loop var_list : Body of the DO loop (list of variables or expressions) Example:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/21/2010 for the course PHYSICS 2351 taught by Professor Drlinlapming during the Spring '10 term at CUHK.

Page1 / 47

Basic_comphy_ch4 - Ch. 4 Fortran 90: Array References: 1....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online