R/118_atoms_cumprod.R

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/atoms/cumprod.R
#####

## CVXPY SOURCE: atoms/cumprod.py
## Cumprod -- cumulative product along an axis
## Log-log affine (T/T), neither convex nor concave.


Cumprod <- new_class("Cumprod", parent = AxisAtom, package = "CVXR",
  constructor = function(x, axis = 2L, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    axis <- as.integer(axis)
    ## Cumprod preserves shape (like cumsum)
    shape <- .shape(x)

    obj <- .fast_new(Cumprod, S7_object(),
      id       = as.integer(id),
      .cache   = new.env(parent = emptyenv()),
      args     = list(x),
      shape    = shape,
      axis     = axis,
      keepdims = FALSE
    )
    ## Inherit AxisAtom's axis-range + no-complex checks; same pattern
    ## as LogSumExp / MaxEntries (CVXPY parity, v1.8.0-9107 fix).
    validate_arguments(obj)
    obj
  }
)

# -- shape: same as input ------------------------------------------
method(shape_from_args, Cumprod) <- function(x) .arg_shape(x)

# -- curvature: neither convex nor concave --------------------------
method(is_atom_convex, Cumprod) <- function(x) FALSE
method(is_atom_concave, Cumprod) <- function(x) FALSE

# -- log-log curvature: affine (T/T) -------------------------------
method(is_atom_log_log_convex, Cumprod) <- function(x) TRUE
method(is_atom_log_log_concave, Cumprod) <- function(x) TRUE

# -- get_data -------------------------------------------------------
method(get_data, Cumprod) <- function(x) list(x@axis)

# -- numeric --------------------------------------------------------
method(numeric_value, Cumprod) <- function(x, values, ...) {
  v <- values[[1L]]
  ax <- x@axis
  if (ax == 2L) {
    apply(v, 2L, cumprod)
  } else {
    t(apply(v, 1L, cumprod))
  }
}

# -- graph_implementation: stub -------------------------------------
method(graph_implementation, Cumprod) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls Cumprod} not yet implemented.")
}

# -- .grad: not implemented (mirrors CVXPY's TODO) ----------------
## CVXPY SOURCE: atoms/cumprod.py:69-78 (Cumprod._grad).
## CVXPY explicitly returns [] (empty list) with a "TODO implement grad"
## comment. We mirror exactly: an empty list signals "no gradient
## available", causing the chain-rule walker to give the empty result
## for any variable that flows only through a Cumprod node.
method(.grad, Cumprod) <- function(x, values, ...) {
  list()
}

Try the CVXR package in your browser

Any scripts or data that you put into this service are public.

CVXR documentation built on Aug. 24, 2026, 9:10 a.m.