Week 15 Questions


Fortran

Is a MODULE better than a COMMON block.

Almost always yes. The only reasons to use COMMON blocks are if you expect to use your program on a computer with only a FORTRAN 77 compiler (they still exist), or if it is very important that you control the order in which your data is stored in memory.

What does the following line do?

real, dimension(10) :: a=(/(j,j=1,10)/), b, c

It reserves memory space for 3 real arrays each containing 10 elements. For the array "a", it also preloads values into those 10 elements (a(1)=1, a(2)=2, ... a(10)=10). However, for "b" and "c" no initial values are set. Usually the elements of "b" and "c" are all set to zero, but don't bet on it.

Could you go over masking one more time?

The idea of a mask is to determine which elements of an array are elgible for an array operation. For example suppose that I have two arrays:

a = (/ 1.0, 2.0, 3.0, 3.5, 4.0, 5.0/)

b = (/ -1., 2.0, -3., 3.0, 6.0, 7.0/)

and I use the following Fortran to sum some of the elements of "a"

asum = sum ( a, mask=b.gt.0 )

The first thing that effectively happens is that the mask argument is used to create a logical array containing the results of the test for every element in b.

logical-test = (.false., .true., .false., .true., .true., .true.)

This had better have the same number of elements as "a" or we are in trouble. This array of "true" and "false" values tells us that elements 1 and 3 of "a" will not be included in the sum, but the rest will.

Masks also occur in the WHERE structure:

where (b.gt.0)

c = a/b

else where

c = 0.

end where

For the values of "a" and "b" given above, elements 1 and 3 are false and produce c(1)=0 and c(2)=0. For the rest of the elements, the division is performed (e.g. c(2)=a(2)/b(2), c(4)=a(4)/b(4), etc).

For the index notation of multidimensional arrays, does the first index number correspond to the number of columns and the second to the number of rows?

Other way around. The first index gives the row number and the second the column number.

Computational Solution Methods

What is the purpose of a Transpose?

A transpose is an operation to change a matrix into another specific matrix. If you want an n by n matrix B to be the transpose of A, then the Fortran to do the job is this:

do i =1,n

do j = 1,n

b(i,j) = a(j,i)

enddo

enddo

It is useful in various obsure solution procedures, one or another of which you may stumble across in your career. If you remember what I said about the order matrix elements are stored in C versus Fortran, you may see that this operation can be very useful in programs involving mixtures of C and Fortran subroutines.

Is Simpson's Method faster than the Trapezoidal method? Which one is more reliable?

Simpson's Method is usually faster simply because you usually don't need to evaluate the function at as many points to obtain the same accuracy for the definite integral. If you are integrating smooth functions both are reliable. If your function may have some regions of rapid change in value and derivatives, then trapezoidal is more reliable.