RWalib C Array Library User Guide > Arithmetic Operations > alibCumSum
  

alibCumSum
Returns the cumulative sum over one dimension of an array. The PV-WAVE API for this routine is the CUMSUM Function.
Prototypes
void alibCumSumi( wvlong m, wvlong n, wvlong *d, int *p, int *q )
void alibCumSuml( wvlong m, wvlong n, wvlong *d, wvlong *p, wvlong *q )
void alibCumSumf( wvlong m, wvlong n, wvlong *d, float *p, float *q )
void alibCumSumd( wvlong m, wvlong n, wvlong *d, double *p, double *q )
Parameters
m — (Input) The dimension >=0 along which elements are cumulatively summed. In a vector or matrix for example, set m=0 to cumulatively sum over the vector or over each column of the matrix, or set m=1 to cumulatively sum over each row of the matrix.
n — (Input) The number of dimensions in the source array.
*d — (Input) The n-element array of dimensions for the source array.
*p — (Input) The n-dimensional source array of dimensions d.
*q — (Input/Output) The destination array. On output, array q contains the cumulative sum over each 'column' along dimension m, and so it has the same dimensions as array p. Computations are in the data-type of arrays p and q, so the integer types are subject to overflow without warning.
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
    /* arrays of dimensions to use for all the examples */
        wvlong dm[3]={4,3,2};
    /* source data for all the examples */
        int    i0[24];
    /* output array to use for all the examples */
        int    i[24];
    /* initialize the source data */
        alibinit( NULL, NULL, NULL, NULL );
        alibIndexi( 24, 0, 1, i0 );
    printf( "\n\n show a 4-element vector" );
        alibPrintArrayi( 1, 1, 1, 4, i0, NULL );
     printf( "\n\n show the cumulative sum over the vector" );
        alibCumSumi( 0, 1, dm, i0, i );
        alibPrintArrayi( 1, 1, 1, 4, i, NULL );
    printf( "\n\n show a (4,3) matrix" );
        alibPrintArrayi( 1, 1, 4, 3, i0, NULL );
    printf( "\n\n show cumulative sum over dim 0, i.e. over each column" );
        alibCumSumi( 0, 2, dm, i0, i );
        alibPrintArrayi( 1, 1, 4, 3, i, NULL );
    printf( "\n\n show cumulative sum over dim 1, i.e. over each row" );
        alibCumSumi( 1, 2, dm, i0, i );
        alibPrintArrayi( 1, 1, 4, 3, i, NULL );
    printf( "\n\n show a (4,3,2) array" );
        alibPrintArrayi( 1, 4, 3, 2, i0, NULL );
    printf( "\n\n show the cumulative sum over dimension 0" );
        alibCumSumi( 0, 3, dm, i0, i );
        alibPrintArrayi( 1, 4, 3, 2, i, NULL );
    printf( "\n\n show the cumulative sum over dimension 1" );
        alibCumSumi( 1, 3, dm, i0, i );
        alibPrintArrayi( 1, 4, 3, 2, i, NULL );
    printf( "\n\n show the cumulative sum over dimension 2" );
        alibCumSumi( 2, 3, dm, i0, i );
        alibPrintArrayi( 1, 4, 3, 2, i, NULL );
}
 
Output:
 
 show a 4-element vector
 
           0           1           2           3
 
 show the cumulative sum over the vector
 
           0           1           3           6
 
 show a (4,3) matrix
 
           0           1           2
           3           4           5
           6           7           8
           9          10          11
 
 show cumulative sum over dim 0, i.e. over each column
 
           0           1           2
           3           5           7
           9          12          15
          18          22          26
 
 
 show cumulative sum over dim 1, i.e. over each row
 
           0           1           3
           3           7          12
           6          13          21
           9          19          30
 
 show a (4,3,2) array
 
           0           1
           2           3
           4           5
 
           6           7
           8           9
          10          11
 
          12          13
          14          15
          16          17
 
          18          19
          20          21
          22          23
 
 show the cumulative sum over dimension 0
 
           0           1
           2           3
           4           5
 
           6           8
          10          12
          14          16
 
          18          21
          24          27
          30          33
 
          36          40
          44          48
          52          56
 
 show the cumulative sum over dimension 1
 
           0           1
           2           4
           6           9
 
           6           7
          14          16
          24          27
 
          12          13
          26          28
          42          45
 
          18          19
          38          40
          60          63
 
 show the cumulative sum over dimension 2
 
           0           1
           2           5
           4           9
 
           6          13
           8          17
          10          21
 
          12          25
          14          29
          16          33
 
          18          37
          20          41
          22          45

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.