The SUM function returns the sum of elements of an array, along a given dimension, for which a mask is true.

## Syntax

SUM (array [,dim] [,mask])

*array* is an INTENT(IN) array of type INTEGER, REAL, or COMPLEX.

*dim* is an INTENT(IN) scalar INTEGER in the range *dim* ≤ *n**n* is the rank of array. The corresponding dummy argument must not be an optional dummy argument.

*mask* is an INTENT(IN) scalar or array of type LOGICAL. It must be conformable with array.

The result is of the same type and kind as array.

The result is scalar if *dim* is absent or if array has rank one; otherwise it is an array of rank n-1 and of shape (*d _{1},d_{2}, ...,d_{dim-1},d_{dim+1}, ...,d_{n}*) where (

If *dim* is absent, the result is the sum of the values of all the elements of array.

If *dim* is present, the result is the sum of the values of all elements of array along dimension dim.

If *mask* is present, only the elements of array for which *mask* is true are considered.

## Example

integer :: m(2,2)=reshape((/1,2,3,4/),shape(m)) write(*,'(2i3)') m ! writes 1 2 ! 3 4 write(*,*) sum(m) ! writes 10 write(*,*) sum(m,dim=1) ! writes 3 7 write(*,*) sum(m,dim=2) ! writes 4 6 write(*,*) sum(m,mask=m>2) ! writes 7