nansum#

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

Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.

Parameters:
  • x (Union[Array, NativeArray]) – Input array.

  • axis (Optional[Union[int, Tuple[int, ...]]], default: None) – Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of input is used.

  • keepdims (bool, default: False) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

  • out (Optional[Array], default: None) – Alternate output array in which to place the result. The default is None.

Return type:

Array

Returns:

ret – A new array holding the result is returned unless out is specified, in which it is returned.

Examples

>>> a = ivy.array([[ 2.1,  3.4,  ivy.nan], [ivy.nan, 2.4, 2.1]])
>>> ivy.nansum(a)
10.0
>>> ivy.nansum(a, axis=0)
ivy.array([2.1, 5.8, 2.1])
>>> ivy.nansum(a, axis=1)
ivy.array([5.5, 4.5])
Array.nansum(self, /, *, axis=None, dtype=None, keepdims=False, out=None)[source]#

ivy.Array instance method variant of ivy.nansum. This method simply wraps the function, and so the docstring for ivy.nansum also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array.

  • axis (Optional[Union[tuple, int]], default: None) – Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of input is used.

  • keepdims (bool, default: False) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

  • out (Optional[Container], default: None) – Alternate output array in which to place the result. The default is None.

Return type:

Array

Returns:

ret – A new array holding the result is returned unless out is specified, in which it is returned.

Examples

>>> a = ivy.array([[ 2.1,  3.4,  ivy.nan], [ivy.nan, 2.4, 2.1]])
>>> ivy.nansum(a)
10.0
>>> ivy.nansum(a, axis=0)
ivy.array([2.1, 5.8, 2.1])
>>> ivy.nansum(a, axis=1)
ivy.array([5.5, 4.5])
Container.nansum(self, /, *, axis=None, dtype=None, keepdims=False, out=None)[source]#

ivy.Container instance method variant of ivy.nansum. This method simply wraps the function, and so the docstring for ivy.nansum also applies to this method with minimal changes.

Parameters:
  • self (Container) – Input container including arrays.

  • axis (Optional[Union[tuple, int, Container]], default: None) – Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array.

  • dtype (Optional[Union[Dtype, NativeDtype, Container]], default: None) – The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of input is used.

  • keepdims (Union[bool, Container], default: False) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

  • out (Optional[Container], default: None) – Alternate output array in which to place the result. The default is None.

Return type:

Container

Returns:

ret – A new array holding the result is returned unless out is specified, in which it is returned.

Examples

With one ivy.Container input: >>> x = ivy.Container(a=ivy.array([[10, 7, 4], [3, 2, 1]]), b=ivy.array([[1, 4, 2], [ivy.nan, ivy.nan, 0]])) >>> x.nansum(axis=0) {

a: ivy.array([13, 9, 5]), b: ivy.array([1., 4., 2.])

} >>> x.nansum(axis=1) {

a: ivy.array([21, 6]), b: ivy.array([7., 0.])

}