R/043_atoms_affine_unary_operators.R

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

## CVXPY SOURCE: atoms/affine/unary_operators.py
## NegExpression -- negation of an expression: -x


NegExpression <- new_class("NegExpression", parent = AffAtom, package = "CVXR",
  constructor = function(expr) {
    ## NEW_OBJECT GUARD: satisfy S7's check_S7_constructor static check.
    if (FALSE) new_object(S7_object())
    expr <- as_expr(expr)
    .fast_new(NegExpression, S7_object(),
      id    = next_expr_id(),
      .cache = new.env(parent = emptyenv()),
      args  = list(expr),
      shape = .shape(expr)
    )
  }
)

# -- shape_from_args -------------------------------------------------
## CVXPY SOURCE: unary_operators.py:80-83 (#3080).
method(bounds_from_args, NegExpression) <- function(x) {
  b <- get_bounds(.args(x)[[1L]])
  neg_bounds(b[[1L]], b[[2L]])
}

## CVXPY SOURCE: unary_operators.py lines 68-71

method(shape_from_args, NegExpression) <- function(x) .arg_shape(x)

# -- sign: flipped ---------------------------------------------------
## CVXPY SOURCE: unary_operators.py lines 73-76

method(sign_from_args, NegExpression) <- function(x) {
  c(is_nonneg = is_nonpos(.args(x)[[1L]]),
    is_nonpos = is_nonneg(.args(x)[[1L]]))
}

# -- monotonicity: always decreasing ---------------------------------
## CVXPY SOURCE: unary_operators.py lines 78-86

method(is_incr, NegExpression) <- function(x, idx, ...) FALSE
method(is_decr, NegExpression) <- function(x, idx, ...) TRUE

# -- symmetric / hermitian: delegates to arg -------------------------
## CVXPY SOURCE: unary_operators.py lines 88-96

method(is_symmetric, NegExpression) <- function(x) is_symmetric(.args(x)[[1L]])
method(is_hermitian, NegExpression) <- function(x) is_hermitian(.args(x)[[1L]])

# -- numeric_value ---------------------------------------------------
## CVXPY SOURCE: unary_operators.py lines 37-38 (OP_FUNC = op.neg)

method(numeric_value, NegExpression) <- function(x, values, ...) -values[[1L]]

# -- graph_implementation --------------------------------------------
## CVXPY SOURCE: unary_operators.py lines 98-117

method(graph_implementation, NegExpression) <- function(x, arg_objs, shape, data = NULL, ...) {
  list(neg_expr_linop(arg_objs[[1L]]), list())
}

# -- expr_name -------------------------------------------------------
## CVXPY SOURCE: unary_operators.py lines 47-53

method(expr_name, NegExpression) <- function(x) {
  arg_name <- expr_name(.args(x)[[1L]])
  ## Add parens for non-leaf args (args that have sub-args)
  if (length(.args(.args(x)[[1L]])) > 0L) {
    sprintf("-(%s)", arg_name)
  } else {
    sprintf("-%s", arg_name)
  }
}

## CVXPY SOURCE: atoms/affine/unary_operators.py NegExpression.format_labeled.
method(format_labeled, NegExpression) <- function(x) {
  lbl <- label(x); if (!is.null(lbl)) return(lbl)
  arg_name <- format_labeled(.args(x)[[1L]])
  if (length(.args(.args(x)[[1L]])) > 0L) {
    sprintf("-(%s)", arg_name)
  } else {
    sprintf("-%s", arg_name)
  }
}

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.