SUM, COUNT, ANY, ALL and the reductions
A reduction takes an array and gives a value: a sum, a product, the largest element, the number of elements that pass a test. Fortran has all of them as intrinsics, with the same two keywords everywhere, so a program almost never writes the loop. This lesson runs the whole family over one array, then over one dimension of a matrix, then over nothing at all.
The reductions
Six reductions answer six questions about a set of numbers, and two more answer yes or no. The names are sum, product, maxval, minval, count, any and all, plus maxloc and minloc for the position of the extreme, which the next lessons use.
The file reductions.f90:
program reductions_demo
implicit none
integer, parameter :: n = 6
integer :: a(n)
a = [3, 8, 1, 9, 2, 7]
print *, "a :", a
print *, "sum :", sum(a)
print *, "product :", product(a)
print *, "maxval :", maxval(a)
print *, "minval :", minval(a)
print *, "count :", count(a > 4)
print *, "any :", any(a > 8)
print *, "all :", all(a > 0)
print *, "maxloc :", maxloc(a)
print *, "sum evens:", sum(a, mask=mod(a, 2) == 0)
end program reductions_demo
gfortran -Wall -Wextra -std=f2018 reductions.f90 -o reductions
./reductions
a : 3 8 1 9 2 7
sum : 30
product : 3024
maxval : 9
minval : 1
count : 3
any : T
all : T
maxloc : 4
sum evens: 10
Two of the answers are worth a second look. maxloc(a) is four, the position of the nine, counted from the lower bound of the array; and count(a > 4) is three although the mask has six elements, because it counts the true ones. The mask in the last line is an expression on the array, so the sum of the even values needs no filter array and no loop.
A mask in a reduction
Every reduction accepts mask= as a keyword, and the result is the reduction of the selected elements only. The keyword form is the one to write: sum(a, mask=a > 4) names the argument, so a reader does not have to remember whether the mask comes second or third.
A mask of the wrong shape is a compile error, and a mask that selects nothing gives the identity value of the operation, which is the subject of the last section. The mean of a filtered set is sum(a, mask=m) / count(m), and the division needs the test the WHERE lesson asked for.
Reducing one dimension
The keyword dim= reduces one dimension and keeps the others: a matrix becomes a vector. Which dimension is which follows from the memory layout of the indexing lesson: the first index runs down a column, so dim=1 adds the values of each column and gives one number per column, while dim=2 adds each row and gives one number per row.
The file dim.f90 on a two by three matrix:
program dim_demo
implicit none
integer, parameter :: nr = 2, nc = 3
integer :: m(nr, nc)
m = reshape([1, 2, 3, 4, 5, 6], [nr, nc])
print *, "m :", m
print *, "sum all :", sum(m)
print *, "sum dim 1 :", sum(m, dim=1)
print *, "sum dim 2 :", sum(m, dim=2)
print *, "maxval dim1:", maxval(m, dim=1)
end program dim_demo
gfortran -Wall -Wextra -std=f2018 dim.f90 -o dim
./dim
m : 1 2 3 4 5 6
sum all : 21
sum dim 1 : 3 7 11
sum dim 2 : 9 12
maxval dim1: 2 4 6
The matrix holds 1 2 3 in its first column and 4 5 6 in its second, which is why the flat print reads 1 2 3 4 5 6. Column sums are 1+2=3, 3+4=7 and 5+6=11; row sums are 1+3+5=9 and 2+4+6=12; the largest of each column is 2, 4 and 6. The shape of the result is the shape of the matrix without the reduced dimension, so a program that keeps it as a vector of length nc or nr matches the matrix it came from.
Empty arrays and empty masks
A zero-sized array is legal, and every reduction has an identity value for it: zero for a sum, one for a product, zero for a count, and the largest or smallest value of the type for an extreme. The same identity appears when a mask selects nothing, which is the case a program meets in practice.
The file empty.f90:
program empty_demo
implicit none
integer :: a(0)
integer :: b(4)
print *, "sum empty :", sum(a)
print *, "product empty :", product(a)
print *, "count empty :", count(a > 0)
b = [1, 2, 3, 4]
print *, "maxval masked :", maxval(b, mask=b > 10)
print *, "minval masked :", minval(b, mask=b > 10)
end program empty_demo
gfortran -Wall -Wextra -std=f2018 empty.f90 -o empty
./empty
sum empty : 0
product empty : 1
count empty : 0
maxval masked : -2147483648
minval masked : 2147483647
Those two large numbers are -huge(1) and huge(1) for a 32-bit integer: the identity of a maximum is the smallest possible value, so that any real element beats it. No element did, so the identity is what comes back, and a program that treats it as data divides by it or prints nonsense. The test is count(mask) before the reduction, never the value after it.
Exercises
- Compute the mean of a with sum(a) / size(a) and print it. Why is the answer an integer?
- Print count(a > 4) and size(a) on one line. How would the mean of the values above four be written?
- Reduce the matrix with dim=1 and dim=2 and check every number against the matrix by hand.
- Ask for all(a > 0) and any(a < 0) for the six values. What do the two answers mean together?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
sum(a)return for an array a?Answer
A scalar with the total of all elements
A reduction without DIM collapses the whole array to one value.
-
What does
count(a > 4)return?Answer
The number of elements above four
count counts the true elements of its mask.
-
What does
any(a > 8)mean?Answer
True if at least one element is above eight
any is true when one element satisfies the mask.
-
What does this program print?
program p implicit none integer :: a(4) a = [2, 4, 6, 8] print *, sum(a, mask=a > 4) end program pAnswer
14
The mask selects 6 and 8, whose sum is 14.
-
What does
sum(m, dim=1)return for a rank-two matrix?Answer
A vector with one column sum per column
Reducing the first index leaves one value per column.
-
What is
sumof a zero-sized array?Answer
0
Zero is the identity value of addition.
-
What is
productof a zero-sized array?Answer
1
One is the identity value of multiplication.
-
What does
maxval(b, mask=b > 10)give when the mask selects nothing?Answer
The smallest integer, -huge(1)
The identity of a maximum must lose to any real value.
-
Which intrinsic gives the position of the largest element?
Answer
maxloc
maxloc returns an index, maxval the value.
-
How is the mean of the values above four written?
Answer
sum(a, mask=a > 4) / count(a > 4)
The numerator and the divisor must both use the same mask.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev