Fortran 90

6.3 Reduction Operations
Reduction operations on arrays are among the more useful of the Fortran 90 intrinsics, especially when a parallel implementation is available. The best way to see how these reduction operations operate is to study a few instances where they are used in a program.
  • Global operations involving every element
    REAL :: a(1024), b(4,1024)
    scalar = SUM(a)  ! sum of all elements
    a = PRODUCT(b, DIM=1) ! product of elts in first dim
    scalar = COUNT(a == 0) ! gives number of zero elements
    scalar = MAXVAL(a, MASK=a.LT.0) ! largest negative element 
  • Logical reductions: ANY, ALL
    LOGICAL a(n)
    REAL, DIMENSION(n) :: b, c
    IF ( ALL(a) ) ...         ! global AND
    IF ( ALL(b == c) ) ...    ! true if all elts equal
    IF ( ANY(a) ) ...         ! global OR
    IF ( ANY(b < 0.0) ) ...   ! true if any elt < 0.0