R/091_atoms_max.R

Defines functions max_entries

Documented in max_entries

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

## CVXPY SOURCE: atoms/max.py
## MaxEntries -- maximum entry of an expression, axis-aware


MaxEntries <- new_class("MaxEntries", parent = AxisAtom, package = "CVXR",
  constructor = function(x, axis = NULL, keepdims = FALSE, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    if (!is.null(axis)) .validate_axis(axis, length(x@shape))
    shape <- .axis_shape(x@shape, axis, keepdims)

    obj <- new_object(S7_object(),
      id       = as.integer(id),
      .cache   = new.env(parent = emptyenv()),
      args     = list(x),
      shape    = shape,
      axis     = axis,
      keepdims = keepdims
    )
    validate_arguments(obj)
    obj
  }
)

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

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

# -- log-log: convex only (CVXPY max.py) -------------------------
method(is_atom_log_log_convex, MaxEntries) <- function(x) TRUE
method(is_atom_log_log_concave, MaxEntries) <- function(x) FALSE

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

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

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

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

#' Maximum entry of an expression
#'
#' @param x An Expression
#' @param axis NULL (all), 1 (row-wise), or 2 (column-wise)
#' @param keepdims Logical
#' @returns A MaxEntries atom
#' @export
max_entries <- function(x, axis = NULL, keepdims = FALSE) {
  MaxEntries(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 March 6, 2026, 9:10 a.m.