R/154_reductions_dcp2cone_canonicalizers_kl_div_canon.R

Defines functions kl_div_canon

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

## CVXPY SOURCE: reductions/dcp2cone/canonicalizers/kl_div_canon.py
## kl_div(x, y) = y - x - t where ExpCone(t, x, y)


kl_div_canon <- function(expr, args, solver_context = NULL) {
  shape <- expr@shape
  x <- cvxr_promote(args[[1L]], shape)
  y <- cvxr_promote(args[[2L]], shape)
  t <- Variable(shape = shape)
  constraints <- list(ExpCone(t, x, y))
  obj <- y - x - t
  list(obj, constraints)
}

method(dcp_canonicalize, KlDiv) <- kl_div_canon
method(has_dcp_canon, KlDiv) <- 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.