R/186_reductions_complex2real_canonicalizers_abs_canon.R

Defines functions c2r_abs_canon

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

## CVXPY SOURCE: reductions/complex2real/canonicalizers/abs_canon.py
## |z| = pnorm(vstack(vec(Re), vec(Im)), p=2, axis=0), reshaped back

c2r_abs_canon <- function(expr, real_args, imag_args, real2imag) {
  if (is.null(real_args[[1L]])) {
    ## Purely imaginary
    output <- Abs(imag_args[[1L]])
  } else if (is.null(imag_args[[1L]])) {
    ## Purely real
    output <- Abs(real_args[[1L]])
  } else {
    ## Complex: |z| = sqrt(Re^2 + Im^2) via pnorm column-wise
    ## CVXPY: flatten() -> shape (n,), vstack -> (2, n), pnorm(axis=0) -> (n,)
    ## R: vec -> (n, 1), so reshape to (1, n) rows first, VStack -> (2, n)
    n <- prod(real_args[[1L]]@shape)
    real_flat <- vec(real_args[[1L]])
    imag_flat <- vec(imag_args[[1L]])
    real_row <- reshape_expr(real_flat, c(1L, n))
    imag_row <- reshape_expr(imag_flat, c(1L, n))
    norms <- Pnorm(VStack(real_row, imag_row), p = 2, axis = 2L)
    output <- reshape_expr(norms, real_args[[1L]]@shape)
  }
  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.