R/068_atoms_elementwise_maximum.R

Defines functions max_elemwise

Documented in max_elemwise

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

## CVXPY SOURCE: atoms/elementwise/maximum.py
## Maximum -- elementwise maximum of 2+ expressions


Maximum <- new_class("Maximum", parent = Elementwise, package = "CVXR",
  constructor = function(..., id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    args <- list(...)
    if (length(args) < 2L) {
      cli_abort("{.cls Maximum} requires at least 2 arguments.")
    }
    if (is.null(id)) id <- next_expr_id()
    args <- lapply(args, as_expr)
    shape <- sum_shapes(lapply(args, function(a) .shape(a)))
    obj <- .fast_new(Maximum, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = args,
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- bounds: elementwise max over args (#3080) --------------------
## CVXPY SOURCE: elementwise/maximum.py:71-74.
method(bounds_from_args, Maximum) <- function(x) {
  maximum_bounds(lapply(.args(x), get_bounds))
}

# -- sign ---------------------------------------------------------
## CVXPY: maximum.py -- nonneg if ANY arg nonneg, nonpos if ALL args nonpos
method(sign_from_args, Maximum) <- function(x) {
  list(
    is_nonneg = .any_args(x, is_nonneg),
    is_nonpos = .all_args(x, is_nonpos)
  )
}

# -- curvature: convex --------------------------------------------
method(is_atom_convex, Maximum) <- function(x) TRUE
method(is_atom_concave, Maximum) <- function(x) FALSE

# -- monotonicity: always increasing ------------------------------
method(is_incr, Maximum) <- function(x, idx, ...) TRUE
method(is_decr, Maximum) <- function(x, idx, ...) FALSE

# -- log-log: convex (CVXPY maximum.py lines 64-72) --------------
method(is_atom_log_log_convex, Maximum) <- function(x) TRUE
method(is_atom_log_log_concave, Maximum) <- function(x) FALSE

# -- PWL ----------------------------------------------------------
method(is_pwl, Maximum) <- function(x) {
  .all_args(x, is_pwl)
}

# -- numeric ------------------------------------------------------
method(numeric_value, Maximum) <- function(x, values, ...) {
  Reduce(pmax, values)
}

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

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/maximum.py:84-105 (maximum._grad).
## At each entry, the gradient w.r.t. each arg is 1 if that arg attains
## the elementwise max and the position has not yet been claimed by a
## prior arg (first-arg-wins tie-break), else 0.
method(.grad, Maximum) <- function(x, values, ...) {
  max_vals <- as.numeric(numeric_value(x, values))   # column-major flat
  unused <- rep(TRUE, length(max_vals))
  cols <- as.integer(prod(.shape(x)))
  grad_list <- vector("list", length(values))
  for (idx in seq_along(values)) {
    rows <- as.integer(prod(.arg_shape(x, idx)))
    eq_max <- (as.numeric(values[[idx]]) == max_vals)
    grad_vals <- eq_max & unused
    unused[eq_max] <- FALSE
    grad_list[[idx]] <- .elemwise_grad_to_diag(grad_vals, rows, cols)
  }
  grad_list
}

#' Elementwise maximum of expressions
#'
#' @param ... Expressions (at least 2)
#' @returns A Maximum atom
#' @export
max_elemwise <- function(...) {
  Maximum(...)
}

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.