R/096_atoms_norm1.R

Defines functions norm1

Documented in norm1

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

## CVXPY SOURCE: atoms/norm1.py
## Norm1 -- L1 norm (sum of absolute values), axis-aware


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

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

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

# -- log-log curvature (norm1.py:58-66) ---------------------------
## On positive arguments norm1 is a sum of positives, which is log-log
## convex.  Without this the DGP analyzer inherited Atom's default FALSE
## and refused valid geometric programs.
method(is_atom_log_log_convex, Norm1) <- function(x) TRUE
method(is_atom_log_log_concave, Norm1) <- function(x) FALSE

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

# -- PWL ----------------------------------------------------------
method(is_pwl, Norm1) <- function(x) {
  is_pwl(.args(x)[[1L]]) && is_real(.args(x)[[1L]])
}

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

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

# -- .column_grad: sign(value) ------------------------------------
## CVXPY SOURCE: atoms/norm1.py:90-110 (norm1._column_grad).
## Subgradient: +1 above 0, -1 below 0, 0 at 0 (CVXPY's canonical pick).
method(.column_grad, Norm1) <- function(x, value, ...) {
  v <- as.numeric(value)
  (v > 0) - (v < 0)
}

#' L1 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 Norm1 atom
#' @export
norm1 <- function(x, axis = NULL, keepdims = FALSE) {
  Norm1(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.