R/152_reductions_dcp2cone_canonicalizers_geo_mean_canon.R

Defines functions geo_mean_approx_canon geo_mean_exact_canon

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

## CVXPY SOURCE: reductions/dcp2cone/canonicalizers/geo_mean_canon.py
## Exact: Uses PowConeND constraints
## Approx: Uses gm_constrs (SOC constraints via rational approximation)


# -- geo_mean_exact_canon ------------------------------------------
## CVXPY SOURCE: dcp2cone/canonicalizers/geo_mean_canon.py lines 29-48
geo_mean_exact_canon <- function(expr, args, solver_context = NULL) {
  x <- args[[1L]]
  w <- expr@w

  ## Single non-zero weight: geo_mean is just that element (affine)
  if (length(w) == 1L) return(list(x, list()))

  shape <- expr@shape
  t <- Variable(shape = shape)

  ## Build x_list from indexing
  x_list <- lapply(seq_along(w), function(i) x[i])

  ## VStack for column assembly (R 2D shapes -> use VStack)
  W <- do.call(VStack, x_list)
  alpha <- matrix(as.numeric(w), ncol = 1L)
  list(t, list(PowConeND(W, t, alpha, axis = 2L)))
}

# -- geo_mean_approx_canon ----------------------------------------
## CVXPY SOURCE: dcp2cone/canonicalizers/geo_mean_canon.py lines 51-88
## Uses gm_constrs (SOC constraints via rational approximation)
geo_mean_approx_canon <- function(expr, args, solver_context = NULL) {
  x <- args[[1L]]
  w <- expr@w

  ## Single non-zero weight: geo_mean is just that element (affine)
  if (length(w) == 1L) return(list(x, list()))

  shape <- expr@shape
  t <- Variable(shape = shape)

  ## Build x_list from indexing
  x_list <- lapply(seq_along(w), function(i) x[i])

  constrs <- gm_constrs(t, x_list, w)
  list(t, constrs)
}

method(dcp_canonicalize, GeoMean) <- geo_mean_exact_canon
method(has_dcp_canon, GeoMean) <- function(expr) TRUE
method(dcp_canonicalize, GeoMeanApprox) <- geo_mean_approx_canon
method(has_dcp_canon, GeoMeanApprox) <- function(expr) TRUE

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.