Efficient Fortran Programming: Mean Calculation and Vector Processing
This comprehensive guide presents various Fortran programming techniques, including how to calculate the mean of a list, perform vector operations, and efficiently sort and search arrays. It explains passing arrays as arguments, using selection sort, linear search, and binary search algorithms. Detailed explanations and examples of multi-dimensional arrays are also provided, including initialization and reading data through nested loops. Ideal for students and professionals looking to enhance their knowledge of array operations in Fortran.
Efficient Fortran Programming: Mean Calculation and Vector Processing
E N D
Presentation Transcript
Arrays asArguments • Example: • Calculating the mean of a list. • (Fortran allows both the array and its size to be passed to the subprogram).
PROGRAM Mean_of_a_List_2 IMPLICIT NONE INTEGER, PARAMETER :: NumItems = 10 REAL, DIMENSION(NumItems) :: Item PRINT *, "Enter the", NumItems, " real numbers:" READ *, Item PRINT '(1X, "Mean of the ", I3, " Numbers is ", F6.2)', & NumItems, Mean(Item, NumItems) CONTAINS
FUNCTION Mean(X, NumElements) INTEGER, INTENT(IN) :: NumElements REAL, DIMENSION(NumElements), INTENT(IN) :: X REAL :: Mean Mean = SUM(X) / REAL(NumElements) END FUNCTION Mean END PROGRAM Mean_of_a_List_2
Vector Processing • Two dimensional vectors can be represented algebraically as ordered pairs • (a1, a2) of real numbers • a1, a2 are called components • In general, n-dimensional vectors can be represented algebraically by ordered n-tuples (a1, a2, … an).
The Norm of a Vector • The norm of an n-dimensional vector • a=(a1, a2, … an) is given by|a| = a21+ a22,+…+ a2n
Function to Calculate the Norm • FUNCTION Norm(A, N) REAL :: Norm REAL, INTENT(IN), DIMENSION(N) :: A INTEGER, INTENT (IN) :: NNorm = SQRT(SUM(A*A))END FUNCTION Norm
Sum & Difference of Vectors • Example: • a=(10, 20) • b=(30, 40) • The SumA + B = (40, 60) • The differenceA - B = (-20, -20)
Sorting • Sorting: Arranging the items in a list so that they are in either ascending or descending order. 25, 33, 1, 14, 27, 451, 14, 25, 27, 33, 45
program sort implicit none integer:: item(10), i, j, min, pos, temp item = (/10, 2, 15, 9, 1, 0, 33, 29, 50, 17/) do i = 1, 9 min = item(i) pos = i do j = i, 10 if (item(j)< min)then min = item(j) pos = j end if end do temp = item(i) item(i)= min item(pos) = temp end do write(*, "(1x, i2)") item end program sort
SUBROUTINE SelectionSort(Item) INTEGER, DIMENSION(:), INTENT(INOUT) :: Item INTEGER :: NumItems, SmallestItem, I INTEGER, DIMENSION(1) :: MINLOC_array NumItems = SIZE(Item) DO I = 1, NumItems - 1 SmallestItem = MINVAL(Item(I:NumItems)) MINLOC_array = MINLOC(Item(I:NumItems)) LocationSmallest = (I - 1) + MINLOC_array(1) Item(LocationSmallest) = Item(I) Item(I) = SmallestItem END DO END SUBROUTINE SelectionSort
Search • Linear Search • Example:
PROGRAM LinearSearch INTEGER, DIMENSION(10) :: A = (/5,10,15,20,25,30,35,40,45,50/) INTEGER :: N, I, POS LOGICAL :: FOUND = .FALSE. WRITE(*,'(1X, A)',ADVANCE="NO") "Search for :" READ *, N DO I=1,10 IF (A(I) == N) THEN POS = I FOUND = .TRUE. EXIT END IF END DO IF (FOUND) PRINT *, "N = ",N," is in position = ",POS END PROGRAM LinearSearch
SUBROUTINE LinearSearch(Item, ItemSought, Found, Location) CHARACTER(*), DIMENSION(:), INTENT(IN) :: Item CHARACTER(*), INTENT(IN) :: ItemSought LOGICAL, INTENT(OUT) :: Found INTEGER, INTENT(OUT) :: Location INTEGER :: NumItems NumItems = SIZE(Item) Location = 1 Found = .FALSE. DO IF ((Location > NumItems) .OR. Found) RETURN IF (ItemSought == Item(Location)) THEN Found = .TRUE. ELSE Location = Location + 1 END IF END DO END SUBROUTINE LinearSearch
Passing Arrays as Arguments SUBROUTINE task(Item) INTEGER, DIMENSION(:), INTENT(IN) :: Item or SUBROUTINE task (Item, NumElements)INTEGER, INTENT(IN) :: NumElements INTEGER, INTENT(IN), DIMENSION(NumElements) :: Item
Binary Search • Binary search can be used to search for an item more efficiently than linear search. • Example:
SUBROUTINE BinarySearch(Item, ItemSought, Found, Location) CHARACTER(*), DIMENSION(:), INTENT(IN) :: Item CHARACTER(*), INTENT(IN) :: ItemSought LOGICAL, INTENT(OUT) :: Found INTEGER, INTENT(OUT) :: Location INTEGER :: First, Last, Middle First = 1 Last = SIZE(Item) Found = .FALSE. DO IF ((First > Last) .OR. Found) RETURN Middle = (First + Last) / 2 IF (ItemSought < Item(Middle)) THEN Last = Middle - 1 ELSE IF (ItemSought > Item(Middle)) THEN First = Middle + 1 ELSE Found = .TRUE. Location = Middle END IF END DO END SUBROUTINE BinarySearch
Multidimensional Arrays • A multidimensional array has more than one subscript. • There is no limit to the number of dimensions Fortran can have, but there is a limit on the total array size.
Declaring Multidimensional Arrays • General Form: • type, DIMENSION(l1:u1, l2:u2, l3:u3,…ln:un) :: &list-of-array-namesexamples:REAL, DIMENSION(24, 365) :: TemperatureREAL, DIMENSION(1:24, 1:365) :: Temperature
Multidimensional Arrays • INTEGER, DIMENSION (3, 5) :: Data Columns Data 1 2 3 4 5 5 10 15 20 25 1 Rows 30 35 40 45 50 2 55 60 65 70 75 3
Multidimensional Arrays • INTEGER, DIMENSION (3, 5) :: DataData(2, 3) 3 5 10 15 20 25 30 35 40 45 50 2 55 60 65 70 75
Reading MD Arrays • Using Nested DO statements • INTEGER :: data (10, 5)DO I = 1, 10 DO J = 1, 5READ *, data (I, J) END DOEND DO
Reading MD Arrays • Using Implied DO StatementsINTEGER :: data (10, 5)READ *, ((data (I, J), J= 1, 5), I=1, 10)
PROGRAM Table_of_Temperatures REAL, DIMENSION(:, :), ALLOCATABLE :: Temperature INTEGER :: NumTimes, NumLocs, Time, Location READ *, NumTimes, NumLocs ALLOCATE (Temperature(NumTimes, NumLocs)) PRINT *, "Enter the temperatures at the first location," PRINT *, "then those at the second location, and so on:" READ *, ((Temperature(Time, Location), & Location = 1, NumLocs), Time = 1, NumTimes) PRINT * PRINT '(1X, T13, "Location" / 1X, "Time", 10I6)', & (Location, Location = 1, NumLocs) DO Time = 1, NumTimes PRINT '(/1X, I3, 2X, 10F6.1/)', & Time, (Temperature(Time, Location), Location=1,NumLocs) END DO DEALLOCATE (Temperature) END PROGRAM Table_of_Temperatures
Matrix Multiplication • Class Exercise
program maxarrays integer, dimension (5)::a, b, c read *, a read *, b c = arrayMax(a, b) print *, c contains function arrayMax (a, b) integer, intent (in), dimension (:) :: a, b integer, dimension(size(a)) :: arrayMax arrayMax = MAX(a, b) end function arrayMax end program maxarrays Exercise 8.3, Prob. 18.