# 11. Use of arrays and array sections

The English word "array" is translated into Swedish as "fält", which, retranslated into English, is "field". It is possible therefore, that we may 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 a 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 of each dimension. Two arrays agree if they have the same shape. Operations are normally done element by 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 which 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 mentioned in chapter 9 about recursion, functions in Fortran 90 can be array valued. In that case the use of the RESULT property is recommended to specify a result variable that is supposed to store the array.

Fortran 90 has many more possibilities than Fortran 77 permitting the dynamic allocation of memory. In Fortran 77 this could 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) had to be included as parameters in the call of the subprogram. This is the adjustable array concept. A very simple case is where the last dimension is given simply with a *, or assumed-size array.

Now we also have allocatable arrays, automatic arrays, and assumed-shape arrays. Dynamic allocation using pointers is discussed in a section of the next chapter. An overview is given in Appendix 3 (section 10). Also see Appendix 9 for an 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.