R/178_reductions_dcp2cone_canonicalizers_quad_quad_over_lin_canon.R

Defines functions qol_quad_canon

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

## CVXPY SOURCE: reductions/dcp2cone/canonicalizers/quad/quad_over_lin_canon.py
## Scalar case only (axis=NULL). Builds I/y as the P matrix.
## Non-scalar with axis falls back to cone canon via is_qpwa() guard.


qol_quad_canon <- function(expr, args, solver_context = NULL) {
  affine_expr <- args[[1L]]
  y <- args[[2L]]
  ## y must be constant scalar (guaranteed by is_qpwa guard)
  y_val <- as.numeric(value(y))
  n <- prod(affine_expr@shape)
  quad_mat <- Matrix::Diagonal(n) / y_val

  if (S7_inherits(affine_expr, Variable)) {
    list(SymbolicQuadForm(affine_expr, Constant(as.matrix(quad_mat)), expr), list())
  } else {
    t <- Variable(shape = affine_expr@shape)
    list(SymbolicQuadForm(t, Constant(as.matrix(quad_mat)), expr), list(affine_expr == t))
  }
}

method(quad_canonicalize, QuadOverLin) <- function(expr, args, ...) {
  if (!is_qpwa(expr)) return(NULL)
  qol_quad_canon(expr, args)
}

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.