dpnp.sum

dpnp.sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=True)[source]

Sum of array elements over a given axis.

For full documentation refer to numpy.sum.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Input array.

  • axis ({None, 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 integers, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before. Default: None.

  • dtype ({None, dtype}, optional) -- The type of the returned array and of the accumulator in which the elements are summed. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used. Default: None.

  • out ({None, dpnp.ndarray, usm_ndarray}, 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. Default: None.

  • keepdims ({None, bool}, 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 input array. Default: False.

Returns:

out -- 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, a zero-dimensional array is returned. If an output array is specified, a reference to out is returned.

Return type:

dpnp.ndarray

Limitations

Parameters initial and where are only supported with their default values. Otherwise NotImplementedError exception will be raised.

See also

dpnp.ndarray.sum

Equivalent method.

dpnp.cumsum

Cumulative sum of array elements.

dpnp.trapezoid

Integration of array values using the composite trapezoidal rule.

dpnp.mean

Compute the arithmetic mean.

dpnp.average

Compute the weighted average.

Examples

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