R/031_atoms_affine_imag.R

Defines functions imag_expr

Documented in imag_expr

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

## CVXPY SOURCE: atoms/affine/imag.py
## Imag_ -- extract the imaginary part of an expression


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

    new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(expr),
      shape = shape
    )
  }
)

# -- numeric_value: R's native Im() ----------------------------------
## CVXPY SOURCE: imag.py lines 29-32

method(numeric_value, Imag_) <- function(x, values, ...) {
  Im(values[[1L]])
}

# -- shape_from_args --------------------------------------------------
## CVXPY SOURCE: imag.py lines 34-37

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

# -- Complex queries: imaginary part is always real -------------------
## CVXPY SOURCE: imag.py lines 39-47

method(is_imag, Imag_) <- function(x) FALSE
method(is_complex, Imag_) <- function(x) FALSE

# -- Symmetry: Im(Hermitian) is skew-symmetric, but that's symmetric? -
## CVXPY SOURCE: imag.py lines 49-52
## CVXPY returns self.args[0].is_hermitian(), meaning
## if the argument is Hermitian, then Im(A) is symmetric.
## (For Hermitian A, Im(A) is skew-symmetric, i.e. Im(A)^T = -Im(A),
## but CVXPY defines is_symmetric for Imag the same way as Real.)

method(is_symmetric, Imag_) <- function(x) {
  is_hermitian(x@args[[1L]])
}

# -- No graph_implementation: consumed by Complex2Real reduction ------
## CVXPY imag.py has no graph_implementation method.

#' Extract Imaginary Part of Expression
#'
#' Returns the imaginary part of a complex expression. R's native \code{Im()}
#' dispatches here for CVXR expressions via the Complex S3 group handler.
#'
#' @param expr A CVXR Expression.
#' @returns An \code{Imag_} atom.
#' @keywords internal
imag_expr <- function(expr) Imag_(expr)

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.