A high-performance general-purpose compute library

- Main Page
- Tutorials
- Release Notes
- Examples

Functions
accum
ArrayFire Functions | ArrayFire Functions by Category » Vector Algorithms » Inclusive scan operations

## Functions
AFAPI array | accum (const array &in, const int dim=0)
| C++ Interface to evaluate the cumulative sum (inclusive) along a given dimension.
AFAPI af_err | af_accum (af_array *out, const af_array in, const int dim)
| C Interface to evaluate the cumulative sum (inclusive) along a given dimension.

## Detailed Description

Evaluate the cumulative sum (inclusive) along a given dimension.

For a 1D array \(X\), the inclusive cumulative sum calculates \(x_i = \sum_{p=0}^{i}x_p\) for every \(x \in X\). Here is a simple example for the 1D case:

float hA[] = {0, 1, 2, 3, 4};
array A(5, hA);
// 0.
// 1.
// 2.
// 3.
// 4.
array accumA = accum(A);
// 0.
// 1.
// 3.
// 6.
// 10.

For 2D arrays and higher dimensions, you can specify the dimension along which the cumulative sum will be calculated. Thus, the formula above will be calculated for all array slices along the specified dimension (in the 2D case for example, this looks like \(x_{i,j} = \sum_{p=0}^{j}x_{i,p}\) if the second dimension (dim1) was chosen). If no dimension is specified, then the first dimension (dim0) is used by default (only in the C++ API; the dimension is required to be specified in the C API):

float hB[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
array B(3, 3, hB);
// 0. 3. 6.
// 1. 4. 7.
// 2. 5. 8.
array accumB_dim0 = accum(B);
// 0. 3. 6.
// 1. 7. 13.
// 3. 12. 21.
array accumB_dim1 = accum(B, 1);
// 0. 3. 9.
// 1. 5. 12.
// 2. 7. 15.

The output array type may be different from the input array type. The following table defines corresponding output types for each input type:

Input Type | Output Type
--- | ---
f32, f64, c32, c64 | same as input
s32, s64, u32, u64 | same as input
s16 | s32
u16, u8, b8 | u32

This function runs across all batches in the input simultaneously.

## Function Documentation

## ◆ accum()

AFAPI array accum | ( | const array & | in,
|  | const int | dim = 0 )

C++ Interface to evaluate the cumulative sum (inclusive) along a given dimension.

Parameters

  [in] | in | input array
  [in] | dim | dimension along which the sum is accumulated, 0 denotes the first non-singleton dimension

Returns
  cumulative sum

## ◆ af_accum()

AFAPI af_err af_accum | ( | af_array * | out,
|  | const af_array | in,
|  | const int | dim )

C Interface to evaluate the cumulative sum (inclusive) along a given dimension.

Parameters

  [out] | out | cumulative sum
  [in] | in | input array
  [in] | dim | dimension along which the sum is accumulated

Returns
  AF_SUCCESS, if function returns successfully, else an af_err code is given