R/068_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 (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    shape <- x@shape
    obj <- new_object(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

# -- 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.")
}

#' 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 March 6, 2026, 9:10 a.m.