11. Use of arrays and array sections

The English word "array" is translated into Swedish as "fält" which is retranslated into English as "field". We may therefore perhaps use the word field either by mistake or as a suitable name of a specific array. A new feature of Fortran 90 is that you can work directly with a whole array or an array section without explicit (or implicit) DO-loops. In the old Fortran you could in some circumstances work directly with an whole array, but then only during I/O processing.

An array is defined to have a shape given by its number of dimensions (called "rank") and the extent for each one of these. Two arrays agree if they have the same shape. Operations are normally done element for element. Please note that the rank of an array is the number of dimensions and has nothing to do with the mathematical rank of a matrix!

In the following simple example I show how you can assign matrices with simple statements like B = A, how you can use the intrinsic matrix multiplication MATMUL and the addition SUM and how you can use the array sections (in the example below I use array sections who are vectors).

PROGRAM ARRAY_EXAMPLE
       IMPLICIT NONE
       INTEGER                    :: I, J
       REAL, DIMENSION (4,4)      :: A, B, C, D, E
       DO I = 1, 4                ! calculate a test matrix
               DO J = 1, 4
                      A(I, J) = (I-1.2)**J
               END DO
       END DO

       B = A*A          ! element for element multiplication
       CALL PRINTF(A,4) ;  CALL PRINTF(B,4)
       C = MATMUL(A, B) ! internal matrix multiplication
       DO I = 1, 4      ! explicit matrix multiplication
               DO J = 1, 4
                       D(I, J) = SUM( A(I,:)*B(:,J)  )
               END DO
       END DO
       CALL PRINTF(C,4) ;  CALL PRINTF(D,4)
       E = C - D        ! comparison of the two methods
       CALL PRINTF(E,4)
CONTAINS
       SUBROUTINE PRINTF(A, N) ! print an array
       IMPLICIT NONE
       INTEGER                     :: N, I
       REAL, DIMENSION (N, N)      :: A
       DO I = 1, N
              WRITE(*,' (4E15.6)')  A(I,:)
       END DO
       WRITE(*,*)       ! write the blank line
       END SUBROUTINE PRINTF
END PROGRAM ARRAY_EXAMPLE
As was mentioned in chapter 9 about recursion, functions in Fortran 90 can be array valued. In that case it is recommended to use the RESULT property to specify a result variable that is supposed to store the array.

Fortran 90 has much larger possibilities than Fortran 77 to permit dynamic memory allocation, which in Fortran 77 only could be done when a sufficient storage area had been allocated in the calling program unit, and both the array name and the required dimension(s) have to be included as parameters in the call of the subprogram. This is the concept adjustable array. A very simple case is the one with the last dimension, which can be given simply with a *, assumed-size array.

Now we also have the possibilities allocatable array, automatic array, and assumed-shape array. Dynamic allocation using pointers is discussed in a section of the next chapter. An overview is given in Appendix 3 (section 10), and see also Appendix 9 (explanation of certain terms).

Exercise

(11.1) Write a routine for the solution of a system of linear equations using Gaussian elimination with partial pivoting.
Solution.


Last modified: 16 November 1995
boein@nsc.liu.se