R/049_atoms_affine_vstack.R

Defines functions vstack

Documented in vstack

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

## CVXPY SOURCE: atoms/affine/vstack.py
## VStack -- vertical concatenation of expressions


VStack <- new_class("VStack", parent = AffAtom, package = "CVXR",
  constructor = function(..., id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    all_args <- lapply(list(...), as_expr)
    if (length(all_args) == 0L) {
      cli_abort("{.cls VStack} requires at least one argument.")
    }
    ## Compute shape: sum of rows, same cols
    cols <- all_args[[1L]]@shape[2L]
    total_rows <- 0L
    for (arg in all_args) {
      if (arg@shape[2L] != cols) {
        cli_abort("All arguments to {.fn vstack} must have the same number of columns.")
      }
      total_rows <- total_rows + arg@shape[1L]
    }
    shape <- c(total_rows, cols)

    obj <- new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = all_args,
      shape = shape
    )
    obj
  }
)

method(shape_from_args, VStack) <- function(x) {
  cols <- x@args[[1L]]@shape[2L]
  total_rows <- sum(vapply(x@args, function(a) a@shape[1L], integer(1)))
  c(total_rows, cols)
}

# -- log-log: affine (CVXPY vstack.py) ---------------------------
method(is_atom_log_log_convex, VStack) <- function(x) TRUE
method(is_atom_log_log_concave, VStack) <- function(x) TRUE

method(numeric_value, VStack) <- function(x, values, ...) {
  do.call(rbind, values)
}

method(graph_implementation, VStack) <- function(x, arg_objs, shape, data = NULL, ...) {
  list(vstack_linop(arg_objs, shape), list())
}

#' Vertical concatenation of expressions
#'
#' @param ... Expressions (same number of columns)
#' @returns A VStack atom
#' @export
vstack <- function(...) {
  VStack(...)
}

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.