R/100_atoms_lambda_max.R

Defines functions lambda_min lambda_max

Documented in lambda_max lambda_min

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

## CVXPY SOURCE: atoms/lambda_max.py
## LambdaMax -- maximum eigenvalue of a symmetric matrix
##
## Also provides lambda_min() pure function: -lambda_max(-X)


LambdaMax <- new_class("LambdaMax", parent = Atom, package = "CVXR",
  constructor = function(A, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    A <- as_expr(A)
    ## Shape is always scalar
    shape <- c(1L, 1L)

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

# -- validate -----------------------------------------------------
## CVXPY: lambda_max.py lines 64-69
method(validate_arguments, LambdaMax) <- function(x) {
  A <- x@args[[1L]]
  if (length(A@shape) != 2L || A@shape[1L] != A@shape[2L]) {
    cli_abort("The argument to {.fn lambda_max} must be a square matrix, got shape ({A@shape[1L]}, {A@shape[2L]}).")
  }
  invisible(NULL)
}

# -- shape --------------------------------------------------------
## CVXPY: lambda_max.py lines 71-74 -- returns tuple()
method(shape_from_args, LambdaMax) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## CVXPY: lambda_max.py lines 76-79 -- (False, False)
method(sign_from_args, LambdaMax) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## CVXPY: lambda_max.py lines 81-89 -- convex, not concave
method(is_atom_convex, LambdaMax) <- function(x) TRUE
method(is_atom_concave, LambdaMax) <- function(x) FALSE

# -- monotonicity -------------------------------------------------
## CVXPY: lambda_max.py lines 91-99 -- not monotone
method(is_incr, LambdaMax) <- function(x, idx, ...) FALSE
method(is_decr, LambdaMax) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: lambda_max.py lines 33-39
method(numeric_value, LambdaMax) <- function(x, values, ...) {
  A <- values[[1L]]
  evals <- .eigvalsh(A, only_values = TRUE)$values
  ## eigen() returns values in decreasing order; largest is first
  matrix(evals[1L], 1L, 1L)
}

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

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

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

#' Maximum eigenvalue
#' @param A A square matrix expression
#' @returns An expression representing the maximum eigenvalue of A
#' @export
lambda_max <- function(A) {
  LambdaMax(A)
}

#' Minimum eigenvalue
#' @param A A square matrix expression
#' @returns An expression representing the minimum eigenvalue of A
#' @export
lambda_min <- function(A) {
  -lambda_max(-as_expr(A))
}

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.