View Raw SPL
/*****************************************************************************
*                                                                            *
*   CUMPROD.SPL  Copyright (C) 2009 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Cumulative product                                          *
*                                                                            *
*   Revisions:   17 Nov 2009  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_CUMPROD

    CUMPROD

    Purpose: Calculates the cumulative product of a series.

    Syntax:  CUMPROD(series)

             series        - Any series, multi-series table, or expression
                          resulting in a series or table.
  
    Returns: A series or table.

    Example:
             cumprod({1, 2, 3, 4})

             returns a new series containing the cumulative
             product {1, 2, 6, 24}.

    Remarks:
             The nth value of the output series is equal to the product of
             the first n points of the input series.

             CUMPROD calculates the cumulative product of a series.

             CUMPROD is similar to PARTPROD.

    See Also:
             CUMSUM
             PARTPROD
             PROD
#endif


/* cumulative product */
cumprod(s, dim)
{
        local y;

        if (argc < 2)
        {
                if (argc < 1) error("cumprod - series required");
                
                dim = 1;
        }

        if (dim > 1) 
        {
                if (dim > 2) 
                {
                        return(s);
                }

                s = s';
        }

        /* set partprod to compute cumprod */
        y = partprod(s, 0, 1);

        if (dim > 1)
        {
                y = y';
        }

        return(y);
}