R/111_atoms_one_minus_pos.R

Defines functions diff_pos one_minus_pos

Documented in diff_pos one_minus_pos

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

## CVXPY SOURCE: atoms/one_minus_pos.py
## OneMInusPos -- 1 - x with domain {x : 0 < x < 1}
## Log-log concave (F/T), sign always positive.


OneMInusPos <- new_class("OneMInusPos", parent = Atom, package = "CVXR",
  constructor = function(x, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    shape <- x@shape

    obj <- new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- shape ----------------------------------------------------------
method(shape_from_args, OneMInusPos) <- function(x) x@args[[1L]]@shape

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

# -- curvature: neither convex nor concave --------------------------
method(is_atom_convex, OneMInusPos) <- function(x) FALSE
method(is_atom_concave, OneMInusPos) <- function(x) FALSE

# -- log-log curvature: concave only (F/T) -------------------------
method(is_atom_log_log_convex, OneMInusPos) <- function(x) FALSE
method(is_atom_log_log_concave, OneMInusPos) <- function(x) TRUE

# -- monotonicity: decreasing --------------------------------------
method(is_incr, OneMInusPos) <- function(x, idx, ...) FALSE
method(is_decr, OneMInusPos) <- function(x, idx, ...) TRUE

# -- numeric --------------------------------------------------------
method(numeric_value, OneMInusPos) <- function(x, values, ...) {
  ones <- matrix(1, nrow = x@shape[1L], ncol = x@shape[2L])
  ones - values[[1L]]
}

# -- get_data -------------------------------------------------------
method(get_data, OneMInusPos) <- function(x) list()

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

#' The difference 1 - x with domain (0, 1)
#'
#' Log-log concave atom for DGP. Solve with
#' \code{psolve(problem, gp = TRUE)}.
#'
#' @param x An Expression (elementwise in (0, 1))
#' @returns A OneMInusPos atom
#' @examples
#' x <- Variable(pos = TRUE)
#' prob <- Problem(Maximize(one_minus_pos(x)), list(x >= 0.1, x <= 0.5))
#' \dontrun{psolve(prob, gp = TRUE)}
#' @export
one_minus_pos <- function(x) {
  OneMInusPos(x)
}

#' The difference x - y with domain x > y > 0
#'
#' Equivalent to x * one_minus_pos(y / x).
#'
#' @param x An Expression (positive)
#' @param y An Expression (positive, elementwise less than x)
#' @returns A product expression
#' @export
diff_pos <- function(x, y) {
  x * one_minus_pos(y / x)
}

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.