R/048_atoms_affine_hstack.R

Defines functions hstack

Documented in hstack

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

## CVXPY SOURCE: atoms/affine/hstack.py
## HStack -- horizontal concatenation of expressions


HStack <- new_class("HStack", 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 HStack} requires at least one argument.")
    }
    ## Compute shape: same rows, sum of cols
    rows <- all_args[[1L]]@shape[1L]
    total_cols <- 0L
    for (arg in all_args) {
      if (arg@shape[1L] != rows) {
        cli_abort("All arguments to {.fn hstack} must have the same number of rows.")
      }
      total_cols <- total_cols + arg@shape[2L]
    }
    shape <- c(rows, total_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, HStack) <- function(x) {
  rows <- x@args[[1L]]@shape[1L]
  total_cols <- sum(vapply(x@args, function(a) a@shape[2L], integer(1)))
  c(rows, total_cols)
}

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

method(numeric_value, HStack) <- function(x, values, ...) {
  do.call(cbind, values)
}

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

#' Horizontal concatenation of expressions
#'
#' @param ... Expressions (same number of rows)
#' @returns An HStack atom
#' @export
hstack <- function(...) {
  HStack(...)
}

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.