Handout Seven
November 24, 2011
1
Third look at arrays
So far you have learnt how to declare arrays and you have made use of ‘
REAL
’ arrays to represent matrices
in your matrix library module, where the ﬁrst dimension indexes the ‘rows’ of the matrix and the second
dimension indexes the ‘columns’.‘Handout Five’ looked at array terminology size, rank, extent, shape
and conformable also explained was referencing arrays and array construction.
1.1
The ‘
RESHAPE
’ intrinsic function
In ‘Handout Five’ we looked at explicitly assigning values to an array.
INTEGER, DIMENSION(10) :: aa !** 1D array
aa=(/1,2,3,4,5,6,7,8,9,10/)
PRINT ’("The array aa = ",10i3)’,aa
The main restriction to this method of array construction is that it can only be used for arrays of rank
one. For arrays of higher dimensions the ‘
RESHAPE
’ intrinsic function must be used in conjuction with the
above. The ‘
RESHAPE
’ function takes as its ﬁrst argument the ‘source’ array and as its second argument
it takes a rank one array whose elements dictate the required shape of the array to be returned. So for
example the code
INTEGER, DIMENSION(10) :: aa !** 1D array
INTEGER, DIMENSION(2,5) :: bb
aa=(/1,2,3,4,5,6,7,8,9,10/)
bb=RESHAPE(aa,(/2,5/))
CALL outmat(bb)
!*** Your own "outmat" subroutine
the output of the above code would be
1 3 5 7 9
2 4 6 8 10
The important points to note here are that, as requested, the result matrix has two rows and ﬁve columns.
The one dimensional array ‘
aa
’ has been reshaped and then assigned to the array ‘
bb
’. This has been
done by ﬁlling in the the ﬁrst column then the second then the third and so on this is referred to as
‘column major’. There are actually two optional arguments to the ‘
RESHAPE
’ function we will only look at
one of them and that is the keyword argument ‘
ORDER
’. The default order is ‘
ORDER=(/1,2/)
’ and results
in the the ‘column major’ ordering. The reverse is specifying ‘
ORDER=(/2,1/)
’ and would result in the