ivy.sum(x, /, *, axis=None, dtype=None, keepdims=False, out=None)[source]#

Calculate the sum of the input array x.

Special Cases

Let N equal the number of elements over which to compute the sum. - If N is 0, the sum is 0 (i.e., the empty sum).

For floating-point operands, - If x_i is NaN, the sum is NaN (i.e., NaN values propagate).

  • x (Union[Array, NativeArray]) – Input array. Should have a numeric data type.

  • axis (Optional[Union[int, Sequence[int]]]) – Axis or axes along which sums must be computed. By default, the sum must be (default: None) computed over the entire array. If a tuple of integers, sums must be computed over multiple axes. Default: None.

  • dtype (Optional[Union[Dtype, NativeDtype]]) –

    (default: None) Data type of the returned array. If None,

    If the default data type corresponding to the data type “kind” (integer or floating-point) of x has a smaller range of values than the data type of x (e.g., x has data type int64 and the default data type is int32, or x has data type uint64 and the default data type is int64), the returned array must have the same data type as x. If x has a floating-point data type, the returned array must have the default floating-point data type. If x has a signed integer data type (e.g., int16), the returned array must have the default integer data type. If x has an unsigned integer data type (e.g., uint16), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is int32, the returned array must have a uint32 data type).

    If the data type (either specified or resolved) differs from the data type of x, the input array should be cast to the specified data type before computing the sum. Default: None.


    keyword argument is intended to help prevent data type overflows.

  • keepdims (Optional[bool]) – If True, the reduced axes (dimensions) must be included in the result as (default: False) singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default: False.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type:



ret – If the sum was computed over the entire array, a zero-dimensional array containing the sum; otherwise, an array containing the sums. The returned array must have a data type as described by the dtype parameter above.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.


With ivy.Array input:

>>> x = ivy.array([0.41, 0.89])
>>> y = ivy.sum(x)
>>> print(y)
>>> x = ivy.array([0.5, 0.7, 2.4])
>>> y = ivy.array(0.0)
>>> ivy.sum(x, out=y)
>>> print(y)
>>> x = ivy.array([[0, 1, 2], [4, 6, 10]])
>>> y = ivy.sum(x, axis = 1, keepdims = False)
>>> print(y)
ivy.array([3, 20])
>>> x = ivy.array([[0, 1, 2], [4, 6, 10]])
>>> y = ivy.array([0,0,0])
>>> ivy.sum(x, axis = 0, keepdims = False, out = y)
>>> print(y)
ivy.array([4, 7, 12])

With ivy.NativeArray input:

>>> x = ivy.native_array([0.1, 0.2, 0.3, 0.3, 0.9, 0.10])
>>> y = ivy.sum(x)
>>> print(y)
>>> x = ivy.native_array([1.0, 2.0, 2.0, 3.0])
>>> y = ivy.array([0.0,0.0,0.0])
>>> ivy.sum(x, out=y)
>>> print(y)

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> y = ivy.sum(x)
>>> print(y)
    a: ivy.array(3.),
    b: ivy.array(12.)
Array.sum(self, /, *, axis=None, keepdims=False, dtype=None, out=None)#
Return type:


Container.sum(self, /, *, axis=None, dtype=None, keepdims=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#
Return type: