Arrays (matrices)
Array elements are numbered 1, 2, 3,
and are stored in consecutive locations of memory. A(1)
A(2) A(3) A(4)
Declaration of Array
REAL, DIMENSION (10) :: A LOGICAL, DIMENSION(5) :: TRUTH Character(len=15) , DIMENSION(100) ::
NAMES Integer, dimension(10,10) :: B
Declaration (contd.)
One can use named constants in
declaration. This is useful in changing matrix sizes. Integer, Parameter :: dim=10 Real :: A(dim) Integer :: B(dim,dim) Real :: C(2*dim)
4
Initialization of Arrays
An array element behaves like an ordinary
variable which has the same type as the declared array type. Array can be initialized by defining every element : Integer, Dimension (5) :: A Do I=1,5 A(I) = 0 End Do
Initialization of Array
It is also possible to initialize array in the
declaration statement itself Real, Dimension(5) :: A=(/1.,2.,3.,4.,5./) Initialization may be done with READ statement from terminal or from a file Initialize through an implicit do integer, dimension(5):: A=(/(i, i=1,5)/) initializes A= 1,2,3,4,5
6
Matrix operations
Fortran 90 allows whole matrix operations
between matrices of the same shape and operations like addition, subtraction and multiplication, division etc. will be done on element to element basis, i.e. an operation C= A+B implies C (I , J)= A(I,J) +B(I,J) or C= A*B implies C(I,J) = A(I,J)*B(I,J) and NOT usual matrix multiplication.
8
Bubble Sort
Read N Data as an array of N numbers For each I check if A(I) > A(J) for J= I+1
to N. If yes, swap.
10
Gauss-Seidel
The method converges if written in
diagonally dominant form Give trial values of Xi Solve for each Xi iteratively.
Subroutines
Compiled as separate program independent of
main program Codes can be used repeatedly in different parts of the same or different program Communication with main program is only through passing of arguments. All variables and data except those in the argument list are hidden from main or subroutine so that mistake in one program is not replicated in the other.
12
Subroutine structure
Similar to main program except for the
argument list
Subroutine sub (argument list) Declaration section Statements Return End subroutine sub
V 2 sin 2 R= g
Program rangefinder
real :: u, t, theta, range2 Write(*,*) Enter initial velocity u , angle of projection in degrees and time t Read(*,*) u,t,theta Call Range(u, theta, t, range2) Write(*,*) Range=, range, time-, t End program rangefinder
15
INTENT Variables
Dummy arguments in a subroutine can
have INTENT attribute INTENT(IN), INTENT(OUT), INTENT(INOUT) Intent attribute of each argument should be declared in the subroutine so that accidental tampering such arguments do not take place.
16
memory location of the array argument. There are several ways to pass on array arguments. In this example we use dummy arguments where the arrays have the same shape as in the calling program. The main program calls a subroutine matmult to do the matrix multiplication.
17
Multiplication (Contd.)
Enter the matrix elements of A and B
Call Matmult (A, B, al, au, bl, bu, C) Write C Subroutine Structure : Subroutine Matmult (A1,B1,al,au,bl,bu,C) ! The names of dummy arguments may or may not be the same as the real argument but their number and the order in which they appear must be identical integer, intent (in) :: al, au, bl, bu real, intent(in), dimension(20,20) :: A1,B1 real, intent(out), dimension(20,20) :: C
19
Function
Result is a single number, logical value a
string or an array Intrinsic functions are built in sin(x), abs(x) Here we talk about user defined functions
21
Functions
Function definition should define the return type.
This can be done in two ways : Integer function myfunction (I, J) OR Function myfunction (I,J) integer :: myfunction
22
23
Subroutine Volume
Real function volume(R) ! R is a dummy argument corresponding to radius in main. implicit none Real, intent(in) :: R Real, parameter :: pi=3.14159 If (r<=0.0) then volume=0 Return ! Return control to the calling program End If Volume=4*p1*R**3/3 end function volume
24
Recursion
A function can call itself recursively For instance n! = n*(n-1)!
Recursive function factorial (n) Result(fac) ! Note the return value is in Result, fac is the result returned Integer, intent(in) :: n Integer :: fac If (n==0) then Fac=1 Else Fac= n*factorial(n-1) End if End Function Factorial
25