R/113_atoms_lambda_sum_largest.R

Defines functions lambda_sum_smallest lambda_sum_largest

Documented in lambda_sum_largest lambda_sum_smallest

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

## CVXPY SOURCE: atoms/lambda_sum_largest.py
## LambdaSumLargest -- sum of the k largest eigenvalues of a symmetric matrix
##
## Inherits from LambdaMax conceptually (in CVXPY).
## In S7, no parent delegation, so we replicate structure.
##
## Also provides lambda_sum_smallest() pure function: -lambda_sum_largest(-X, k)


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

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

# -- validate -----------------------------------------------------
## CVXPY: lambda_sum_largest.py lines 33-40
method(validate_arguments, LambdaSumLargest) <- function(x) {
  A <- .args(x)[[1L]]
  if (length(.shape(A)) != 2L || .shape(A)[1L] != .shape(A)[2L]) {
    cli_abort("First argument to {.fn lambda_sum_largest} must be a square matrix.")
  }
  if (x@k <= 0L) {
    cli_abort("Second argument to {.fn lambda_sum_largest} must be a positive integer.")
  }
  invisible(NULL)
}

# -- shape --------------------------------------------------------
method(shape_from_args, LambdaSumLargest) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## Same as lambda_max: (False, False)
method(sign_from_args, LambdaSumLargest) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## Convex (like lambda_max)
method(is_atom_convex, LambdaSumLargest) <- function(x) TRUE
method(is_atom_concave, LambdaSumLargest) <- function(x) FALSE

# -- monotonicity -------------------------------------------------
method(is_incr, LambdaSumLargest) <- function(x, idx, ...) FALSE
method(is_decr, LambdaSumLargest) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: lambda_sum_largest.py lines 42-48
method(numeric_value, LambdaSumLargest) <- function(x, values, ...) {
  A <- values[[1L]]
  evals <- .eigvalsh(A, only_values = TRUE)$values
  ## eigen() returns values in decreasing order; sum the k largest
  matrix(sum(evals[seq_len(x@k)]), 1L, 1L)
}

# -- get_data -----------------------------------------------------
## CVXPY: lambda_sum_largest.py lines 50-52 -- returns [self.k]
method(get_data, LambdaSumLargest) <- function(x) list(x@k)

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

# -- .grad: not implemented (mirrors CVXPY's NotImplementedError) -
## CVXPY SOURCE: atoms/lambda_sum_largest.py:55-69 (lambda_sum_largest._grad).
## CVXPY explicitly raises NotImplementedError. CVXPY test_grad.py
## skip_reason="_grad raises NotImplementedError". Mirror with NULL
## so the chain rule walker propagates NULL up.
method(.grad, LambdaSumLargest) <- function(x, values, ...) {
  list(NULL)
}

# ==================================================================
# Convenience functions
# ==================================================================

#' Sum of largest k eigenvalues
#' @param A A square matrix expression
#' @param k Number of largest eigenvalues to sum (positive integer)
#' @returns An expression representing the sum of the k largest eigenvalues
#' @export
lambda_sum_largest <- function(A, k) {
  LambdaSumLargest(A, k)
}

#' Sum of smallest k eigenvalues
#' @param A A square matrix expression
#' @param k Number of smallest eigenvalues to sum (positive integer)
#' @returns An expression representing the sum of the k smallest eigenvalues
#' @export
lambda_sum_smallest <- function(A, k) {
  -lambda_sum_largest(-as_expr(A), k)
}

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.