1.1 basics

Post on 30-Dec-2015

36 Views

Category:

Documents

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

1.1 Basics. 程式架構 (I). program { name } { declarations } { other statements } stop end program { name } 紅色部分為 Fortran 90 的做法. 程式架構 (II). we now show a program that computes the surface area of a sphere from the formula A= : program area real r, Aarea - PowerPoint PPT Presentation

TRANSCRIPT

1

1.1 Basics

2

程式架構 (I) program {name}

{declarations}

{other statements}

stop

end program {name}

紅色部分為 Fortran 90的做法

3

程式架構 (II) we now show a program that computes

the surface area of a sphere from the formula A= : program area real r, Aarea

c this program reads a real number r c and prints the surface area of a sphere that c has radius r.

read(*,*) r Aarea = 4. * 3.14159 * r * r write(*,*) Aarea stop end

24 r

4

Column position rules(Fortran77) I

Columns 1 : : 空白

1 : 數字 , 代表行數 c : 當成註解

Columns 6 :第六行若為非零以外字元 (即 &), 表示本行接續上一行程式 EX. a=b-c &

& -d+e

此為 a=b-c-d+e

5

Columns 7-72 : 程式敘述區 (開頭需空六格 ) 程式敘述……… ..

Columns 73-80: 不使用 ,超過部份不被當成程式碼

Fortran 90無以上 columns的規定

Column position rules(Fortran77) II

6

Fortran 77 vs. 90

註解 :- c 以後的字元當成註解 (Fortran 77)- ! 以後的字元當成註解 (Fortran 90)

儲存檔案 :*.for(Fortran 77)*.f90(Fortran 90)

7

Read and Write Read :

read( * , * ) {variables}

代表 UNIT(輸出位置 ) FMT(輸出格式 )

Write :write(UNIT= ,FMT= ) {variables}

包裝字串 : 使用‘ ’單引號 (Fortran 77), “ ”雙引號 (Fortran90)

8

Type and Declaration 整數 :Integer {list-of-variables} 複數 :Complex {list-of-variables} 字元 :Character {list-of-variables} 邏輯變數 :Logical {list-of-variables} 浮點數 (單精準 ):Real {list-of-variables} 浮點數 (雙精準 ):Double precision {list-of-variables}

變數名稱可由 a~z, A~Z, 0~9組成 ,

但須注意 , 要以字母開頭且變數長度不超過 6 個

9

Integer Variables

In a typical computer might be allocated for each variable x with 32bits , , and then

x =

Ex.

3110 ,....,, bbb

}1,0{ , ).....()1( 201293031 ibbbbbb

13

2021212021

0110143210

2

)(

10

Floating Point Variables Numbers that are stored in real or double

precision variables are represented in Floating point style.

A typical 32-bit FP number x might have a 24-bit mantissa m and an 8-bit exponent e ,

64-bits computer has 56bits for mantissa.

2221023 )...... ()1( bbbbm

2243031 )....()1( bbbe emx 2

11

Arithmetic Assignment add(+) , subtract(-), multiply(*) ,

divide(/) :Ex. 7*4-6/3

exponential :Ex. 2**3 (2 的 3次方 =8)

build-function :Ex. sqrt(4) (4開平方 =2.000000)

12

String Manipulation I

宣告 :character * 7 str

字串 str長度為 7(初始設定為 1)

假設 s1 = ‘abcde’ , s2=‘xyz’ :- 連結 : s=s1//s2 s=‘abcdexyz’

- 指定 : s1(2:4)=‘123’ s1=‘a123e’- 抽出 : s=s1(2:5) s=‘bcde’

13

String Manipulation II 內建函數 ( 當 s=‘abcdabcd’) :

- 計算長度 (length): Ex. length(s)=8- 比對內文 (index): Ex. index(s,’da’) => s=abcdabcd => 4

index(s,’dc’) => s=abcdabcd => 0- 輸入數字 , 對照電腦使用的字元集 (char): Ex. char(72)=H- 輸入字元 , 對照電腦使用的字元集 (ichar): Ex. ichar(H)=72

Fortran 90可使用 len求字串長度

14

Type conversion

c-charr-reald-double precisioni-integerx-real,double precision,

integer

Ex. If ksum is an integer and sum is real then sum = real (ksum) converts ksum to a floating representation(real), and stores the result in sum.

15

Parameter Statement 在程式中 ,有時需要固定不變的資料常數 ,如圓周率 ,

重力加速度…等 .為了簡化程式 ,可直接宣告成 parameter.

Ex. program areareal r, area1 ,fourpiparameter (fourpi = 12.566e0)read(*,*) rarea1 = fourpi * r * rwrite(*,*) area1stopend

16

1.2 LOGICAL OPERATIONS

17

Logical Variables

Declaration : logical a,b

Assigned either the value .TRUE. Or .FALSE.Ex. a = .TRUE.

It could be show like “T” to .TRUE.“F” to .FALSE.

It could be also assigned to .true. or .false. with the lower case.

18

Logical Expressions I

We define some relational operators :.LT. Less than.LE. Less than or equal.EQ. Equal.NE. Not equal.GT. Greater than.GE. Greater than or equal

Ex. How to test if i between 3 and 7 ? Ans: a =(3 .LE. i).AND.(i .LT. 10)

19

Logical Variables II

Truth table (And ,Or , Not) :

20

“IF” Constructs I

The simplest ‘if’ statement :if ({logical expression}) {executable

statement}

Ex. The following statement prints count if count is positive.

if (count .GT. 0) write(*,*) count

21

“IF” Constructs II

if ( {logical expression} ) then{statement}

endif

Ex. The following statement prints count if it is positive

if (count .GT. 0) then

write(*,*) count

endif

22

“IF” Constructs III

The most general form of ‘if’ :

if ( {logical expression} ) then {statement}

elseif ( {logical expression} ) then {statement}

: elseif ( {logical expression} ) then

{statement} endif

23

“IF” Constructs IV A program reads three distinct integers and prints the

median:program medianinteger x,y,z,median1read(*,*)x,y,zif ( (x-z)*(x-y) .LT. 0 ) then

median1 = xelseif ( (y-x)*(y-z) .LT. 0 ) then

median1 = yelse

median1 = zendifwrite(*,*) median1stopend

24

Stylistic Considerations

Make the program statements involves indentation to enhances the readability of if-then-else constructs.

An example :

25

1.3 LOOPS

26

While-Loops I

{label} if ( { logical expression }) then{statements}

goto {label}endif

All the labels in a program must be unique, except in two program ,like between main program and subprogram.

The label is a number between 1 and 99999 and must be situated within columns 2 through 5.

27

While-Loops II

Example:Prints Fibonacci numbers that are strictly less than 200.

program fibon integer x, y, z x = 1 y = 1    10    if ( x .LT. 200 ) then write(*,*)x z = x + y y = x x = z goto 10 endif stop end

28

Until-Loops {label} continue

{statements} if ( { logical expression }) goto {label}

Example:Prints Fibonacci numbers that are strictly less than 200.

program fibonacci integer x, y, z x = 1 y = 1

10  continue z = x + y y = x x = z write(*,*)x if ( x .LE. 200 ) goto 10 stop end

29

Do-Loops I

do {label} {var} = { exp.1 },{ exp.2 },{ exp.3 }{statements}

{label} continue

var is the loop index.exp.1 is an initial value of var.exp.2 is the terminating bound.exp.3 defines the increment(or decrement).

label is code used to assign the loop range.

30

Do-Loops II

Example:Prints Fibonacci numbers from x1 to x10.

program fibonacci integer x, y, z x = 1 y = 1

do 10 count = 1, 10 z = x + y y = x x = z write(*,*)x

10 continue stop end

31

Do-Loops III

Example: compute s = n(n-1)…….(n-k).

s = n do 20 j = n-1, n-k, -1 s = s*j

20 continue

The do statement here says “step from n-1 to n-k in steps of –1 ”.

32

Nesting Do-Loops

Example:Prints all integer triplets (i,j,k) with the property that

.

do 10 i = 1, 100 do 5 j = 1, 100 k = int( sqrt( real( i*i + j*j ) ) ) if ( k*k .EQ. i*i + j*j ) then write(*,*)i, j, k endif

5 continue 10 continue

222 i k and 100ji1 j

33

More on “goto” and “continue”

The ‘no operation’ continue statement is useful for specifying the target of the goto. For Example 10 continue

{statement}

goto 10

cause control to return up to statement 10 and continue (downward) form there.

The goto statement should be used only as last resort since the presence of goto’s in a program makes for difficult reading.

A do-loop must be entered ‘from the top’.Never jump into the body of a do-loop.

34

Loops, Indentation, and Labels

Indentation :The body of a loop should be uniformly indented to highlight the program structure and it should be more

readability. Labels :

Labels should be assigned so that the sequence of numbers is increasing,top to bottom.It’s good practice to leave gaps between the value of consecutive label and the subsequence program will be easily accommodated.

35

1.4 ARRAYS

36

One-dimensional Arrays I

Declaration{type} {name} (size)

Ex. real a(100)- one-dimensional- real array of size 100

37

One-dimensional Arrays II

Contentthe content of cell 1 is denoted by a(1)

Declaration

{name} ({first_index}:{last_index})

Ex. Claim a real array of size 100, with index range –41 to 58:

real b(-41:58)

38

Two-dimensional Arrays I

Declaration{name} (number of row, number of column)

Ex. integer A(4,5),identify a two-dimensional integer array of size 4x5:

(1,1) (1,2) (1,3) (1,4) (1,5)(2,1) (2,2) (2,3) (2,4) (2,5)(3,1) (3,2) (3,3) (3,4) (3,5)(4,1) (4,2) (4,3) (4,4) (4,5)

39

Two-dimensional Arrays II

Declaration

The total size of the array is(last_index1 - first_index1 + 1)*(last_index2 - first_index2 + 1)

integer k(20,100) integer k(1:20,1:100)

It’s the programmer’s responsibility to ensure that the index values are meaningful.Do not assume that array entries are automatically initialized to zero by the compiler.

x2}){last_inde:ex2}{first_indx1},{last_inde:dex1}({first_in {name}

40

Space Allocation for Two-dimensional Arrays I

Fortran stores a two-dimensional array as a contiguous, linear sequence of cells, by column.For example: 4x5 array

41

Space Allocation for Two-dimensional Arrays II

Note the storage of the 3-by-3 times table matrix in the 4-by-5 array results in unused array space:(the following matrix elements are not contiguous in memory)

42

Space Allocation for Two-dimensional Arrays III

Address of an array element isaddr[A(i,j)]=addr[A(1,1)]+(j-1)*adim+(i-1)

where A is an m-by-n matrix stored in array A that has row dimension adim.

43

Space Allocation for Two-dimensional Arrays IV

Physical address:Example:A cell may be 4 bytes long for integer arrays and 8 bytes long for double precision arrays.

Then,the physical address is addr[A(i,j)]=addr[A(1,1)]+[(j-1)*adim+(i-1)]*(4 or 8)byte

program frank integer F(15, 15), n, i, j do 25 n = 2, 6, 2 do 10 i = 1, n do 5 j = 1, n if ( i .LE. j ) then F(i, j) = n-j+1 elseif ( i .EQ. j+1 ) then F(i, j) = n-j else F(i, j) = 0 endif 5 continue10 continue do 15 i = 1, n write(*,*)(F(i, j), j=1, n)15 continue do 20 i = 1, n write(*,*)(F(j, i), j=1, n)20 continue25 continue stop end

45

Packed Storage

Packed form:It’s a useful data structure when dealing with

symmetric and triangular matrices,and only store the lower triangular portion in column-by-column fashion in a one-dimensional array.Ex.

k = 1

do 10 j = 1, n do 5 i = j, n c c a(k) = A(i, j) c

a(k) = i*j k = k+1 5 continue

10 continue

16

12

9

8

6

4

4

3

2

1

a

1

A

161284

12963

8642

432

46

Arrays of Higher Dimension It’s possible to manipulate arrays of dimension up to

seven.real A( 2, 3, 6, 2, 8, 7, 4)

means that there are 2x3x6x2x8x7x4=16128 memory locations are reserved.

Ex. Assign (abcd)2 to hcube(a,b,c,d) [binary to decimal] do 40 a = 0 , 1 do 30 b = 0 , 1 do 20 c = 0 , 1 do 10 d = 0 , 1

hcube(a,b,c,d)=8*a+4*b+2*c+d10 continue20 continue30 continue40 continue

47

1.5 SUBPROGRAMS

48

Built-in Functions

SpecificA specific function requires arguments of a particular type and returns a predefined type.

Ex. char(72) and we will have a string “H” Generic

A generic function accepts arguments of any appropriate type ;the return value is determined

by the types of the arguments.Ex. x = cos(y)

49

Functions I

Declaration{type} function {name} ({list-of-variables})

{Declaration}{statements}returnend

50

Functions II

Rules for user-defined function -The value produced by the function is returned to the

calling program via a variable whose name is the function’s name. Ex. f(x) is returned in the variable f

-A function has a type and it must be declared in the calling program.

-The body of a function resembles the body of a main program and the same rules apply.

-Execution begins at the top and flows to the bottom.Control is passed back to calling program when return statement is encountered the subroutine is exited.

-A reference to a defined function can appear anywhere in an expression,subject to type consideration.

51

Functions III

Solve the problem

program min1integer kreal m greater clarity m=-3.0e0do 10 k=1,10 x = real(k) m= min(m,((x-2.0)*x-7.0)*x-3.0)

10 continuewrite(*,*) mstopend

372)(

)}(),....0(min{),(23

xxxxf

nffnfm

real function f(x)real xf = ((x-2.0)*x-7.0)*x-3.0)returnend

program min2integer kreal m ,fm=f(0.0e0)do 10 k=1,10m= min(m,f(real(k) ) )

10 continuewrite(*,*) mstopend

52

Functions with Other Functions as Arguments

Solve the problem

program min3integer kreal m1,m2 m1=f1(0.0e0)do 10 k=1,10 greater clarity m1= min(m1,f1(real(k))

10continue m2=f2(0.0e0)do 20 k=1,20 m2= min(m2,f2(real(k))

20continuewrite(*,*) 'm(f1,10)=',m1,'m(f2,20)=',m2stopend

)20,( and )10,(

)}(),....0(min{),(

21 fmfm

nffnfm

real function fmin(f,n)integer nreal f fmin = f(0.0e0)do 10 k = 1,n fmin = min(fimn,f(real(k)))10 continuereturnend

program min4real m1,m2,f1,f2external f1,f2m1 =fmin(f1,10)m2 =fmin(f2,20)write(*,*) ‘m(f1,10)=’,m1,’m(f2,20)=’,m2stopend

53

Common I

When we want to solve problem for arbitrary n and arbitrary cubic ,we couldn't use fmin to compute m(f,n).The reason is that fmin expect a function with a single argument,not a function of the form f(x,a,b,c,d).So by placing variables “in common” they can be shared between the main program and one or more subprograms.

dcxbxaxxf 23)(

54

Common II

program min5

integer n

real m,a,b,c,d,f

common /coeff/ a,b,c,d

read (*,*)n,a,b,c,d

m = fmin(f,n)

write(*,*) m

stop

end

55

Common III The syntax for a common statement is as follows:

common /{ name } / { list-of-variables }different common block must have different names.

It’s legal for a variable to belong to more than one common block.

The variables listed in a common block are shared by all subprograms that list the block.

The common statement should be placed before any executable statements and declared in every subprogram using the common block.

The common variables don’t have to be named the same in each such routine, but they need to have same type and in the same order.

56

Subroutines I

Declarationsubroutine {name} ({argument list})

declaration{statements}end

A subroutine is called by a statement of the formcall { name } ({ argument list })

Unlike functions, subroutines do not have a type.The name of a subroutine does not return a value.

57

Subroutines II

subroutine fmin(f,n,value,point)integer n, pointreal f, valueinteger ireal temppoint = 0 value = f(0.0e0)do 10 i = 1,n temp = f(real(i)) if (temp .LT. value) then

point = i value = temp

endif 10 continue

returnend

program min6external finteger n,mptreal mvalread(*,*) n call fmin( f, n, mval, mpt)write(*,*)'m(f,n)=',mval,'mpt=',mpt stopend

58

Functions versus Subroutines I Every function can be put into “equivalent” subroutine form.

Ex. Prints program printreal a, b, c, d, x0, value, fread (*,*) a, b, c, d, x0value = f (a, b, c, d, x0)write (*,*) valuestopend

real function f(a, b, c ,d,x)real a, b, c, d, xf = ((a*x + b)*x + c)*x +dreturn end

dcxbxaxxf 23)(

59

Functions versus Subroutines II Change to “subroutine language”

program printreal a, b, c, d, x0, valueread (*,*) a, b, c, d, x0call f(a, b, ,c, d, x0, value)write (*,*) valuestopend

subroutine f(a, b, c ,d, x, value)real a, b, c, d, x, valuevalue= ((a*x + b)*x + c)*x +dreturn end

dcxbxaxxf 23)(

60

Save Ordinarily, the local variable values are lost when control

passed back to calling program.However, we can retain the value if it is named in a save statement.Ex. subroutine print(k)

integer kinteger lastksave lastkif (k .EQ. 0) then write (*,*) k lastk = kelseif (k .NE. lastk) then write (*,*) k lastk = k endifreturnend

61

Further Rules and Guidelines

Local variables exist only in the subprogram except they are named in a save statement.

Subprograms can be invoked by other subprogram as well as by the main program.

All functions used by a subprogram should be declared.This includes all specific build-in function.

To enhance readability ,there should be only one return in a subprogram.

Minimize the use of common.

62

1.6 ARRAYS AND

SUBPROGRAMS

63

Subprogram with Array Arguments I

A subroutine that performs matrix-vector multiplication

subroutine matvec(p, q, c, cdim, v, w ) integer p, q, cdim real v(*),w(*), C(cdim,*) integer i, j do 10 i = 1, p w(i) = 0.0e010 continue do 30 j = 1, q

do 20 i = 1, p w(i)=w(i)+c(i,j)*v(j)

20 continue30 continue return end

64

Subprogram with Array Arguments II

A main program that calls matvec:

integer idim, jdim parameter (idim = 50, jdim=40) integer i,j,m,n real A(idim,jdim), x(jdim),y(idim)

call matvec(m, n, A, idim, x, y)

stop end

65

Subprogram with Array Arguments III

The last dimension of an F77 array is not needed for address computation, so asterisks suffice.

Ex. The above subroutine , we claim some variables ,like

real v(*),w(*), C(cdim,*)

means v and w is one dimension array and C is two dimension array.

66

Different Dimensions I

The dimension of a passed array does not have to conform to its dimension in the calling program.

It’s perfectly legal to pass a two-dimensional array to a subprogram and then to treat it as one-dimensional in the subprogram.

67

Different Dimensions II

Ex.integer function prod(n,x,y)integer n,x(*),y(*)integer iprod = 0.do 10 i = 1, n prod = prod +x(i)*y(i)

10 continue return

end

Then calculate the AtA with A is an m*n matrix by function prodlength = m * nfrob2A = prod( length, A, A)

First of all ,We must sure m and adim have the same value.

68

Passing Submatrices

Ex.

then

161284

151173

141062

13951

A

1511

1410 )4:3 , 3:2(A

69

Stride

subroutine scale2(n , c, v, incv)integer n, incvreal c, v(*)integer j, k k=1do 10 j = 1, n

v(k)= c*v(k)k = k+incv

10 continuereturnend

suppose c and v(1,8) are initialized,then

call scale2(4 ,c ,v ,2) call scale2(4 ,c ,v(2) ,2) call scale2(2 ,c ,v(3) ,4)

scaled v(7) v(3),

scaled v(8)v(6),v(4), v(2),

scaled v(7)v(5),v(3), v(1),

70

A Note on Index Ranges

Declarationsubroutine sub2(x, p1, p2, A, q1, q2, r1, r2 ….)

integer p1, p2, q1, q2, r1, r2

real x(p1:p2), A(q1:q2,r1:r2)

Locationaddr[A(i, j)] = addr[A(q1, r1)] + (j-r1)*(q2-q1+1) +(i-q1)

71

Passing Arrays in Common Blocks Common blocks provide another way to communicate to

subprograms.[Like the global variable in C]

real function f(x)real u(100) , v(100)integer ncommon /fdate/ u, v, nreal s integer ks = 0.

do 10 k = 1 , n s = s + (u(k)*x – v(k)) ** 2

10 continuef = s- 1 returnend

common /fdate/ u, v, n

n integer

v(100) , u(100) real

mainprogram

72

1.7 INPUT AND OUTPUT

73

“ read” and “write” Statement

Declarationread ({unit number} , {format number}) {list-of-variables}write ({unit number} , {format number}) {list-of-variables}

The first argument indicates where the data is coming from or going to.

The second argument indicates the format of the data.

The asterisk(*) invokes certain convenient default options and it is at this simple level that we begin our discussion.

74

List-directed “read”

Declarationread ({unit number} , {format number}) {list-of-variables}

The format of data is “directed”( ) by the list of variables.Ex. read (*,*) i, j, m, x

read (*,*) y, z

if we set 10 , 5 , 6 , 1.0 -1.0 , 2.0

data items 10, 5 , 6 (i, j, m)are integer.data items 1.0, -1.0, 2.0(x, y, z) are real.

75

List-directed “write”

Declarationwrite ({unit number} , {format number}) {list-of-variables}

We can use single quotes(' ' ) to list the names of the variables along with their values.

Ex. suppose i=1,j=2,x=3,y=4,then when we usewrite(*,*) 'i=',i,',j=',j,',x=',x,',y=',y

and shows like this in the screeni = 1 , j = 2, x = 3 , y = 4

76

Formatted “read” read(*,100) i, j, k

100 format(I6, I6, I6)

There are three integer fields per line,each of the form “I6”.

“6” means six spaces wide. The spaces is includes sign bit.It means that we can’t show

-123456 with ‘I6’.

read(*,100) x, y100 format( E9.1, E10.3)

“E9.1” designates a floating number with length 9 bit including 1 bit mantissa.

It will show asterisk(*) when we don’t allow enough spaces.

77

Formatted “write”

write(* , 101)

write(* , 101)

101 format (1x,//, ' Upon termination : ')

102 format (1x,//, 2x, 'x= ', F8.4, 2x, 'y= ',

& F.4, 2X, 'i= ', I5, 2x, 'j= ', I5)

The slash(/) cause a blank line to be printed. Single quotes are used for names. ‘iX’ indicates that i spaces are to be skipped’

78

Location of “format” Statements

Collecting all the format statements in the program and placing them at the end just before and the end statement.

We recommend labels for all the format statements that appear in a program and encourage the reader interested in the formats to look for their specification at the end of the program.

79

Some Shortcuts

100 format (2x, I2, 2x, I2, 2x, I2)

100 format (3 (2x, I2) )

100 format (I5, I5, /E10.2/, /E10.2/)

100 format (2I5, 2(/E10.2), /)

80

The “data” Statement I

Declarationdata {list-of-variables /{list-of -values}/,…..

Ex. Show the following assignment:n=100, m=-5, x=2.0 ,y=2.0 ,z=2.0

data n/100/, m/-5/, x/2.0/, y/2.0/, z/2.0/data n,m/100,-5/, x,y,z/3*2.0/

81

The “data” Statement II

Ex. Write a code to assign 4-by-3 matrix of ones to A,set the first row of B to [1,2], assign a 4-vector of

zeros to c, and sets the second component of d to 1.

integer idim, jdim, kdim

parameter (idim = 4, jdim = 3, kdim = 2)

real A(idim, jdim), B(kdim, kdim), c(idim), d(kdim)

data A/12 *1.0/, B(1,1)/1.0/

data B(1,2)/2.0/, c/4*0.0/ , d(2)/1.0

A/12 *1.0/ means that matrix A has 12 ones in it.

82

Input and Output of Arrays It’s convenient to read in a m*n matrix :

read (*,*) ((A(i,j), i=1,m) , j=1,n)Ex. read (*,10) ((A(i,j), i=1,5) , j=1,6) 10 format (5 I3)

The example is the same with:j= 1,6 i=1,5

write (*, 10) A10 format (6 F7.4)

which results in 6 numbers per line.If A /1,2,3,4,5,6,7,8,9/ , it shows 1 2 3 4 5 6 -line1

7 8 9 -line2

83

1.8 COMPLEX ARITHMETIC

84

Declaring Complex Variables

Just as the complex number z = x+iy (i2 = -1), and ordered pair (x,y) of real numbers.The declaration

complex z Complex arrays are also possible.

Because complex variables take up twice as much space as real variables, memory constraints sometimes pose a problem when large complex array are involved,

85

Useful Built-in Functions

In a typical situation one often has to ‘set up’ a complex number from two real numbers.

z = complex (x,y)

assigns x+iy to z.Likewise,

x = real (z)

y = aimag(z)

86

Using Complex Variables

Complex variable can be manipulated just as can real and double precision variables.Ex. p = z**2 +z

is equivalent to x = real(z)

y = aimag(z)u = x*x – y*y + xv = 2.0e0*x*y+ yp = complex(u,v)

87

Input/Output

If z is complex write(*,100) z

100 format(‘real(z)=’, f10.7, ‘imag(z)=’, f10.7)

is equivalent tox = real(z)

y = aimag(z)

100 format(‘real(z)=’, f10.7, ‘imag(z)=’, f10.7)

where x and y are real.

88

Avoiding Complex Arithmetic Consider the following function that compute the absolute value

of the large root of the real quadratic equation x2+2bx+c = 0.real function maxrt( b, c ) real b, c d = b*b - c if (d .GE. 0.0e0) then maxrt = abs ( -b + sign(sqrt(d),-b)) else maxrt = sqrt ( b*b - d) endif

return end

this is preferable to the coded= csqrt( complex (b*b +cc,0.0e0)) maxrt = max (cabs(-b+d),cabs(-b-d))

csqrt(a) compute the square root of a,and return complex value.

89

1.9 PROGRAMMING TIPS

90

Intermediate Variables for Clarity and Efficiency I

Ex. If d2 = [r sin(b1) cos(a1) - r sin(b2)cos(a2)]2 +

[r sin(b1) sin(a1) - r sin(b2)sin(a2)]2 +

[r cos(b1) - r cos(b2)]2

Here are three ways to compute d. First: a straight encoding of the formula

d = sqrt (

& ( r*sin(b1)*cos(a1) - r*sin(b2)*cos(a2) )**2 +

& ( r*sin(b1)*sin(a1) - r*sin(b2)*sin(a2) )**2 +

& ( r*cos(b1) - r*cos(b2) )**2 )

91

Intermediate Variables for Clarity and Efficiency II

Second : Use some common subexpressions.

s1 = sin(b1)

s2 = sin(b1)

xdist = s1 * cos(a1) - s2*cos(a2)

ydist = s1 * sin(a1) - s2*sin(a2)

zdist = cos(b1) - cos(b2)

d = r*sqrt ( xdist **2 + xdist**2 + xdist**2)

92

Intermediate Variables for Clarity and Efficiency III

Third : Use some trigonometric identities.

t = cos(a1-a2)

d = r*sqrt (1. – t*cos(b1-b2) -

& cos(b1)*cos(b2)*(1. - t))

The third method is more difficult to read than first and second, and it could be addressed with sufficient comments.It is readability and efficiency ,but is difficult to reconcile.

93

Intermediate Variables for Clarity and Efficiency IV

A temporary array can lead to a more efficient computation.

For example, to set up n*n matrix F with fij = exp(-i –0.5j)

do 10 j=1,n

x(j) = exp(-float(j))

y(j) = exp(-.5 *float(j))

10 continue

do 20 j = 1,n

do 15 i = 1,n

F(i,j) = x(i) * y(j)

15 continue

20 continue

94

Checking Input Parameters

Make sure that the range is permissible.mmult(A, adim, ma, na, BB, bdim, mb, nb, C,

cdim)

if ( na .NE. mb) then

write(*,*) 'Product AB not defined'

return

elseif ( cdim .LT. ma)

write(*,*) 'Array C not big enough'

return

endif

95

Test Most Likely Conditions First

Depend on the comparison permutation

do 10 j=1,n do 5 i= 1,n

if (i .LT. j)then A(i,j)=-1elseif (i .GT. j)then

A(i,j)=0elseif (i .EQ. j)

A(i,j)=1 endif 5 continue10 continue

Avoid all ‘i-j’ comparison do 10 j = 2,n do 5 i =1,j-1 A(i,j) = -1 5 continue 10 continue do 20 j= 1,n A(j,j) = 1 20 continue do 30 j = 1,n-1 do 25 i = j+1,n A(i,j) = 0 25 continue 30 continue

Arrange the conditions in decreasing order of likelihood to minimize execution time.

96

Integer Arithmetic In order to reduce the amount of subscripting, we could do

two.1. Build the running sum in a scalar variable to save space.2. Let alphabets substitute into some fixed variable.

do 30 k =0 ,n-1 s(k) = 0. do 10 i = 0,n-k+1

s(k) = s(k) + x(i)*y(i+k) 10 continue

do 20 i = n-k, n-1 s(k) = s(k) +x(i)*y(i-n+k)

20 continue 30 continue

do 30 k =0 ,n-1 t = 0.

do 10 i = 0,n-k+1 t = t + x(i)*y(i+k)

10 continue kmn = k - n do 20 i = n-k, n-1

t = t +x(i)*y(i+kmn)

20 continue

s(k) = t 30 continue

97

Generality versus Efficiency I Efficient case :

real function dot1(n,x,y) integer n real x(*), y(*) integer i real s s = 0 do 10 i=1,n s = s +x(i)*y(i)

10 continue dot1=s return end

98

Generality versus Efficiency II General case :

real function dot2(n, x, incx, y incy) integer n, incx, incy real x(*), y(*) integer i, ix, iy real s ix = 1 iy = 1 s = 0 do 10 i = 1, n s = s + x(ix)*y(iy) ix = ix + incx iy = iy + incy

10 continue dot2 = s return end

99

Generality versus Efficiency III

Clear case :real function dot2(n, x, incx, y incy) integer n, incx, incy , i, ix, iy real x(*), y(*) ,s s = 0 if ( incx .EQ. 1 .AND. incy .EQ. 1 ) then do 5 i = 1, n

s = s + x(ix)*y(iy) continue else ix = 1 iy = 1 do 10 i = 1,n

s = s + x(ix)*y(iy) ix = ix + incx iy = iy + incy

continue endif

dot3 = s

return

end

100

Machine-independent Testing For Roundoff Noise I The unit roundoff u in a floating point system that

has t-bit mantissas is defined by u=21-t.We have some method to check the precision of the machine.

1.

But it only works in 59-bit floating point system.

endif

else

y)then*if(x.LT.u

55))*(*2uparameter(

101

Machine-independent Testing For Roundoff Noise II

2. A better solution is :

endif

else

y)then .GT. if(z

yxz

102

Termination Criteria I

real function exp1(x,tol)real x, tolreal s, terminteger ks = 1term = xdo 10 k = 1,30

s = s + term if ( abs(term) .LT. tol*abs(s) ) exp1 = s return endif term = term*(x/real(k))

10 continueexp1 = sreturnend

103

Termination Criteria II Some things wrong with above program.

1.’indefinite termination’ is better to use the while construct

2.’tol’ may underlie machine precision.

10 if ( k .LE. kmax .AND. big ) then

s = snew

k = k+1

term = term*(x/float(k))

snew = s +term

big = term .GE. tol*s .AND. snew .NE. s

goto 10

endif

exp2 = s

return

end

real function exp1(x,tol)

real x, tol , s, snew

integer kamx, k

parameter (kmax = 30)

logical big

s = 1.

k = 0

term = x

snew = s + term

big = term .GE. tol*s .AND. snew .NE. s

104

Computing Small Corrections

It’s better to compute the midpoint m of a and b,withm = a-(b-a)/2

rather than with the formulam = (a+b)/2

where a and b are nearby floating point numbers.

105

Pay Attention to the Innermost Loops Ex. We want to evaluate the polynomial

p(x) = a0+a1x2+a2x4+…+anx2n

using Horner’s rule.

s= a(n)do 10 k = n-1:-1:0 s = s*z*z + a(k)

10 continue

we add a number ‘w’ by getting the ‘z*z’ computation outside the loop

s= a(n)w = z*zdo 10 k = n-1:-1:0 s = s*w + a(k)

10 continue

106

Guarding against Overflow I Consider the computation of a cosine-sine pair(c,s) such

that –sx+cy=0, where x and y are given real numbers. A naïve solution:

d = sqrt ( x**2 + y **2 ) if d .GT. 0 c = x/d

s = y/d else c = 1 s = 0 endif

Overflow might occur in the computation of d

107

Guarding against Overflow II

A scaled versions of x and y:m = abs(x) + abs(y)

if m .NE. 0 then

x = x/m

y = y/m

d = sqrt( x*x + y*y )

c = x/d

s = y/d

else

c = 1

s = 0

endif

108

Guarding against Overflow III

Simplify scaled versions of x and y:if abs(y) .GT. abs(x) t = x/y s = 1./sqrt(1 + t*t) c = s*t elseif (abs(x) .GT. 0.) t = y/x c = 1./sqrt(1. + t*t) s = t*c else c = 1 s = 0 endif

109

Column-oriented versus Row-oriented Algorithm

Consider the matrix-vector multiplication y=Ax,where A is an m*n matrix and x is an n-vector. Compute y as follows

do 10 i = 1,m

y(i) = 0.

10 continue

do 30 j = 1,n

do 20 i = 1,m

y(i) = y(i) + A(i,j)*x(j)

20 continue

30 continue

The inner loop is row index , because arrays are stored by column in Fortran

top related