R/237_reductions_dnlp2smooth_canonicalizers_geo_mean_canon.R

Defines functions .smooth_geo_mean_canon

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

## CVXPY SOURCE: reductions/dnlp2smooth/canonicalizers/geo_mean_canon.py
## geo_mean(x; w) via the log-domain: t >= 0, log(t) == sum(w .* log(x)).

.dnlp_geomean_MIN_INIT <- 1e-3

## CVXPY SOURCE: dnlp2smooth/canonicalizers/geo_mean_canon.py lines 28-44
.smooth_geo_mean_canon <- function(expr, args) {
  t <- Variable(.shape(expr), nonneg = TRUE)
  v <- value(args[[1L]])
  if (!is.null(v)) {
    value(t) <- pmax(numeric_value(expr, list(v)), .dnlp_geomean_MIN_INIT)
  }
  weights <- as.numeric(expr@w)
  log_expr <- Log(args[[1L]])
  ## NOTE (matches CVXPY): log_canon is passed the ORIGINAL expr@args, not the
  ## already-canonicalized `args`.
  lc <- .smooth_log_canon(log_expr, .args(expr))
  var <- lc[[1L]]
  ## Reduced entries along the rows of the aligned view; one weight per row.
  aligned <- .gm_aligned_arg(expr, var)
  W <- matrix(weights, nrow = length(weights), ncol = .shape(aligned)[2L])
  reduced <- .gm_to_shape(SumEntries(W * aligned, axis = 2L), .shape(expr))
  list(t, c(list(Log(t) == reduced), lc[[2L]]))
}

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.