INTRODUCTION TO FORTRAN PART II Prasenjit Ghosh
An example: The Fibonacci Sequence The Fibonacci sequence consists of an infinite set of integer nos. which satisfy the following recurrence relation x n =x n 1 x n 2,n 3 x 1 =1, x 2 =2 Originally it is a model for rabbit breeding The ratio of x n /x n-1 tends to the golden ratio = 5 1 2 The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... Write a program to generate the first 45 terms of the series and estimate the golden ratio.
ALGORITHM (1) Initial conditions, set x 1 =x 2 =1 (2) Initialize the iterator, set n=3 (3)Calculate x n =x n-1 +x n-2 (4) Print out x n and x n /x n-1 (5) Increment n (6) If n < 46 go back to step 3 What type of data type for n & x n? What variable names? How to implement step 6?
PROGRAM DESIGN Write x x n, y x n 1, z x n 2 Variable x,y,z,n are all of type INTEGER Iteration step is x=y+z Have to rename z=y, y=x before next iteration
FIBONACCI PROGRAM
GOTO/ GO TO Statement The syntax is: GOTO label or GO TO label Where the label is the statement label elsewhere in the program. The label must be the first thing on the line The GOTO statement transfers program control to the line of code with the statement label. It can be confusing, so one should try to avoid use this.
IF Statement The syntax is IF (logical expression) action statement If the logical expression is true then the action statement is executed. Note: Only one action statement What if there are more than one logical expression and more than one action statement? Use the IF construct
IF Construct The syntax is IF (logical expr) THEN block 1 ELSEIF (logical expr) THEN block 2... ELSE block n END IF ELSEIF must always come before ELSE
EXAMPLE OF USAGE OF IF BLOCK
Named IF statement The IF can be preceded by a <name> and the END IF followed by a name The loop name must match and be distinct. This helps for checking and clarity
The syntax is: The infinite DO loop DO code-block END DO When END DO is reached, control returns to the beginning of the loop The loop is exited only if there is an EXIT statement in the code-block. The EXIT statement takes the control of the program immediately after the next END DO
The finite DO loop INTEGER:: myvar, mymin, mymax,n..... DO myvar=mymin,mymax, n code block END DO myvar: the loop counter variable mymin: the starting value of the counter mymax: the end value of the counter n: increment of the counter by n, default is 1 NOTE: All variables should be INTEGERS There can be many DO loops within a DO loop. These are called nested DO loops Similar to IF there can be named DO loops
ARRAYS Array is an ordered sequence of many instances of the same data type Equivalent of a vector or a matrix Declared using the DIMENSION keyword: <DTYPE>, DIMENSION(<id>) :: <var> <id> is a comma separated list describing the index range in each dimension: A single value n means that n values in that dimension with indices {1,2,...,n} Values n:m means (m-n+1) values in that dimension, with indices {n, n+1,...,m}
Examples
SOME TERMINOLOGIES REAL:: a(0:100), b(5,5,5) Rank is the no. of dimensions eg. 'a' has rank 1 and 'b' has rank 3 Bounds are the upper & lower limits eg. 'a' has bounds 0:100 and B has 1:5, 1:5, 1:5 A dimension's extent is the UPB-LWB+1 eg. 'a' has an extent of 100 while 'b' has 5, 5 and 5 Size is the total no. of elements eg. 'a' has size 100 while 'b' has size 125 Shape is it's rank and extent eg. 'a' has shape (100) and 'b' (5,5,5) Arrays are conformable if they share a shape
Accessing array elements We access a particular element of an array via its subscript values So to assign to a variable x the value in the m-th row and n-th column of a matrix A, we would code x=a(m,n) The index specifiers should lie within the ranges given in the declaration Sub-array can be accessed using a range-specifier (as in a declaration).
Example of array assignment
Example of array assignment
Array operation Most built in operators/ functions acts element by element on arrays REAL, DIMENSION(50):: v1, v2, v3 v1=sin(v2)+exp(v3) REAL, DIMENSION(50):: v1, v2, v3 LOGICAL, DIMENSION(50):: flag flag=(arr1 > arr2.or. arr3 < 0.0)
Array intrinsic function SUM(x[,n])! Sum of all elements of x PRODUCT(x[,n])! Product of all elements of x TRANSPOSE(x)! x ij x ji DOT_PRODUCT(x,y)! i x i. y i z MATMUL(x,y)! k x ik. y kj z ij Second dimension of x must match with first dimension of y
Array Element Order Fortran arrays are laid out in rows REAL:: A(1:3, 1:4) The elements of A are stored in the order A(1,1),A(2,1),A(3,1),A(1,2),A(2,2),A(3,2),A(1,3),A(2,3),A(3,3), A(1,4),A(2,4),A(3,4) In general one need not worry about the order except for: I/O of arrays, especially unformatted ones Array constructors & array constants
SIMPLE I/O OF ARRAYS Arrays can be included in input & output. They are expanded in array element order REAL, DIMENSIONS(3,2):: array1 READ*, array1 It is same as REAL, DIMENSIONS(3,2):: array1 READ*, array1(1,1),array1(2,1),array1(3,1), & array1(1,2),array1(2,2),array1(3,2)
ALLOCATABLE ARRAYS Sometimes the size of the array may not be known Beforehand. In those cases use the key words ALLOCATABLE, ALLOCATE and DEALLOCATE
ACCESSING ARRAY SUBSETS It is possible to access any sub-array Syntax: must supply a comma-separated list of index descriptors (one for each dimension) Each index descriptor may be (in addition to a single value or colon separated pair): A colon (returns all the elements in that dimension) A colon preceded by a value: array(i:) is equivalent to array(i:ubound(array, 1)) A colon followed by a value: array(:i) is equivalent to array(lbound(array,1):i)
STRINGS A string is an array of type CHARACTER String length declared using the LEN keyword Sub-string referencing requires a colon, so to access i-th character of the string Concatenation operator //: to link together 2 or more strings
An example
Array sorting, an exercise Sort a list of numbers into ascending order Algorithm: Read the nos. and store them in an array Sort them into ascending order of magnitude Print them out in sorted order