R/190_reductions_complex2real_canonicalizers_soc_canon.R

Defines functions c2r_soc_canon

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/reductions/complex2real/canonicalizers/soc_canon.R
#####

## CVXPY SOURCE: reductions/complex2real/canonicalizers/soc_canon.py
## SOC constraint: ||z||_2 <= t -> auxiliary SOC via real/imag stacking

c2r_soc_canon <- function(expr, real_args, imag_args, real2imag) {
  if (is.null(real_args[[2L]])) {
    ## Purely imaginary X
    output <- list(SOC(real_args[[1L]], imag_args[[2L]],
                       axis = expr@axis,
                       constr_id = real2imag[[as.character(expr@id)]]))
  } else if (is.null(imag_args[[2L]])) {
    ## Purely real X
    output <- list(SOC(real_args[[1L]], real_args[[2L]],
                       axis = expr@axis,
                       constr_id = expr@id))
  } else {
    ## Complex X: introduce auxiliary variable
    ## In CVXPY, flatten() returns 1-D (n,) and vstack([a, b]) produces (2, n).
    ## In R, vec() returns (n, 1). We transpose to (1, n) row vectors so
    ## VStack produces (2, n), matching CVXPY's shape semantics for SOC axis=0.
    orig_shape <- real_args[[2L]]@shape
    real_flat <- t(vec(real_args[[2L]]))
    imag_flat <- t(vec(imag_args[[2L]]))
    flat_X <- Variable(real_flat@shape)
    inner_SOC <- SOC(flat_X,
                     do.call(VStack, list(real_flat, imag_flat)),
                     axis = 2L)
    real_X <- reshape_expr(flat_X, orig_shape)
    outer_SOC <- SOC(real_args[[1L]], real_X,
                     axis = expr@axis,
                     constr_id = expr@id)
    output <- list(inner_SOC, outer_SOC)
  }
  list(output, NULL)
}

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.