nlcpy.sum

nlcpy.sum(a, axis=None, dtype=None, out=None, keepdims=nlcpy._NoValue, initial=nlcpy._NoValue, where=nlcpy._NoValue)

Sum of array elements over a given axis.

Parameters
aarray_like

Elements to sum.

axisNone or int or tuple of ints, optional

Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.

dtypedtype, optional

The type used to represent the intermediate results. Defaults to the dtype of the output array if this is provided. If out is not provided, the dtype of a is used unless a has nlcpy.int32 or nlcpy.uint32. In that case, nlcpy.int64 or nlcpy.uint64 is used.

outndarray, optional

Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.

keepdimsbool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array.

initialscalar, optional

The value with which to start the reduction. Defaults to 0. If None is given, the first element of the reduction is used, and an error is thrown if the reduction is empty.

wherearray_like of bool, optional

A boolean array which is broadcasted to match the dimensions of a, and selects elements to include in the reduction.

Returns
sum_along_axisndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, this function returns the result as a 0-dimention array. If an output array is specified, a reference to out is returned.

See also

cumsum

Returns the cumulative sum of the elements along a given axis.

mean

Computes the arithmetic mean along the specified axis.

average

Computes the weighted average along the specified axis.

Note

  • nlcpy.reduce.add is called in this function.

  • Arithmetic is modular when using integer types, and no error is raised on overflow.

  • The sum of an empty array is the neutral element 0:

>>> import nlcpy as vp
>>> vp.sum([])
array(0.)

Restriction

  • If an ndarray is passed to where and where.shape != a.shape, NotImplementedError occurs.

  • If an ndarray is passed to out and out.shape != sum_along_axis.shape, NotImplementedError occurs.

Examples

>>> import nlcpy as vp
>>> vp.sum([0.5, 1.5])
array(2.)
>>> vp.sum([0.5, 0.7, 0.2, 1.5], dtype=vp.int32)
array(1, dtype=int32)
>>> vp.sum([[0, 1], [0, 5]])
array(6)
>>> vp.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> vp.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

You can also start the sum with a value other than zero:

>>> vp.sum([10], initial=5)
array(15)