scipp.sum#

scipp.sum(x, dim=None)#

Sum of elements in the input.

If the input data is in single precision (dtype=’float32’) this internally uses double precision (dtype=’float64’) to reduce the effect of accumulated rounding errors. If multiple dimensions are reduced, the current implementation casts back to float32 after handling each dimension, i.e., the result is equivalent to what would be obtained from manually summing individual dimensions.

Inputs of dtype ‘bool’ or ‘int32’ are accumulated in and returned as ‘int64’, since a sum of bools is a count and sums of ‘int32’ leave the ‘int32’ range after comparatively few elements. Other dtypes are preserved; note that sums of ‘int64’ can still overflow, silently.

Parameters:
Returns:

Same type as x – The sum of the input values.

See also

scipp.nansum

Ignore NaN’s when calculating the sum.

Examples

>>> import scipp as sc
>>> x = sc.array(dims=['x'], values=[1, 2, 3, 4], unit='m')
>>> sc.sum(x)
<scipp.Variable> ()      int64              [m]  10

Sum along a specific dimension:

>>> x = sc.array(dims=['x', 'y'], values=[[1, 2, 3], [4, 5, 6]])
>>> sc.sum(x, 'y')
<scipp.Variable> (x: 2)      int64  [dimensionless]  [6, 15]
>>> sc.sum(x, 'x')
<scipp.Variable> (y: 3)      int64  [dimensionless]  [5, 7, 9]