R/065_atoms_elementwise_rel_entr.R

Defines functions rel_entr

Documented in rel_entr

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

## CVXPY SOURCE: atoms/elementwise/rel_entr.py
## RelEntr -- elementwise relative entropy: x*log(x/y)


RelEntr <- new_class("RelEntr", 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: unknown (can be positive or negative) -----------------
method(sign_from_args, RelEntr) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

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

# -- monotonicity: not incr in either; decr in arg 2 -------------
method(is_incr, RelEntr) <- function(x, idx, ...) FALSE
method(is_decr, RelEntr) <- function(x, idx, ...) {
  idx == 2L  # decreasing in second argument (1-indexed: idx==2)
}

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

# -- numeric: x*log(x/y) -----------------------------------------
## Matches scipy.special.rel_entr(x, y)
method(numeric_value, RelEntr) <- 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))
  ## rel_entr(x, y) = x*log(x/y) when x > 0, y > 0
  ## rel_entr(0, y) = 0 when y >= 0
  ## rel_entr(x, 0) = Inf when x > 0
  result <- ifelse(xv > 0 & yv > 0,
                   xv * log(xv / yv),
                   ifelse(xv == 0 & yv >= 0, 0, Inf))
  result
}

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

#' Relative Entropy: x*log(x/y)
#'
#' @param x An Expression
#' @param y An Expression
#' @returns A RelEntr atom
#' @export
rel_entr <- function(x, y) {
  RelEntr(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.