R/234_reductions_dnlp2smooth_canonicalizers_quad_over_lin_canon.R

Defines functions .smooth_quad_over_lin_canon

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

## CVXPY SOURCE: reductions/dnlp2smooth/canonicalizers/quad_over_lin_canon.py
## If the denominator is constant, reuse the power canonicalizer. Otherwise
## introduce fresh variables for numerator/denominator (denominator nonneg,
## initialized to 1, a point in the domain).

.dnlp_qol_MIN_INIT <- 1

.smooth_quad_over_lin_canon <- function(expr, args) {
  if (is_constant(args[[2L]])) {
    p2 <- power(args[[1L]], 2)
    p_res <- .smooth_power_canon(p2, .args(p2))
    summation <- SumEntries(p_res[[1L]])
    return(list((1 / as.numeric(value(args[[2L]]))) * summation, p_res[[2L]]))
  } else {
    t1 <- args[[1L]]
    constraints <- list()
    if (!.s7_is(t1, Variable)) {
      t1 <- Variable(.shape(args[[1L]]))
      constraints <- c(constraints, list(t1 == args[[1L]]))
      v1 <- value(args[[1L]])
      if (!is.null(v1)) value(t1) <- v1
    }
    ## always introduce a fresh denominator variable so it can start at 1
    t2 <- Variable(.shape(args[[2L]]), nonneg = TRUE)
    constraints <- c(constraints, list(t2 == args[[2L]]))
    v2 <- value(args[[2L]])
    if (!is.null(v2)) value(t2) <- pmax(v2, .dnlp_qol_MIN_INIT)
    list(expr_copy(expr, list(t1, t2)), constraints)
  }
}

Try the CVXR package in your browser

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

CVXR documentation built on Aug. 24, 2026, 9:10 a.m.