Cumulative sum, optionally along a single dimension.
Arguments
- operand
(
arrayish)
Operand.- dim
(
integer(1)|NULL)
Dimension along which to accumulate. IfNULL(default), the input is first flattened to a 1-D array, likebase::cumsum().- nan_rm
(
logical(1))
How to handleNaNvalues in floating-point inputs. IfFALSE(default),NaNpropagates forward from its first occurrence. IfTRUE,NaNis treated as the identity element of the cumulative op (0for sum,1for prod,-Inf/+Inffor max / min) and contributes nothing to the running value.
Value
arrayish
Has the same shape and data type as the input.
Relation to base R
Both nv_cumsum() (with dim = NULL) and base::cumsum()
flatten a multi-dimensional input to 1-D before accumulating, but the
flatten order differs: anvl arrays are row-major (C order), so the
flattened sequence iterates the last dim fastest, whereas base R uses
column-major (Fortran) order. The two agree on 1-D inputs.
See also
prim_cumsum() for the underlying primitive.
Examples
x <- nv_matrix(1:6, nrow = 2)
nv_cumsum(x) # row-major flatten, then accumulate
#> AnvlArray
#> 1
#> 4
#> 9
#> 11
#> 15
#> 21
#> [ CPUi32{6} ]
nv_cumsum(x, dim = 1L) # accumulate along rows
#> AnvlArray
#> 1 3 5
#> 3 7 11
#> [ CPUi32{2,3} ]
nv_cumsum(nv_array(c(1, NaN, 3))) # NaN propagates
#> AnvlArray
#> 1
#> nan
#> nan
#> [ CPUf32{3} ]
nv_cumsum(nv_array(c(1, NaN, 3)), nan_rm = TRUE) # NaN treated as 0
#> AnvlArray
#> 1
#> 1
#> 4
#> [ CPUf32{3} ]