R/073_atoms_elementwise_xexp.R

Defines functions xexp

Documented in xexp

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

## CVXPY SOURCE: atoms/elementwise/xexp.py
## Xexp -- elementwise x * exp(x)


Xexp <- new_class("Xexp", parent = Elementwise, package = "CVXR",
  constructor = function(x, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    shape <- .shape(x)
    obj <- .fast_new(Xexp, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: tracks argument sign -----------------------------------
method(sign_from_args, Xexp) <- function(x) {
  list(is_nonneg = is_nonneg(.args(x)[[1L]]),
       is_nonpos = is_nonpos(.args(x)[[1L]]))
}

# -- curvature: conditionally convex (only when arg nonneg) -------
method(is_atom_convex, Xexp) <- function(x) {
  is_nonneg(.args(x)[[1L]])
}
method(is_atom_concave, Xexp) <- function(x) FALSE

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

# -- monotonicity: always increasing ------------------------------
method(is_incr, Xexp) <- function(x, idx, ...) TRUE
method(is_decr, Xexp) <- function(x, idx, ...) FALSE

# -- log-log curvature --------------------------------------------
method(is_atom_log_log_convex, Xexp) <- function(x) TRUE
method(is_atom_log_log_concave, Xexp) <- function(x) FALSE

# -- domain: x >= 0 ----------------------------------------------
method(atom_domain, Xexp) <- function(x) {
  list(.args(x)[[1L]] >= 0)
}

# -- numeric: x * exp(x) -----------------------------------------
method(numeric_value, Xexp) <- function(x, values, ...) {
  values[[1L]] * exp(values[[1L]])
}

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

# -- .grad: per-atom subgradient ----------------------------------
## CVXPY SOURCE: atoms/elementwise/xexp.py:73-93 (xexp._grad).
## d/dx (x * exp(x)) = exp(x) * (1 + x); domain x >= 0. NULL below.
method(.grad, Xexp) <- function(x, values, ...) {
  v <- values[[1L]]
  if (min(v) < 0) return(list(NULL))
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(exp(v) * (1 + v), rows, cols))
}

#' x * exp(x) -- elementwise
#'
#' @param x An Expression
#' @returns An Xexp atom
#' @export
xexp <- function(x) {
  Xexp(x)
}

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.