2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. index:: SUM, array, sum, array, add elements, array, conditionally add elements, sum array elements
10 SUM --- Sum of array elements
11 *****************************
13 .. function:: SUM(ARRAY, DIM, MASK)
15 Adds the elements of :samp:`{ARRAY}` along dimension :samp:`{DIM}` if
16 the corresponding element in :samp:`{MASK}` is ``TRUE``.
19 Shall be an array of type ``INTEGER``,
20 ``REAL`` or ``COMPLEX``.
23 (Optional) shall be a scalar of type
24 ``INTEGER`` with a value in the range from 1 to n, where n
25 equals the rank of :samp:`{ARRAY}`.
28 (Optional) shall be of type ``LOGICAL``
29 and either be a scalar or an array of the same shape as :samp:`{ARRAY}`.
32 The result is of the same type as :samp:`{ARRAY}`.
38 Transformational function
41 .. code-block:: fortran
43 RESULT = SUM(ARRAY[, MASK])
44 RESULT = SUM(ARRAY, DIM[, MASK])
47 .. code-block:: fortran
50 INTEGER :: x(5) = (/ 1, 2, 3, 4 ,5 /)
51 print *, SUM(x) ! all elements, sum = 15
52 print *, SUM(x, MASK=MOD(x, 2)==1) ! odd elements, sum = 9