R/239_reductions_dnlp2smooth_canonicalizers_huber_canon.R

Defines functions .smooth_huber_canon

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

## CVXPY SOURCE: reductions/dnlp2smooth/canonicalizers/huber_canon.py
## Identical to the dcp2cone version except it uses the dnlp2smooth power
## canonicalizer (and reuses the shared abs canonicalizer). Smooth epigraph:
## huber(x; M) = n^2 + 2*M*|s| with x == s + n.

.smooth_huber_canon <- function(expr, args) {
  M <- expr@M
  x <- args[[1L]]
  shape <- .shape(expr)
  n <- Variable(shape)
  s <- Variable(shape)
  ## initial values (CVXPY mutates x.value to zeros if unset; we use a local
  ## copy since the only use is to seed n/s -- the constraint uses x as an expr)
  xv <- value(x)
  if (is.null(xv)) xv <- matrix(0, .shape(x)[1L], .shape(x)[2L])
  Mv <- as.numeric(value(M))
  ## this init follows the smooth epigraph construction of the huber function
  value(n) <- pmin(abs(xv), Mv) * sign(xv)
  value(s) <- xv - value(n)
  ## n^2 + 2*M*|s|
  power_expr <- power(n, 2)
  ps <- .smooth_power_canon(power_expr, .args(power_expr))
  abs_expr <- Abs(s)
  ## reuse the shared abs canonicalizer (dcp2cone/canonicalizers/abs_canon.R)
  ac <- abs_canon(abs_expr, .args(abs_expr))
  obj <- ps[[1L]] + 2 * M * ac[[1L]]
  ## x == s + n
  constraints <- c(ps[[2L]], ac[[2L]], list(x == s + n))
  list(obj, 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.