R/161_reductions_dcp2cone_canonicalizers_logistic_canon.R

Defines functions logistic_canon

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/reductions/dcp2cone/canonicalizers/logistic_canon.R
#####

## CVXPY SOURCE: reductions/dcp2cone/canonicalizers/logistic_canon.py
## log(1 + exp(x)) <= t <=> exp(-t) + exp(x - t) <= 1
## Recursive: calls exp_canon twice


logistic_canon <- function(expr, args, solver_context = NULL) {
  x <- args[[1L]]
  shape <- expr@shape
  t0 <- Variable(shape = shape)
  t1_result <- exp_canon(expr, list(-t0))
  t1 <- t1_result[[1L]]
  constr1 <- t1_result[[2L]]
  t2_result <- exp_canon(expr, list(x - t0))
  t2 <- t2_result[[1L]]
  constr2 <- t2_result[[2L]]
  ones <- Constant(matrix(1, nrow = shape[1L], ncol = shape[2L]))
  constraints <- c(constr1, constr2, list(t1 + t2 <= ones))
  list(t0, constraints)
}

method(dcp_canonicalize, Logistic) <- logistic_canon
method(has_dcp_canon, Logistic) <- function(expr) TRUE

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.