R/097_atoms_norm_inf.R

Defines functions norm_inf

Documented in norm_inf

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

## CVXPY SOURCE: atoms/norm_inf.py
## NormInf -- L-infinity norm (max absolute value), axis-aware


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

# -- bounds: inf-norm = max(|x|) along axis (#3080) ---------------
## CVXPY SOURCE: norm_inf.py:46-49. CVXR axis 1-based -> numpy = 2 - axis;
## reshape to the atom's 2D shape (cf. SumEntries).
method(bounds_from_args, NormInf) <- function(x) {
  b <- get_bounds(.args(x)[[1L]])
  npaxis <- if (is.null(x@axis)) NULL else 2L - x@axis
  rb <- norm_inf_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: always nonneg ------------------------------------------
method(sign_from_args, NormInf) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

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

# -- monotonicity -------------------------------------------------
method(is_incr, NormInf) <- function(x, idx, ...) is_nonneg(.args(x)[[1L]])
method(is_decr, NormInf) <- function(x, idx, ...) is_nonpos(.args(x)[[1L]])

# -- log-log: convex (CVXPY norm_inf.py) --------------------------
method(is_atom_log_log_convex, NormInf) <- function(x) TRUE
method(is_atom_log_log_concave, NormInf) <- function(x) FALSE

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

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

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

# -- .column_grad: not implemented (mirrors CVXPY's TODO) --------
## CVXPY SOURCE: atoms/norm_inf.py:102-118 (norm_inf._column_grad).
## CVXPY raises NotImplementedError with comment "TODO(akshayka):".
## CVXPY test_grad.py marks this skip_reason="Gradient not implemented".
## We mirror exactly: return NULL so the chain-rule walker propagates
## NULL up through every variable downstream of a NormInf node.
method(.column_grad, NormInf) <- function(x, value, ...) {
  NULL
}

#' L-infinity norm of an expression
#'
#' @param x An Expression
#' @param axis NULL (all), 1 (row-wise), or 2 (column-wise)
#' @param keepdims Logical: keep reduced dimensions?
#' @returns A NormInf atom
#' @export
norm_inf <- function(x, axis = NULL, keepdims = FALSE) {
  NormInf(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.