38
CHAPTER 9 MUTIDIMENSIONAL ARRAYS

CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Embed Size (px)

Citation preview

Page 1: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

CHAPTER 9 MUTIDIMENSIONAL ARRAYS

Page 2: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Introduction to multidimensional Arrays and Multiply subscripted variables

Page 3: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Compile-Time Arrays & Run-Time Arrays

Compile-Time Arrays: The size is fixed before execution begins.Run-Time (or Allocatable 可分配的 ) Arrays: The memory is allocated ( 分配 ) during execution, making it possible to allocate an array of appropriate size.

Page 4: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Compile-Time Arrays

REAL, DIMENSION(4, 3)::Temperature REAL, DIMENSION(1:4, 1:3):: Temperature Temperature (2, 3 ) 64.5 Temperature (I, J)

REAL, DIMENSION(4, 3, 7):: TemperatureArray REAL, DIMENSION(1:4, 1:3, 1:7)::TemperatureArray TemperatureArray (1, 3, 2) → 64.3 TemperatureArray (Time, Location, Day)

Page 5: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Compile-Time Arrays

REAL, DIMENSION(1:2, -1:3)::Gamma Gamma(1, -1), Gamma(1,0),

Gamma(1,1), Gamma(1, 2), Gamma(1,3), Gamma(2, -1), Gamma(2,0), Gamma(2,1), Gamma(2, 2), Gamma(2,3)

REAL, DIMENSION (0:2, 0:3, 1:2) :: Beta INTEGER, DIMENSION(5:12) :: Kappa

Page 6: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Declaration of Compile-Time Array

type, DIMENSION(l1:u1, l2:u2 , ‧ ‧ ‧ lk:uk) :: & list-of-array-names

li:ui

The specified lower limit li through the upper limit ui.

The number k of dimensions, called the rank ( 秩 ) of array, is at most seven.

Page 7: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Declaration of Allocatable Array

type, DIMENSION(:, : , ‧ ‧ ‧ :), &

ALLOCATABLE :: listtype, DIMENSION(:, : , ‧ ‧ ‧ :) :: list

ALLOCATABLE :: list

The rank k of the array (the number of dimensions) is at most seven.

Page 8: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Allocatable Array ( 可分配的 ) / Run-Time Arrays

REAL, DIMENSION(:, :, :), ALLOCATABLE :: &

Beta

REAL, DIMENSION(:, :), ALLOCATABLE :: & Gamma

Page 9: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

ALLOCATE StatementALLOCA TE (list)ALLOCATE (list, STAT = status-variable)

where list is a list of array specifications of the form array-name (l1:u1, l2:u2 , ‧ ‧ ‧ lk:uk)

ALLOCATE (Beta(0:2, 0:3, 1:2), Gamma & (1:N, -1:3), STAT = AllocateSatus)

DEALLOCATE(***)

Page 10: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output of Multidimensional Arrays

Element-wise Processing row ( 列 ) × column ( 行 )

Two natural orders for processing the elements of a two-dimensional array: row-wise and column-wise.In most cases, a programmer can select one of these orderings by controlling the way the subscripts ( 下標 ) vary. If this is not done, the Fortran convention is that two-dimensional arrays will be processed column-wise.

Page 11: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

(a) Row-wise Processing (b) Column-wise Processing

Page 12: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Processing a Three-Dimensional Array

(2×4×3)

Page 13: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output of Array Elements

Using a DO loop Using the array name Using an implied DO loop Using an array section

INTEGER, DIMENSION (3, 4) :: Table

33

46

25

77

100

32

89

10

56

48

99

77

Page 14: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using DO Loops

INTEGER, DIMENSION (3, 4) :: TableDO Row = 1, 3

DO Col =1, 4READ *, Table (Row, Col)

END DO END DO

Page 15: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using DO LoopsINTEGER, DIMENSION (3, 4) :: Table

DO Col =1, 4DO Row = 1, 3

READ *, Table (Row, Col)END DO

END DO

DO Row = 1, 3DO Col =1, 4

PRINT *, Table (Row, Col)END DO

END DO

Page 16: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using the Array Name

INTEGER, DIMENSION (3, 4) :: Table READ *, Table

77, 99, 48, 56, 10, 8932, 100, 77, 25, 46, 33

PRINT ‘(1X, 4I5/)’ Table

33

46

25

77

100

32

89

10

56

48

99

77

Page 17: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using Implied DO Loops

INTEGER, DIMENSION (3, 4) :: TableREAD *, ((Table (Row, Col), Col =1, 4 ), & Row = 1, 3)

READ *, (Table (Row,1), Table (Row,2), & Table (Row,3), Table (Row,4), & Row = 1, 3)

Page 18: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using Implied DO Loops

READ *, ((Table (Row, Col), Row = 1, 3) ), &

Col =1, 4)

READ *, (((B(I, J, K), I = 1, 2), J =1, 4), & K = 1, 3)

Page 19: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Input/Output Using Implied DO Loops

DO Row = 1, 3 PRINT ‘(1X, 4I5)’ , (Table (Row, Col), Col

=1, 4)END DO

33

46

25

77

100

32

89

10

56

48

99

77

Page 20: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Examples Figure 9.3, p.628

Temperature TableRate is a 3 × 4 array

0.0

5.3

5.6

0.0

0.1

4.18

9.16

0.1

3.7

2.18

0.0

1.16

Page 21: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Examples: p. 630READ *, N, (Number (I), I =1, N), M, &

((Rate (I,J), J = 1, N), I = 1, M)

4 16, 37, 76, 23 3 16.1, 7.3, 18.4, 6.5 0.0, 1.0, 1.0, 3.5 18.2, 16.9, 0.0, 0.0

Page 22: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Examples: p. 630

PRINT 5, (“Row”, I, (Rate (I,J), J= 1, 4), I = 1, 3)

5 FORMAT (1X, A, I2, “--”, 4F6.1/)

Row 1-- 16.1 7.3 18.4 6.5_________________________Row 2-- 0.0 1.0 1.0 3.5_________________________Row 3-- 18.2 16.9 0.0 0.0_________________________

Page 23: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Examples: p. 630PRINT 6, (J, (Rate (I,J), I = 1, 3), & Number (J), J= 1, 4), “Total”, Total6 FORMAT (4(1X, I4, 5X, 3F6.1, I10/), A, T35,

I3)

1 16.1 0.0 18.2 16 2 7.3 1.0 16.9 37 3 18.4 1.0 0.0 76 4 6.5 3.5 0.0 23Total 152

Page 24: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

9.3 Processing Multidimensional Arrays

Array ConstantsINTEGER, DIMENSION (2, 3) :: AA = RESHAPE ((/ 11, 22, 33, 44, 55, 66 /), (/ 2, 3 /))orA = RESHAPE ((/ (11*N, N =1, 6) /), (/ 2, 3 /) Reshape (v.) 重塑 Shape (n. v.) 形狀

66

55

44

33

22

11

Page 25: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Constants

A = RESHAPE ((/11, 22, 33, 44, 55, 66 /), & (/ 2, 3 /), ORDER = (/2, 1/))

The order (/2, 1/) specifies that the second subscript ( 下標 ) is to be varied before the first, which causes the array to be filled row-wise.

66

33

55

22

44

11

Page 26: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Constants

A = RESHAPE ((/11, 22, 33, 44 /), (/ 2, 3 /), & PAD = (/0, 0/), ORDER = (/2, 1))

pad (v.) 填充

0

33

0

22

44

11

Page 27: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Constants

The intrinsic function SHAPE can be used to determine the shape of an array, which consists of number of dimensions for array and the extent (the number of subscripts 下標之大小程度 ) in each dimension.For example, SHAPE (A) will return (2, 3).

Shape 形狀 (n.); 塑造 (v.)

Page 28: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Expressions (p. 636) &Array sections and Subarrays

INTEGER, DIMENSION (2, 3) :: A

A(1:2:1, 2:3:1) or A(:, 2:3)

66

33

55

22

66

33

55

22

44

11A

Page 29: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array sections and Subarrays

A(2, 1:3:1) or A(2, :)

A((/ 2, 1 /), 2:3)

665544

33

66

22

55

66

33

55

22

44

11A

Page 30: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Assignment

INTEGER, DIMENSION (2, 3) :: AINTEGER, DIMENSION (3, 2) :: B

A = 0B = RESHAPE (A, (/3, 2/))

0

0

0

0

0

0A

0

0

0

0

0

0

B

Page 31: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Assignment

A(:, 2:3) = RESHAPE ((/ (I**2, I = 1, 4) /), & (/2, 3/))

16

9

4

1

0

0A

Page 32: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Array Assignment: Example

REAL, DIMENSION (2, 3) :: Alpha, BetaWHERE (Alpha /= 0.0)

Beta = 1.0 / AlphaELSEWHERE

Beta = 0.0

END WHERE

0.5

0.0

0.10

0.2

0.0

0.1Alpha

2.0

0.0

1.0

5.0

0.0

0.1Beta

Page 33: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Intrinsic Array-Processing Subprograms

Page 34: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables
Page 35: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables
Page 36: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Matrix Processing (Sec. 9.6) &Intrinsic Array-Processing Subprograms

MATMUL (A, B) --- The product AB

TRANSPOSE (A)

Page 37: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Application: Pollution Tables

In a certain city, the air pollution is measured at a two-hour intervals, beginning at midnight. These measurements are recorded for a one-week period and stored in a file, the first line of which contains the pollution level for day 1, the second line for day 2, and so on.A program must be written to produce a weekly report that displays the pollution levels in a table of the form:

Page 38: CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables

Monitoring Air Pollution