R/070_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 (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    y <- as_expr(y)
    shape <- sum_shapes(list(.shape(x), .shape(y)))
    obj <- .fast_new(KlDiv, 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

## CVXPY elementwise/kl_div.py: kl_div is smooth.
method(is_atom_smooth, KlDiv) <- function(x) TRUE

# -- 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(.args(x)[[1L]] >= 0, .args(x)[[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.")
}

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/kl_div.py:68-92 (kl_div._grad).
## kl_div(x, y) = x*log(x/y) - x + y; domain x > 0, y > 0.
## d/dx = log(x/y); d/dy = 1 - x/y.
method(.grad, KlDiv) <- function(x, values, ...) {
  v0 <- values[[1L]]; v1 <- values[[2L]]
  if (min(v0) <= 0 || min(v1) <= 0) return(list(NULL, NULL))
  div <- v0 / v1
  cols <- as.integer(prod(.shape(x)))
  rows0 <- as.integer(prod(.arg_shape(x)))
  rows1 <- as.integer(prod(.arg_shape(x, 2L)))
  list(
    .elemwise_grad_to_diag(log(div),   rows0, cols),
    .elemwise_grad_to_diag(1 - div,    rows1, cols)
  )
}

#' 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 Aug. 24, 2026, 9:10 a.m.