R/240_reductions_dnlp2smooth_canonicalizers_log_sum_exp_canon.R

Defines functions .smooth_log_sum_exp_canon

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

## CVXPY SOURCE: reductions/dnlp2smooth/canonicalizers/log_sum_exp_canon.py
## t = log_sum_exp(x)  <=>  exp(t) = sum(exp(x))  <=>  sum(exp(x - t)) = 1.
## Introduce v = x - t (nonpositive).

.smooth_log_sum_exp_canon <- function(expr, args) {
  x <- args[[1L]]
  t <- Variable(.shape(expr))
  v <- Variable(.shape(x), nonpos = TRUE)
  xv <- value(x)
  if (!is.null(xv)) {
    tv <- numeric_value(expr, list(xv))
    value(t) <- tv
    value(v) <- pmin(xv - as.numeric(tv), -1)
  }
  constraints <- list(SumEntries(Exp(v)) == 1, v == x - t)
  list(t, constraints)
}

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.