Reduction functions are aptly named because an array is operated upon and a result obtained which has a smaller rank than the original source array. For a rank n array, if DIM is absent or n = 1 then the result is scalar, otherwise the result is of rank n-1.
If DIM is not specified for the SUM of a 2D array then the result is obtained by adding all the elements together
Figure 19: Summing along Dimension 1 of a 2D Array
or along the rows,
PRINT*, PRODUCT(A,DIM=1) PRINT*, PRODUCT(A,DIM=2)
gives
2 12 30 15 48
PRINT*, PRODUCT(A,MASK=A.LT.4)
gives
6
For example, consider a 2D array, if DIM=2 then the function returns a 1D vector with the result being as if the ALL function has been applied to each column in turn. If DIM=1 the result is as if the ALL function had been applied to each row in turn.
If A is as before, and
then the following
PRINT*, ALL(A.NE.B,DIM=1)
gives
T F F
recall that dimension 1 runs up and down the page.
Similarly
PRINT*, ALL(A.NE.B,DIM=2)
gives,
F F
where dimension 2 run across the page.
PRINT*, ALL(A.NE.B)
gives the scalar value,
F
PRINT*, ANY(A.NE.B,DIM=1) PRINT*, ANY(A.NE.B,DIM=2)
gives
T F T T T
PRINT*, ANY(A.NE.B)
gives the scalar value,
T
PRINT*, COUNT(A.NE.B,DIM=1) PRINT*, COUNT(A.NE.B,DIM=2)
gives
2 0 1 1 2
PRINT*, COUNT(A.NE.B)
gives the scalar,
3
PRINT*, MAXVAL(A,DIM=1) PRINT*, MAXVAL(A,DIM=2)
gives
2 4 6 5 6
PRINT*, MAXVAL(A,MASK=A.LT.4)
only considers elements of A that are less than 4 and gives
3
PRINT*, MINVAL(A,DIM=1) PRINT*, MINVAL(A,DIM=2)
gives
1 3 5 1 2
PRINT*, MINVAL(A,MASK=A.GT.4)
gives
5
Return to corresponding overview page