R/122_atoms_gmatmul.R

Defines functions gmatmul

Documented in gmatmul

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/atoms/gmatmul.R
#####

## CVXPY SOURCE: atoms/gmatmul.py
## Gmatmul -- geometric matrix multiplication A o X
## A is stored as a property (not an arg) to prevent DGP log-transformation.
## Log-log affine (T/T), sign always positive.


Gmatmul <- new_class("Gmatmul", parent = Atom, package = "CVXR",
  properties = list(
    A = class_any  ## constant matrix (stored as Constant, not an arg)
  ),
  constructor = function(A, X, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    A <- as_expr(A)
    X <- as_expr(X)
    ## Shape: matrix multiplication shape A %*% X
    shape <- c(.shape(A)[1L], .shape(X)[2L])

    obj <- .fast_new(Gmatmul, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(X),
      shape = shape,
      A     = A
    )
    validate_arguments(obj)
    obj
  }
)

# -- parameters -----------------------------------------------------
## CVXPY: gmatmul.py:112-117 — override needed because `A` is stored
## as a property (so DGP doesn't log-transform it), not in `args`.
method(parameters, Gmatmul) <- function(x) {
  unique_list(c(
    unlist(lapply(.args(x), parameters), recursive = FALSE),
    parameters(x@A)
  ))
}

# -- validate -------------------------------------------------------
method(validate_arguments, Gmatmul) <- function(x) {
  if (!is_constant(x@A)) {
    cli_abort("{.fn gmatmul} requires that {.arg A} be constant.")
  }
  if (!is_pos(.args(x)[[1L]])) {
    cli_abort("{.fn gmatmul} requires that {.arg X} be positive.")
  }
  ## Check dimension compatibility
  if (.shape(x@A)[2L] != .arg_shape(x)[1L]) {
    cli_abort("Incompatible dimensions: A is {x@A@shape[1L]}x{x@A@shape[2L]} but X is {x@args[[1L]]@shape[1L]}x{x@args[[1L]]@shape[2L]}.")
  }
  invisible(NULL)
}

# -- shape ----------------------------------------------------------
method(shape_from_args, Gmatmul) <- function(x) {
  c(.shape(x@A)[1L], .arg_shape(x)[2L])
}

# -- sign: always positive -----------------------------------------
method(sign_from_args, Gmatmul) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave --------------------------
method(is_atom_convex, Gmatmul) <- function(x) FALSE
method(is_atom_concave, Gmatmul) <- function(x) FALSE

# -- log-log curvature: affine (T/T) -------------------------------
method(is_atom_log_log_convex, Gmatmul) <- function(x) TRUE
method(is_atom_log_log_concave, Gmatmul) <- function(x) {
  is_atom_log_log_convex(x)
}

# -- monotonicity ---------------------------------------------------
method(is_incr, Gmatmul) <- function(x, idx, ...) is_nonneg(x@A)
method(is_decr, Gmatmul) <- function(x, idx, ...) is_nonpos(x@A)

# -- get_data: A is data, not an arg -------------------------------
method(get_data, Gmatmul) <- function(x) list(x@A)

# -- numeric --------------------------------------------------------
method(numeric_value, Gmatmul) <- function(x, values, ...) {
  A_val <- as.matrix(value(x@A))
  logX <- log(values[[1L]])
  exp(A_val %*% logX)
}

# -- graph_implementation: stub -------------------------------------
method(graph_implementation, Gmatmul) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls Gmatmul} not yet implemented.")
}

# -- .grad: not implemented (mirrors CVXPY's [None]) --------------
## CVXPY SOURCE: atoms/gmatmul.py:153-154 (gmatmul._grad).
## CVXPY returns [None] (single-arg signature on the variable side;
## the multiplier A is data not an Expression arg).
method(.grad, Gmatmul) <- function(x, values, ...) {
  list(NULL)
}

#' Geometric matrix multiplication A diamond X
#'
#' Computes the geometric matrix product where (A diamond X)_ij = prod_k X_kj^A_ik.
#' Log-log affine atom for DGP. Solve with
#' \code{psolve(problem, gp = TRUE)}.
#'
#' @param A A constant matrix
#' @param X An Expression (positive matrix)
#' @returns A Gmatmul atom
#' @examples
#' x <- Variable(2, pos = TRUE)
#' A <- matrix(c(1, 0, 0, 1), 2, 2)
#' prob <- Problem(Minimize(sum(gmatmul(A, x))), list(x >= 0.5))
#' \dontrun{psolve(prob, gp = TRUE)}
#' @export
gmatmul <- function(A, X) {
  Gmatmul(A, X)
}

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.