<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://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" xml:id="cumsum">
    <refnamediv>
        <refname>cumsum</refname>
        <refpurpose>partial cumulative sums of the elements of an array</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            y = cumsum(x)
            y = cumsum(x, outtype)
            y = cumsum(x, orientation)
            y = cumsum(x, orientation, outtype)
        </synopsis>
    </refsynopsisdiv>

    <refsection role="parameters">
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x</term>
                <listitem>
                    <para>
                        array of booleans, integers, real or complex numbers, polynomials, or
                        rational fractions.
                        Hypermatrices or sparse boolean or numerical matrices are supported as well.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>orientation</term>
                <listitem>
                    <para>This argument can be</para>
                    <itemizedlist>
                        <listitem>
                            <para>
                                either a string with possible values <literal>"*"</literal>,
                                <literal>"r"</literal>, <literal>"c"</literal> or <literal>"m"</literal>
                            </para>
                        </listitem>
                        <listitem>
                            <para>
                              positive integer 1 ≤ orientation ≤ ndims(x): the index of the
                              dimension along which the cumulative sum must be computed.
                              1 and "r", and 2 and "c", are equivalent.
                            </para>
                        </listitem>
                    </itemizedlist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>outtype</term>
                <listitem>
                    <para>
                        Word <literal>"native"</literal> or <literal>"double"</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>y</term>
                <listitem>
                    <para>Array of size equal to that of <varname>x</varname>.</para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>

    <refsection role="description">
        <title>Description</title>
        <para>
            <emphasis role="bold">y = cumsum(x)</emphasis> computes and provides the partial
            cumulative sums <literal>y(i)=sum(x(1:i))</literal>, <emphasis role="italic">i.e.</emphasis>:
        </para>
        <para>
            <latex alt="y(i) = ∑u=1→i x(u)"><![CDATA[ y(i) = \sum_{u=1}^i x(u)]]></latex>
        </para>
        <para>
            <emphasis role="bold">y = cumsum(x, orientation)</emphasis> returns the partial
            cumulative sums of <term>x</term> along the dimension given by <term>orientation</term>:
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    if <term>orientation</term> is equal to 1 or "r" then:
                </para>
                <para>
                    <latex alt="y(i,j) = ∑u=1→i x(u,j)"><![CDATA[
                        y(\mathbf{i},j) = \sum_{u=1}^{\mathbf{i}} x(\mathbf{u},j)   ]]>
                    </latex>, or for a N-Dimensional array:
                </para>
                <para>
                    <latex alt="y(i,j,k,…) = ∑u=1→i x(u,j,k,…)"><![CDATA[
                        y(\mathbf{i},j,k,\ldots) = \sum_{u=1}^{\mathbf{i}} x(\mathbf{u},j,k,\ldots)  ]]>
                    </latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    if <term>orientation</term> is equal to 2 or "c" then:
                </para>
                <para>
                    <latex alt="y(i,j) = ∑u=1→j x(i,u)"><![CDATA[
                        y(i,\mathbf{j}) = \sum_{u=1}^{\mathbf{j}} x(i,{\mathbf{u})  ]]>
                    </latex>, or for a N-Dimensional array:
                </para>
                <para>
                    <latex alt="y(i,j,k,…) = ∑u=1→j x(i,u,k,…)"><![CDATA[
                        y(i,\mathbf{j},k,\ldots) = \sum_{u=1}^{\mathbf{j}} x(i,\mathbf{u},k,\ldots) ]]>
                    </latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    if <term>orientation</term> is equal to n then:
                </para>
                <para>
                    <latex alt="y(i₁,…,iₙ₋₁, iₙ,iₙ₊₁,…) = ∑u=1…iₙ  x(i₁,…,iₙ₋₁, u,iₙ₊₁,…)"><![CDATA[
                        y(i_1,\ldots,i_{n-1},\mathbf{i_{n}},i_{n+1},\ldots) =
                        \sum_{u=1}^{\mathbf{i_n}} x(i_1,\ldots,i_{n-1},\mathbf{u},i_{n+1},\ldots) ]]>
                    </latex>
                </para>
            </listitem>
            <listitem>
                <para>
                    <emphasis role="bold">y = cumsum(x, "*")</emphasis> is equivalent to
                    <literal>y = cumsum(x)</literal>
                </para>
            </listitem>
            <listitem>
                <para>
                    <emphasis role="bold">y = cumsum(x, "m")</emphasis> is equivalent to
                    <literal>y = cumsum(x, orientation)</literal> where <term>orientation</term>
                    is the index of the first dimension of <term>x</term> that is greater than 1.
                    This option is used for Matlab compatibility.
                </para>
            </listitem>
        </itemizedlist>
        <para/>
        <para>
            The <term>outtype</term> argument rules the way the summations are 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 with |),
                </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>
        <warning>
            When the input <varname>x</varname> is sparse, please keep in mind that the density
            of the result <varname>y</varname> will be almost always close to 100%.
        </warning>
    </refsection>

    <refsection role="examples">
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
A = [1,2;3,4];
cumsum(A)
cumsum(A,1)

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

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

B = [%t %t %f %f];
cumsum(B)          // evaluation in float
cumsum(B,"native") // similar to or(B)
 ]]></programlisting>
    </refsection>

    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="sum">sum</link>
            </member>
            <member>
                <link linkend="cumprod">cumprod</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
