R/069_atoms_elementwise_minimum.R

Defines functions min_elemwise

Documented in min_elemwise

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

## CVXPY SOURCE: atoms/elementwise/minimum.py
## Minimum -- elementwise minimum of 2+ expressions


Minimum <- new_class("Minimum", 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 Minimum} 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(Minimum, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = args,
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- bounds: elementwise min over args (#3080) --------------------
## CVXPY SOURCE: elementwise/minimum.py:63-66.
method(bounds_from_args, Minimum) <- function(x) {
  minimum_bounds(lapply(.args(x), get_bounds))
}

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

# -- curvature: concave ------------------------------------------
method(is_atom_convex, Minimum) <- function(x) FALSE
method(is_atom_concave, Minimum) <- function(x) TRUE

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

# -- log-log: concave (CVXPY minimum.py lines 56-63) -------------
method(is_atom_log_log_convex, Minimum) <- function(x) FALSE
method(is_atom_log_log_concave, Minimum) <- function(x) TRUE

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

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

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

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/minimum.py:76-97 (minimum._grad).
## Symmetric to Maximum: 1 at positions where this arg attains the
## elementwise min and the position is not yet claimed (first-arg-wins
## tie-break), else 0.
method(.grad, Minimum) <- function(x, values, ...) {
  min_vals <- as.numeric(numeric_value(x, values))   # column-major flat
  unused <- rep(TRUE, length(min_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_min <- (as.numeric(values[[idx]]) == min_vals)
    grad_vals <- eq_min & unused
    unused[eq_min] <- FALSE
    grad_list[[idx]] <- .elemwise_grad_to_diag(grad_vals, rows, cols)
  }
  grad_list
}

#' Elementwise minimum of expressions
#'
#' @param ... Expressions (at least 2)
#' @returns A Minimum atom
#' @export
min_elemwise <- function(...) {
  Minimum(...)
}

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.