<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2010 - Serge Steer - INRIA
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
          xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
          xml:id="sum" xml:lang="en">
    <refnamediv>
        <refname>sum</refname>
        <refpurpose>sum of array elements</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            y = sum(x)
            y = sum(x, outtype)
            y = sum(x, orientation)
            y = sum(x, orientation, outtype)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x</term>
                <listitem>
                    Array of booleans (full or sparse), encoded integers,
                    real or complex numbers (full or sparse), polynomials, or rationals.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>orientation</term>
                <listitem>
                    it can be either
                    <itemizedlist>
                        <listitem>
                            a character <literal>"*"</literal> (default), <literal>"r"</literal>,
                          <literal>"c"</literal> or  <literal>"m"</literal>
                        </listitem>
                        <listitem>
                            a positive integer: Index of the dimension along which the sums must be computed.
                        </listitem>
                    </itemizedlist>
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>outtype</term>
                <listitem>
                    string <literal>"native"</literal> or <literal>"double"</literal>.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>y</term>
                <listitem>
                    scalar or array
                    <para/>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            For an array <literal>x</literal>,
            <literal>y=sum(x)</literal> returns in the scalar <literal>y</literal> the
            sum of all the elements of <literal>x</literal>.
        </para>
        <para>
            <literal>y=sum(x,orientation)</literal> returns in
            <literal>y</literal> the sum of <literal>x</literal> along the
            dimension given by <literal>orientation</literal>:
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    if <literal>orientation</literal> is equal to 1 or "r" then
                </para>
                <para>
                    <latex><![CDATA[{ y(\mathbf{1},j) = \sum_{\mathbf{i}} x(\mathbf{i},j)}]]></latex>
                </para>
                <para>or </para>
                <para>
                    <latex><![CDATA[ y(\mathbf{1},j,k,\ldots) = \sum_{\mathbf{i}} x(\mathbf{i},j,k,\ldots)]]></latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    if <literal>orientation</literal> is equal to 2 or "c" then:
                </para>
                <para>
                    <latex><![CDATA[ y(i,\mathbf{1}) = \sum_{\mathbf{j}} x(i,\mathbf{j})]]></latex>
                </para>
                <para>or </para>
                <para>
                    <latex><![CDATA[ y(i,\mathbf{1},k,\ldots) = \sum_{j} x(i,\mathbf{j},k,\ldots)]]></latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    if <literal>orientation</literal> is equal to n  then
                </para>
                <para>
                    <latex><![CDATA[ y(i_1,\ldots,i_{n-1},\mathbf{1},i_{n+1},\ldots) = \sum_{\mathbf{i_n}} x(i_1,\ldots,i_{n-1},\mathbf{i_n},i_{n+1},\ldots)]]></latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    <literal>y=sum(x,"*")</literal> is equivalent to <literal>y=sum(x)</literal>
                </para>
            </listitem>
            <listitem>
                <para>
                    <literal>y=sum(x,"m")</literal> is equivalent to
                    <literal>y=sum(x,orientation)</literal> where
                    <literal>orientation</literal> is the index of the
                    first dimension of  <literal>x</literal> that is greater than 1.
                </para>
            </listitem>
        </itemizedlist>
        <para/>
        <para>
            The <literal>outtype</literal>  argument rules the way the summation is done:
        </para>
        <itemizedlist>
            <listitem>
                <para>For arrays of floats, of polynomials, of rational
                    fractions, the evaluation is always done using floating points
                    computations. The <literal>"double"</literal> or <literal>"native"</literal> options are equivalent.
                </para>
            </listitem>
            <listitem>
                <para>For arrays of integers,</para>
                <para>
                    if <literal>outtype="native"</literal> the evaluation is done using integer
                    computations (modulo 2^b, where b is the number of bits used),
                </para>
                <para>
                    if <literal>outtype="double"</literal> the evaluation is done using floating point
                    computations.
                </para>
                <para>
                    The default value is <literal>outtype="native"</literal>.
                </para>
            </listitem>
            <listitem>
                <para>For arrays of booleans,</para>
                <para>
                    if <literal>outtype="native"</literal> the evaluation is done using boolean
                    computations ( + is replaced by |),
                </para>
                <para>
                    if <literal>outtype="double"</literal> the evaluation is done using floating point
                    computations (%t values are replaced by 1 and %f values by 0).
                </para>
                <para>
                    The default value is <literal>outtype="double"</literal>.
                </para>
            </listitem>
        </itemizedlist>
        <note>This function applies with identical rules to <link linkend="sparse">sparse matrices</link>
        </note>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
A = [1,2 ; 3,4];
sum(A)
sum(A,1)

I = uint8([2 95 103;254 9 0])
sum(I) // native evaluation
sum(I, "double")
sum(I, 2, "double")

s = poly(0,"s");
P = [s,%i+s;s^2,1];
sum(P),
sum(P, 2)

B = [%t %t %f %f];
sum(B) //evaluation in float
sum(B, "native") //similar to or(B)
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="plus">plus</link>
            </member>
            <member>
                <link linkend="cumsum">cumsum</link>
            </member>
            <member>
                <link linkend="prod">prod</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
