﻿ SUM Function
 LF Fortran 95

# SUM Function

### Description

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] )
```

### Required Arguments

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

### Optional Arguments

dim is an INTENT(IN) scalar INTEGER in the range 1 ≤ dimn, where 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.

### Result

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 (d1,d2, ...,ddim-1,ddim+1, ...,dn) where (d1,d2, ...,dn) is the shape of array.

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