#
Fortran Wiki

sum

## Description

Adds the elements of `array`

along dimension `dim`

if the corresponding element in `mask`

is `true`

.

## Standard

Fortran 95 and later

## Class

Transformational function

## Syntax

`result = sum(array[, mask])`

`result = sum(array, dim[, mask])`

## Arguments

`array`

- Shall be an array of type `integer`

, `real`

or `complex`

.
`dim`

- (Optional) shall be a scalar of type `integer`

with a value in the range from 1 to n, where n equals the rank of `array`

.
`mask`

- (Optional) shall be of type `logical`

and either be a scalar or an array of the same shape as `array`

.

## Return value

The result is of the same type as `array`

.

If `dim`

is absent, a scalar with the sum of all elements in `array`

is returned. Otherwise, an array of rank n-1, where n equals the rank of `array`

,and a shape similar to that of `array`

with dimension `dim`

dropped is returned.

## Example

```
program test_sum
integer :: x(5) = (/ 1, 2, 3, 4 ,5 /)
print *, sum(x)
print *, sum(x, mask=mod(x, 2)==1)
end program
```

## See also

product

Revised on September 3, 2010 15:37:42
by

Jason Blevins
(128.146.137.198)
(1199 characters / 0.0 pages)