sum#
- ivy.sum(x, /, *, axis=None, dtype=None, keepdims=False, out=None)[source]#
Calculate the sum of the input array x.
Special Cases
Let
Nequal the number of elements over which to compute the sum. - IfNis0, the sum is0(i.e., the empty sum).For floating-point operands, - If
x_iisNaN, the sum isNaN(i.e.,NaNvalues propagate).For both real-valued and complex floating-point operands, special cases must be handled as if the operation is implemented by successive application of
ivy.add():- Parameters:
x (
Union[Array,NativeArray]) – Input array. Should have a numeric data type.axis (
Optional[Union[int,Sequence[int]]], default:None) – Axis or axes along which sums must be computed. By default, the sum must be 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
xhas a smaller range of values than the data type ofx(e.g.,xhas data typeint64and the default data type isint32, orxhas data typeuint64and the default data type isint64), the returned array must have the same data type asx. Ifxhas a floating-point data type, the returned array must have the default floating-point data type. Ifxhas a signed integer data type (e.g.,int16), the returned array must have the default integer data type. Ifxhas 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 isint32, the returned array must have auint32data 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.Note
keyword argument is intended to help prevent data type overflows.
- Data type of the returned array. If
keepdims (
Optional[bool], default:False) – IfTrue, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, ifFalse, the reduced axes (dimensions) must not be included in the result. Default:False.out (
Optional[Array], default:None) – optional output array, for writing the result to.
- Return type:
- Returns:
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
dtypeparameter 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.Containerinstances in place of any of the arguments.Examples
With
ivy.Arrayinput:>>> x = ivy.array([0.41, 0.89]) >>> y = ivy.sum(x) >>> print(y) ivy.array(1.3)
>>> x = ivy.array([0.5, 0.7, 2.4]) >>> y = ivy.array(0.0) >>> ivy.sum(x, out=y) >>> print(y) ivy.array(3.6)
>>> 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.NativeArrayinput:>>> x = ivy.native_array([0.1, 0.2, 0.3, 0.3, 0.9, 0.10]) >>> y = ivy.sum(x) >>> print(y) ivy.array(1.9)
>>> x = ivy.native_array([1.0, 2.0, 2.0, 3.0]) >>> y = ivy.array(0.0) >>> ivy.sum(x, out=y) >>> print(y) ivy.array(8.)
With
ivy.Containerinput:>>> 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.) }