R/100_atoms_min.R

Defines functions min_entries

Documented in min_entries

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

## CVXPY SOURCE: atoms/min.py
## MinEntries -- minimum entry of an expression, axis-aware


MinEntries <- new_class("MinEntries", parent = AxisAtom, package = "CVXR",
  constructor = function(x, axis = NULL, keepdims = FALSE, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    if (!is.null(axis)) .validate_axis(axis, length(.shape(x)))
    shape <- .axis_shape(.shape(x), axis, keepdims)

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

# -- bounds: min reduction along axis (#3080) ---------------------
## CVXPY SOURCE: min.py:88-91. CVXR axis 1-based -> numpy = 2 - axis;
## reshape to the atom's 2D shape (cf. SumEntries).
method(bounds_from_args, MinEntries) <- function(x) {
  b <- get_bounds(.args(x)[[1L]])
  npaxis <- if (is.null(x@axis)) NULL else 2L - x@axis
  rb <- min_reduction_bounds(b[[1L]], b[[2L]], axis = npaxis, keepdims = x@keepdims)
  lb <- rb[[1L]]; ub <- rb[[2L]]
  dim(lb) <- .shape(x); dim(ub) <- .shape(x)
  list(lb, ub)
}

# -- sign: same as arg --------------------------------------------
method(sign_from_args, MinEntries) <- function(x) {
  list(is_nonneg = is_nonneg(.args(x)[[1L]]),
       is_nonpos = is_nonpos(.args(x)[[1L]]))
}

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

# -- log-log: concave only (CVXPY min.py) ------------------------
method(is_atom_log_log_convex, MinEntries) <- function(x) FALSE
method(is_atom_log_log_concave, MinEntries) <- function(x) TRUE

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

# -- PWL ----------------------------------------------------------
method(is_pwl, MinEntries) <- function(x) is_pwl(.args(x)[[1L]])

# -- numeric ------------------------------------------------------
method(numeric_value, MinEntries) <- function(x, values, ...) {
  v <- values[[1L]]
  if (is.null(x@axis)) {
    matrix(min(v), 1L, 1L)
  } else if (x@axis == 2L) {
    res <- apply(v, 2L, min)
    if (x@keepdims) matrix(res, nrow = 1L) else matrix(res, nrow = 1L)
  } else {
    res <- apply(v, 1L, min)
    if (x@keepdims) matrix(res, ncol = 1L) else matrix(res, ncol = 1L)
  }
}

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

# -- .column_grad: indicator at the smallest index ----------------
## CVXPY SOURCE: atoms/min.py:69-89 (min._column_grad).
## Symmetric to MaxEntries: 1 at the (first) argmin, 0 elsewhere.
method(.column_grad, MinEntries) <- function(x, value, ...) {
  v <- as.numeric(value)
  D <- numeric(length(v))
  D[which.min(v)] <- 1
  D
}

#' Minimum entry of an expression
#'
#' @param x An Expression
#' @param axis NULL (all), 1 (row-wise), or 2 (column-wise)
#' @param keepdims Logical
#' @returns A MinEntries atom
#' @export
min_entries <- function(x, axis = NULL, keepdims = FALSE) {
  MinEntries(x, axis, keepdims)
}

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.