R/064_atoms_elementwise_kl_div.R

Defines functions kl_div

Documented in kl_div

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/atoms/elementwise/kl_div.R
#####

## CVXPY SOURCE: atoms/elementwise/kl_div.py
## KlDiv -- elementwise KL divergence: x*log(x/y) - x + y


KlDiv <- new_class("KlDiv", parent = Elementwise, package = "CVXR",
  constructor = function(x, y, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    y <- as_expr(y)
    shape <- sum_shapes(list(x@shape, y@shape))
    obj <- new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x, y),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: always nonneg ------------------------------------------
method(sign_from_args, KlDiv) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature: convex --------------------------------------------
method(is_atom_convex, KlDiv) <- function(x) TRUE
method(is_atom_concave, KlDiv) <- function(x) FALSE

# -- monotonicity: not monotone in either argument ----------------
method(is_incr, KlDiv) <- function(x, idx, ...) FALSE
method(is_decr, KlDiv) <- function(x, idx, ...) FALSE

# -- domain: x >= 0, y >= 0 --------------------------------------
method(atom_domain, KlDiv) <- function(x) {
  list(x@args[[1L]] >= 0, x@args[[2L]] >= 0)
}

# -- numeric: x*log(x/y) - x + y ---------------------------------
## Matches scipy.special.kl_div(x, y)
method(numeric_value, KlDiv) <- function(x, values, ...) {
  xv <- as.matrix(values[[1L]])
  yv <- as.matrix(values[[2L]])
  ## Broadcast scalar to match shape (R doesn't broadcast ifelse/&)
  if (length(yv) == 1L && length(xv) > 1L) yv <- matrix(yv, nrow(xv), ncol(xv))
  if (length(xv) == 1L && length(yv) > 1L) xv <- matrix(xv, nrow(yv), ncol(yv))
  ## kl_div(x, y) = x*log(x/y) - x + y when x > 0, y > 0
  ## kl_div(0, y) = y when y >= 0
  ## kl_div(x, 0) = Inf when x > 0
  result <- ifelse(xv > 0 & yv > 0,
                   xv * log(xv / yv) - xv + yv,
                   ifelse(xv == 0 & yv >= 0, yv, Inf))
  result
}

# -- graph_implementation: stub -----------------------------------
method(graph_implementation, KlDiv) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls KlDiv} not yet implemented.")
}

#' KL Divergence: x*log(x/y) - x + y
#'
#' @param x An Expression
#' @param y An Expression
#' @returns A KlDiv atom
#' @export
kl_div <- function(x, y) {
  KlDiv(x, y)
}

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.