R/109_atoms_sigma_max.R

Defines functions sigma_max

Documented in sigma_max

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

## CVXPY SOURCE: atoms/sigma_max.py
## SigmaMax -- maximum singular value of a matrix


SigmaMax <- new_class("SigmaMax", parent = Atom, package = "CVXR",
  constructor = function(A, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    A <- as_expr(A)
    ## Shape is always scalar
    shape <- c(1L, 1L)

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

# -- validate -----------------------------------------------------
## CVXPY: sigma_max.py lines 33-38 -- must be 2D
method(validate_arguments, SigmaMax) <- function(x) {
  A <- .args(x)[[1L]]
  if (length(.shape(A)) != 2L) {
    cli_abort("The argument to {.fn sigma_max} must be a 2-d matrix.")
  }
  invisible(NULL)
}

# -- shape --------------------------------------------------------
## CVXPY: sigma_max.py lines 64-67 -- returns tuple()
method(shape_from_args, SigmaMax) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## CVXPY: sigma_max.py lines 69-73 -- always nonneg
method(sign_from_args, SigmaMax) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## CVXPY: sigma_max.py lines 75-83 -- convex, not concave
method(is_atom_convex, SigmaMax) <- function(x) TRUE
method(is_atom_concave, SigmaMax) <- function(x) FALSE

# -- monotonicity -------------------------------------------------
## CVXPY: sigma_max.py lines 85-93 -- not monotone
method(is_incr, SigmaMax) <- function(x, idx, ...) FALSE
method(is_decr, SigmaMax) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: sigma_max.py lines 41-44 -- norm(A, 2) = max singular value
method(numeric_value, SigmaMax) <- function(x, values, ...) {
  A <- values[[1L]]
  matrix(max(svd(A, nu = 0L, nv = 0L)$d), 1L, 1L)
}

# -- get_data -----------------------------------------------------
method(get_data, SigmaMax) <- function(x) list()

# -- graph_implementation -----------------------------------------
method(graph_implementation, SigmaMax) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls SigmaMax} not available; use Dcp2Cone canonicalization.")
}

# -- .grad: subgradient of the largest singular value -------------
## CVXPY SOURCE: atoms/sigma_max.py:45-62 (sigma_max._grad).
## d/dX sigma_max(X) = u_1 v_1^T (rank-1 outer product of top
## left and right singular vectors). CVXPY: U[:,0:1] @ V[0:1,:].
## R's svd() gives U and V matrices; first column / first column suffice.
method(.grad, SigmaMax) <- function(x, values, ...) {
  X <- as.matrix(values[[1L]])
  s <- svd(X)
  D <- s$u[, 1L, drop = FALSE] %*% t(s$v[, 1L, drop = FALSE])
  rows <- as.integer(prod(.arg_shape(x)))
  list(.dense_to_csc_vector(as.numeric(D), rows))
}

# ==================================================================
# Convenience function
# ==================================================================

#' Maximum singular value
#' @param A A matrix expression
#' @returns An expression representing the maximum singular value of A
#' @export
sigma_max <- function(A) {
  SigmaMax(A)
}

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.