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
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documentsource array being copied into the result array ﬁlling in the result array row by row, this is referred to as
‘row major’. Consider the following bit of 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/),ORDER=(/2,1/))
CALL outmat(bb)
the output of the above code would be
1 2 3 4 5
6 7 8 9 10
Exercise One :
In a ‘
handout7/exercise1
’ directory write a piece of code to create a rank
one array of sixteen ‘
REAL
’ numbers. Construct the array so that it holds the numbers one
to sixteen in numeric order, ie. index one holds the number one and index two holds the
number to etc. Then ‘
RESHAPE
’ the rank one array into a rank two array to represent the
matrix below.
1
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '10
 BROOKS
 Matrices, Array, Subroutine, Array Intrinsics

Click to edit the document details