The evolution of arrays in Fortran

Arrays in Fortran weren’t always that fun to deal with. In the first manual for Fortran,  “The Fortran Automatic Coding System for theIBM 704″ in 1956, the keyword DIMENSION was used to specify an array. For example, a floating point array of length 10 could be created in the following manner. The fact that the array name began with A, implicitly made it a real.

DIMENSION A(10)

By Fortran 77, the form of the array declaration had changed:

REAL A(10)

Fortran 90 introduced a new form of type statement with double colon to declare arrays:

real :: a(10)

It also introduced the dimension attribute, which is useful if several arrays have the same shape:

real, dimension(10) :: a, b, c

The way arrays could be used, also has changed over time. In earlier versions of Fortran, working with arrays implied cycling through them using loops. In F90, arrays were promoted to first-class objects, and array-valued expressions could be  evaluated element-wise, allowing the use of loops to be avoided. For example consider a piece of code which assigns all the elements of an array arr to be zero, then perform the assignment a(i) = a(i)/3.1 + b(i)*SQRT(c(i)) for all i. Consider the code first in F77:

    real a(20), b(20), c(20)
    do 10 i=1,20
        a(i)=0
10  continue

    do 20 i=1,20
        a(i)=a(i)/3.1+b(i)*sqrt(c(i))
20  continue

Next, consider the same code in Fortran95:

    real, dimension(20) :: a, b, c
    a = 0
    a = a/3.1+b*sqrt(c)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s