R/074_atoms_elementwise_logistic.R

Defines functions logistic

Documented in logistic

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

## CVXPY SOURCE: atoms/elementwise/logistic.py
## Logistic -- elementwise log(1 + exp(x))


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

# -- sign: always nonneg ------------------------------------------
method(sign_from_args, Logistic) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

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

## CVXPY elementwise/logistic.py: logistic is smooth.
method(is_atom_smooth, Logistic) <- function(x) TRUE

# -- monotonicity: increasing -------------------------------------
method(is_incr, Logistic) <- function(x, idx, ...) TRUE
method(is_decr, Logistic) <- function(x, idx, ...) FALSE

# -- numeric: log(1 + exp(x)) ------------------------------------
## Using numerically stable logaddexp(0, x)
method(numeric_value, Logistic) <- function(x, values, ...) {
  v <- values[[1L]]
  ## Numerically stable: log(1+exp(x)) = max(0,x) + log(1+exp(-abs(x)))
  pmax(v, 0) + log1p(exp(-abs(v)))
}

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

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/logistic.py:66-87 (logistic._grad).
## d/dx log(1 + exp(x)) = sigmoid(x) = 1 / (1 + exp(-x)),
## computed in numerically stable form per CVXPY's
## exp(values[0] - logaddexp(0, values[0])).
method(.grad, Logistic) <- function(x, values, ...) {
  v <- values[[1L]]
  ## Stable sigmoid: avoid overflow on either tail.
  sig <- ifelse(v >= 0, 1 / (1 + exp(-v)), exp(v) / (1 + exp(v)))
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(sig, rows, cols))
}

#' Logistic function: log(1 + exp(x)) -- elementwise
#'
#' @param x An Expression
#' @returns A Logistic atom
#' @export
logistic <- function(x) {
  Logistic(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.