sum

Date:

02-06-2023

NAME

SUM - Sums array elements

SYNOPSIS

SUM([ARRAY=]array[,[DIM=]dim][,[MASK=]mask])

STANDARDS

Fortran

DESCRIPTION

The SUM intrinsic function sums all the elements of array along dimension dim that correspond to the true elements of mask. It accepts the following arguments:

array

Must be of type integer, real, or complex. It must not be scalar.

dim

Must be scalar and of type integer with a value in the range 1 <= dim <= n, where n is the rank of array. The corresponding actual argument must not be an optional dummy argument.

mask

Must be of type logical and must be conformable with array.

SUM is a transformational intrinsic function. The name of this intrinsic cannot be passed as an argument.

RETURN VALUES

The result is of the same type and kind type parameter as array. It is scalar if dim is absent or if array has rank one; otherwise, the result is an array of rank n-1 and of shape

(d1, d2, ..., ddim-1, ddim+1, ..., dn)

where

(d1, d2, ..., dn)

is the shape of array.

The result of SUM(array) has a value equal to the sum of all the elements of array. If array has size 0, it has the value 0.

The result of SUM(array,MASK=mask) has a value equal to the sum of the elements of array corresponding to the true elements of mask. It has the value 0 if there are no true elements.

If array has rank one, SUM(array,dim[,mask]) has a value equal to that of SUM(array[,MASK=mask]). Otherwise, the value of element

(s1, s2, ..., sdim-1, sdim+1, ..., sn)

of SUM(array,dim[,mask]) is equal to

SUM(array(s1, s2, ..., sdim-1, : , sdim+1, ..., sn)

   [, MASK=mask(s1, s2, ..., sdim-1, : , sdim+1, ..., sn)])

EXAMPLES

Example 1: SUM((/1,2,3/)) yields 6.

Example 2: SUM(C,MASK=C.GT.0.0) yields the arithmetic sum of the positive elements of C.

Example 3: Assume that B is the following array:

| 1 3 5 |
| 2 4 6 |

In this case, the following are true:

  • SUM(B,DIM=1) yields (/3,7,11/).

  • SUM(B,DIM=2) yields (/9,12/).