R/006_utilities_sign.R

Defines functions mul_sign sum_signs

Documented in mul_sign sum_signs

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/utilities/sign.R
#####

## CVXPY SOURCE: utilities/sign.py
## Sign combination rules for expression arithmetic

#' Sign of a sum of expressions
#'
#' Determines whether the sum of a list of expressions is nonnegative,
#' nonpositive, or unknown.
#'
#' @param exprs List of Expression objects
#' @returns Named logical vector c(is_nonneg, is_nonpos)
#' @keywords internal
sum_signs <- function(exprs) {
  ## CVXPY: sum_signs(exprs) in utilities/sign.py lines 23-34
  ## Sum is nonneg iff ALL summands are nonneg
  ## Sum is nonpos iff ALL summands are nonpos
  is_pos <- all(vapply(exprs, is_nonneg, logical(1)))
  is_neg <- all(vapply(exprs, is_nonpos, logical(1)))
  c(is_nonneg = is_pos, is_nonpos = is_neg)
}

#' Sign of a product of two expressions
#'
#' Determines whether the product of two expressions is nonnegative,
#' nonpositive, or unknown, using the sign multiplication table.
#'
#' @param lh_expr An Expression object (left-hand operand)
#' @param rh_expr An Expression object (right-hand operand)
#' @returns Named logical vector c(is_nonneg, is_nonpos)
#' @keywords internal
mul_sign <- function(lh_expr, rh_expr) {
  ## CVXPY: mul_sign(lh_expr, rh_expr) in utilities/sign.py lines 37-64
  ## ZERO * ANYTHING == ZERO
  ## POSITIVE * POSITIVE == POSITIVE
  ## NEGATIVE * POSITIVE == NEGATIVE
  ## NEGATIVE * NEGATIVE == POSITIVE
  lh_nonneg <- is_nonneg(lh_expr)
  rh_nonneg <- is_nonneg(rh_expr)
  lh_nonpos <- is_nonpos(lh_expr)
  rh_nonpos <- is_nonpos(rh_expr)

  lh_zero <- lh_nonneg && lh_nonpos
  rh_zero <- rh_nonneg && rh_nonpos

  is_zero <- lh_zero || rh_zero

  is_pos <- is_zero || (lh_nonneg && rh_nonneg) || (lh_nonpos && rh_nonpos)
  is_neg <- is_zero || (lh_nonneg && rh_nonpos) || (lh_nonpos && rh_nonneg)
  c(is_nonneg = is_pos, is_nonpos = is_neg)
}

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.