R/148_reductions_dcp2cone_canonicalizers_norm_inf_canon.R

Defines functions norm_inf_canon

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

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


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

  if (is.null(axis)) {
    promoted_t <- cvxr_promote(t, x@shape)
  } else if (axis == 2L) {
    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 {
    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
  }

  list(t, list(x <= promoted_t, x + promoted_t >= 0))
}

method(dcp_canonicalize, NormInf) <- norm_inf_canon
method(has_dcp_canon, NormInf) <- 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.