The C++ interface for sum. More...

array sum (const array &input, int dim=-1)
 
template<typename ty >
ty sum (const array &input)
 

Detailed Description

The C++ interface for sum.

Sum is a reduction operation that computes the arithmetic
sum of the elements in an array.

The first sum function sums elements along a specific dimension
of the array. The defualt dimension is the first non-singleton
dimension.
array a = constant(1,5,5);
//a = 1 1 1 1 1
// = 1 1 1 1 1
// = 1 1 1 1 1
// = 1 1 1 1 1
// = 1 1 1 1 1
b = sum(a); //b = [5]
// [5]
// [5]
// [5]
// [5]

The second form of sum is templated, and returns the sum of all array elements to the host.

float hx[] = { -3, -5, 2, -9 };
array x1(2,2, hx); // x1 = -3 2
// -5 -9
printf("%g\n", sum<float>(x1)); // -15

Function Documentation

array af::sum ( const array &  input,
int  dim = -1 
)
Parameters
[in]input
[in]dimdimension along which to operate (-1 indicates first non-singleton dimension)
Returns
array of size equal to the dimension with values summed along the dimension dim in the input.
Examples:
examples/getting_started/gfor.cpp, examples/getting_started/integer.cpp, and examples/machine_learning/kmeans.cpp.
ty af::sum ( const array &  input)
Parameters
[in]inputthe array of values to sum
Returns
sum of all values in the input array