R/220_reductions_dgp2dcp_canonicalizers_geo_mean_canon.R

Defines functions .dgp_geo_mean_canon

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

## CVXPY SOURCE: reductions/dgp2dcp/canonicalizers/geo_mean_canon.py
## DGP canonicalizer for GeoMean: geo_mean(x, p) -> (1/sum(p)) * sum(p_i * x_i)
## In log-space, geometric mean is affine (weighted average of logs).

## CVXPY SOURCE: dgp2dcp/canonicalizers/geo_mean_canon.py lines 4-14
## Reduced entries along the rows of the aligned view, zero-weight ones
## already dropped, so the weights line up with the rows whatever the axis.
.dgp_geo_mean_canon <- function(expr, args) {
  p_vec <- expr@p
  x <- .gm_aligned_arg(expr, args[[1L]])
  n <- length(p_vec)
  out <- p_vec[1L] * x[1L, ]
  if (n > 1L) {
    for (i in 2L:n) {
      out <- out + p_vec[i] * x[i, ]
    }
  }
  result <- (1.0 / sum(p_vec)) * out
  list(.gm_to_shape(result, .shape(expr)), list())
}

Try the CVXR package in your browser

Any scripts or data that you put into this service are public.

CVXR documentation built on Oct. 19, 2026, 5:07 p.m.