R/110_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 31-36 -- 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 60-63 -- returns tuple()
method(shape_from_args, SigmaMax) <- function(x) c(1L, 1L)

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

# -- curvature ----------------------------------------------------
## CVXPY: sigma_max.py lines 71-79 -- 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 81-89 -- not monotone
method(is_incr, SigmaMax) <- function(x, idx, ...) FALSE
method(is_decr, SigmaMax) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: sigma_max.py lines 38-42 -- 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:44-58 (sigma_max._grad).
## d/dX sigma_max(X) = u_1 v_1^H (rank-1 outer product of the top left and
## right singular vectors). CVXPY: U[:, 0:1] @ Vh[0:1, :], where numpy's
## third SVD factor is V^H. R's svd() gives V itself, so the first column of
## v conjugate-transposed, which is t(v_1) for a real X. sigma_max allows
## complex (sigma_max.py:26): a complex X keeps a complex D (claims-audit
## C0254, decision 15, CVXR 1.9.2.9153; `as.numeric` dropped the imaginary
## part).
method(.grad, SigmaMax) <- function(x, values, ...) {
  X <- as.matrix(values[[1L]])
  s <- svd(X)
  D <- s$u[, 1L, drop = FALSE] %*% Conj(t(s$v[, 1L, drop = FALSE]))
  rows <- as.integer(prod(.arg_shape(x)))
  list(.dense_to_csc_vector(as.vector(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 Oct. 19, 2026, 5:07 p.m.