mygrad.cumsum#

mygrad.cumsum(a: ArrayLike, axis: None | int | Tuple[int, ...] = None, *, constant: bool | None = None) Tensor[source]#

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

This docstring was adapted from the official numpy documentation

Parameters:
aArrayLike

Input array.

axisint, optional

Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns:
mygrad.Tensor

Examples

>>> from mygrad import cumsum, Tensor
>>> a = Tensor([[1, 2, 3],
...             [4, 5, 6]])
>>> cumsum(a)
Tensor([ 1,  3,  6, 10, 15, 21])
>>> cumsum(a, axis=0)      # sum over rows for each of the 3 columns
Tensor([[1, 2, 3],
        [5, 7, 9]])
>>> cumsum(a, axis=1)      # sum over columns for each of the 2 rows
Tensor([[ 1,  3,  6],
        [ 4,  9, 15]])