View Raw SPL
/*****************************************************************************
*                                                                            *
*   VSUM.SPL   Copyright (C) 2014 DSP Development Corporation                *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns the sum of one or more input arguments              *
*                                                                            *
*   Revisions:   22 Sep 2014  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_VSUM

    VSUM

    Purpose: Returns the scalar sum of one or more input arguments.

    Syntax:  VSUM(val1, val2, ..., valN)

               valN - One ore more series or scalars.

    Returns: A real, the sum of all the input values.

    Example:
             vsum(10, 15, 20)

             returns 45

    Example:
             vsum({1, 2, 3, 4, 5}, {1, 4, 2}, {1, 0, 3, 8})

             returns 34

    Example:
             W1: gsin(100, 1/100, 1)^2
             W2: gcos(1000, 1/1000, 1)^2
             W3: gline(200, 1/200, 1, 0)
             W4: concat(W1, W2, W3)

             a = sum(W4);
             b = vsum(W1, W2, W3);

             a == b == 649.5

    Example:
             W1: ravel(1..9, 3)
             W2: {sum(W1)}
             W3: {vsum(w1)}

             W1 == {{1, 4, 7},
                    {2, 5, 8},
                    {3, 6, 9}}

             W2 == W3 == 45

    Remarks:
             VSUM with no input arguments uses the current Window.

             Non-numeric arguments are ignored.

             Unlike SUM, the input values can consist of scalars and/or
             series.

             See VSUMS to compute the point by point sum of multiple
             input values.

             See VMEAN to compute the scalar mean of the input values.

    See Also:
             Sum
             Sums
             Vavgs
             Vmax
             Vmean
             Vmin
             Vsums
#endif


/* sum of input args */
vsum(argv)
{
        local j, sums;

        if (argc < 1)
        {
                /* return sum of current window */
                return(sum(w0));
        }

        sums = 0;

        /* get sum and lengths of inputs */
        loop (j = 1..argc)
        {
                if (isnumeric(getargv(j)))
                {
                        if (isarray(getargv(j)))
                        {
                                /* assume series is multi-column */
                                sums += sum(colsum(getargv(j))');
                        }
                        else
                        {
                                /* scalar */
                                sums += getargv(j);
                        }
                }
        }

        /* total sum */
        return(sums);
}