R/143_reductions_dcp2cone_canonicalizers_max_canon.R

Defines functions max_canon

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/reductions/dcp2cone/canonicalizers/max_canon.R
#####

## CVXPY SOURCE: reductions/eliminate_pwl/canonicalizers/max_canon.py
## max(x, axis=...) -> t where x <= promoted_t


max_canon <- function(expr, args, solver_context = NULL) {
  x <- args[[1L]]
  shape <- expr@shape
  axis <- expr@axis
  t <- Variable(shape = shape)

  if (is.null(axis)) {
    ## axis=NULL: scalar output, promote t to match x shape
    promoted_t <- cvxr_promote(t, x@shape)
  } else if (axis == 2L) {
    ## axis=2: reduce rows (column-wise) -> shape (1, ncol)
    ## promoted_t = ones(nrow,1) %*% reshape(t, (1, ncol))
    ones_col <- Constant(matrix(1, nrow = x@shape[1L], ncol = 1L))
    t_row <- reshape_expr(t, c(1L, x@shape[2L]))
    promoted_t <- ones_col %*% t_row
  } else {
    ## axis=1: reduce cols -> shape (m, 1)
    ## promoted_t = reshape(t, (m, 1)) %*% ones(1, n)
    t_col <- reshape_expr(t, c(x@shape[1L], 1L))
    ones_row <- Constant(matrix(1, nrow = 1L, ncol = x@shape[2L]))
    promoted_t <- t_col %*% ones_row
  }

  constraints <- list(x <= promoted_t)
  list(t, constraints)
}

method(dcp_canonicalize, MaxEntries) <- max_canon
method(has_dcp_canon, MaxEntries) <- function(expr) TRUE

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.