The cumulative sum, \(\sum_{i=1}^k x_i\) for \(k=1,\ldots,n\).
When calling cumsum
, matrices are automatically flattened into column-major order before the sum is taken.
cumsum_axis(expr, axis = 2)
# S4 method for class 'Expression'
cumsum(x)
(Optional) The dimension across which to apply the function: 1
indicates rows, and 2
indicates columns. The default is 2
.
An Expression, vector, or matrix.
val <- cbind(c(1,2), c(3,4))
value(cumsum(Constant(val)))
#> [,1]
#> [1,] 1
#> [2,] 3
#> [3,] 6
#> [4,] 10
value(cumsum_axis(Constant(val)))
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 3 7
x <- Variable(2,2)
prob <- Problem(Minimize(cumsum(x)[4]), list(x == val))
result <- solve(prob)
result$value
#> [1] 10
result$getValue(cumsum(x))
#> [,1]
#> [1,] 1
#> [2,] 3
#> [3,] 6
#> [4,] 10