R/110_atoms_norm_nuc.R

Defines functions norm_nuc

Documented in norm_nuc

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

## CVXPY SOURCE: atoms/norm_nuc.py
## NormNuc -- nuclear norm (sum of singular values)


NormNuc <- new_class("NormNuc", 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(NormNuc, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(A),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

# -- validate -----------------------------------------------------
## Must be 2D (same as sigma_max)
method(validate_arguments, NormNuc) <- function(x) {
  A <- .args(x)[[1L]]
  if (length(.shape(A)) != 2L) {
    cli_abort("The argument to {.fn norm_nuc} must be a 2-d matrix.")
  }
  invisible(NULL)
}

# -- shape --------------------------------------------------------
## CVXPY: norm_nuc.py lines 54-57 -- returns tuple()
method(shape_from_args, NormNuc) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## CVXPY: norm_nuc.py lines 59-62 -- always nonneg
method(sign_from_args, NormNuc) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## CVXPY: norm_nuc.py lines 64-72 -- convex, not concave
method(is_atom_convex, NormNuc) <- function(x) TRUE
method(is_atom_concave, NormNuc) <- function(x) FALSE

# -- monotonicity -------------------------------------------------
## CVXPY: norm_nuc.py lines 74-82 -- not monotone
method(is_incr, NormNuc) <- function(x, idx, ...) FALSE
method(is_decr, NormNuc) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: norm_nuc.py lines 33-36 -- sum of singular values
method(numeric_value, NormNuc) <- function(x, values, ...) {
  A <- values[[1L]]
  matrix(sum(svd(A, nu = 0L, nv = 0L)$d), 1L, 1L)
}

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

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

# -- .grad: subgradient of the nuclear norm ----------------------
## CVXPY SOURCE: atoms/norm_nuc.py:38-55 (normNuc._grad).
## d/dX ||X||_* = U V^T where X = U S V^T (SVD).
## CVXPY uses np.linalg.svd(..., full_matrices=False) and returns U.dot(V).
## R's svd() returns the same factors but V (not V^T) — `D = u %*% t(v)`.
method(.grad, NormNuc) <- function(x, values, ...) {
  X <- as.matrix(values[[1L]])
  s <- svd(X)
  D <- s$u %*% t(s$v)
  rows <- as.integer(prod(.arg_shape(x)))
  list(.dense_to_csc_vector(as.numeric(D), rows))
}

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

#' Nuclear norm (sum of singular values)
#' @param A A matrix expression
#' @returns An expression representing the nuclear norm of A
#' @export
norm_nuc <- function(A) {
  NormNuc(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.