R/115_atoms_cummax.R

Defines functions cummax_expr

Documented in cummax_expr

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

## CVXPY SOURCE: atoms/cummax.py
## Cummax -- cumulative maximum along an axis


Cummax <- new_class("Cummax", 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)
    shape <- .shape(x)

    obj <- .fast_new(Cummax, S7_object(),
      id       = as.integer(id),
      .cache   = new.env(parent = emptyenv()),
      args     = list(x),
      shape    = shape,
      axis     = as.integer(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 (cummax preserves shape)
method(shape_from_args, Cummax) <- function(x) {
  .arg_shape(x)
}

## Sign: passthrough from argument
method(sign_from_args, Cummax) <- function(x) {
  list(is_nonneg = is_nonneg(.args(x)[[1L]]),
       is_nonpos = is_nonpos(.args(x)[[1L]]))
}

## Curvature: convex
method(is_atom_convex, Cummax) <- function(x) TRUE
method(is_atom_concave, Cummax) <- function(x) FALSE

## Monotonicity: increasing
method(is_incr, Cummax) <- function(x, idx, ...) TRUE
method(is_decr, Cummax) <- function(x, idx, ...) FALSE

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

## numeric
method(numeric_value, Cummax) <- function(x, values, ...) {
  v <- values[[1L]]
  ax <- x@axis
  if (ax == 2L) {
    ## Cummax down columns (column-wise)
    apply(v, 2L, cummax)
  } else {
    ## Cummax across rows
    t(apply(v, 1L, cummax))
  }
}

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

# -- .column_grad: 1 wherever the running max changes -------------
## CVXPY SOURCE: atoms/cummax.py:58-77 (Cummax._column_grad).
## D[1] = 1 always; D[i] = 1 iff cummax up to i is strictly greater
## than cummax up to i-1, i.e. iff the maximum was attained at i.
## CVXPY uses np.maximum.accumulate; R has cummax().
method(.column_grad, Cummax) <- function(x, value, ...) {
  v <- as.numeric(value)
  D <- numeric(length(v))
  D[1L] <- 1
  if (length(v) > 1L) {
    maxes <- cummax(v)
    D[-1L] <- as.numeric(maxes[-1L] > maxes[-length(maxes)])
  }
  D
}

#' Cumulative maximum along an axis
#'
#' @param x An Expression
#' @param axis 1 (across rows) or 2 (down columns, default)
#' @returns A Cummax atom
#' @export
cummax_expr <- function(x, axis = 2L) {
  Cummax(x, axis)
}

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.