R/037_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) {
    expr <- as_expr(expr)
    new_object(S7_object(),
      id    = next_expr_id(),
      .cache = new.env(parent = emptyenv()),
      args  = list(expr),
      shape = expr@shape
    )
  }
)

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

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

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

method(sign_from_args, NegExpression) <- function(x) {
  c(is_nonneg = is_nonpos(x@args[[1L]]),
    is_nonpos = is_nonneg(x@args[[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(x@args[[1L]])
method(is_hermitian, NegExpression) <- function(x) is_hermitian(x@args[[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(x@args[[1L]])
  ## Add parens for non-leaf args (args that have sub-args)
  if (length(x@args[[1L]]@args) > 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 March 6, 2026, 9:10 a.m.