R/072_atoms_elementwise_log1p.R

Defines functions log1p_atom

Documented in log1p_atom

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

## CVXPY SOURCE: atoms/elementwise/log1p.py
## Log1p -- elementwise log(1 + x), extends Log


Log1p <- new_class("Log1p", parent = Log, package = "CVXR",
  constructor = function(x, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    shape <- .shape(x)
    obj <- .fast_new(Log1p, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: tracks argument sign (unlike Log which is unknown) -----
method(sign_from_args, Log1p) <- function(x) {
  list(is_nonneg = is_nonneg(.args(x)[[1L]]),
       is_nonpos = is_nonpos(.args(x)[[1L]]))
}

# -- bounds: those of log(1 + x) ----------------------------------
## CVXPY SOURCE: elementwise/log1p.py:43-47 (CVXPY 1.9.2).
## MUST override Log's method: inheriting it computes log(x)'s bounds, so
## [1, 2] gave (log 1, log 2) instead of (log 2, log 3) and, worse, [-1, 0]
## gave an unbounded +Inf upper bound instead of 0.
method(bounds_from_args, Log1p) <- function(x) {
  b <- get_bounds(.args(x)[[1L]])
  log_bounds(b[[1L]] + 1, b[[2L]] + 1)
}

# -- curvature: concave (inherited from Log) ----------------------
# is_atom_convex, is_atom_concave inherited from Log

# -- monotonicity: increasing (inherited from Log) ----------------
# is_incr, is_decr inherited from Log

# -- domain: x >= -1 (NOT x >= 0 like Log) -----------------------
method(atom_domain, Log1p) <- function(x) {
  list(.args(x)[[1L]] >= -1)
}

# -- numeric: log(1 + x) -----------------------------------------
method(numeric_value, Log1p) <- function(x, values, ...) {
  log1p(values[[1L]])
}

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

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/log1p.py:43-63 (log1p._grad).
## d/dx log(1 + x) = 1/(1 + x); domain x > -1. NULL on boundary or below.
method(.grad, Log1p) <- function(x, values, ...) {
  v <- values[[1L]]
  if (min(v) <= -1) return(list(NULL))
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(1 / (v + 1), rows, cols))
}

#' Log(1 + x) -- elementwise
#'
#' @param x An Expression
#' @returns A Log1p atom
#' @export
log1p_atom <- function(x) {
  Log1p(x)
}

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.