]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/doc/gfortran/intrinsic-procedures/sum.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / fortran / doc / gfortran / intrinsic-procedures / sum.rst
CommitLineData
c63539ff
ML
1..
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.
5
6.. index:: SUM, array, sum, array, add elements, array, conditionally add elements, sum array elements
7
8.. _sum:
9
10SUM --- Sum of array elements
11*****************************
12
13.. function:: SUM(ARRAY, DIM, MASK)
14
15 Adds the elements of :samp:`{ARRAY}` along dimension :samp:`{DIM}` if
16 the corresponding element in :samp:`{MASK}` is ``TRUE``.
17
18 :param ARRAY:
19 Shall be an array of type ``INTEGER``,
20 ``REAL`` or ``COMPLEX``.
21
22 :param DIM:
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}`.
26
27 :param MASK:
28 (Optional) shall be of type ``LOGICAL``
29 and either be a scalar or an array of the same shape as :samp:`{ARRAY}`.
30
31 :return:
32 The result is of the same type as :samp:`{ARRAY}`.
33
34 Standard:
35 Fortran 90 and later
36
37 Class:
38 Transformational function
39
40 Syntax:
41 .. code-block:: fortran
42
43 RESULT = SUM(ARRAY[, MASK])
44 RESULT = SUM(ARRAY, DIM[, MASK])
45
46 Example:
47 .. code-block:: fortran
48
49 PROGRAM test_sum
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
53 END PROGRAM
54
55 See also:
3ed1b4ce 56 :ref:`PRODUCT`