R/117_atoms_condition_number.R

Defines functions condition_number

Documented in condition_number

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

## CVXPY SOURCE: atoms/condition_number.py
## ConditionNumber -- condition number of a PSD matrix: lambda_max(A) / lambda_min(A)


ConditionNumber <- new_class("ConditionNumber", parent = Atom, package = "CVXR",
  constructor = function(A, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    A <- as_expr(A)
    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: condition_number.py lines 59-65
method(validate_arguments, ConditionNumber) <- function(x) {
  A <- x@args[[1L]]
  if (length(A@shape) != 2L || A@shape[1L] != A@shape[2L]) {
    cli_abort("The argument to {.fn condition_number} must be a square matrix, got shape ({A@shape[1L]}, {A@shape[2L]}).")
  }
  invisible(NULL)
}

# -- shape --------------------------------------------------------
## CVXPY: condition_number.py lines 67-70 -- returns tuple()
method(shape_from_args, ConditionNumber) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## CVXPY: condition_number.py lines 72-75 -- (True, False) -- always nonneg
method(sign_from_args, ConditionNumber) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## CVXPY: condition_number.py lines 77-89 -- not convex, not concave, quasiconvex
method(is_atom_convex, ConditionNumber) <- function(x) FALSE
method(is_atom_concave, ConditionNumber) <- function(x) FALSE
method(is_atom_quasiconvex, ConditionNumber) <- function(x) TRUE

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

# -- numeric ------------------------------------------------------
## CVXPY: condition_number.py lines 31-39
method(numeric_value, ConditionNumber) <- function(x, values, ...) {
  A <- values[[1L]]
  evals <- .eigvalsh(A, only_values = TRUE)$values
  ## eigenvalues in decreasing order; max is first, min is last
  matrix(evals[1L] / evals[length(evals)], 1L, 1L)
}

# -- domain -------------------------------------------------------
## CVXPY: condition_number.py lines 41-44 -- [A.H == A, A >> 0]
method(atom_domain, ConditionNumber) <- function(x) {
  A <- x@args[[1L]]
  list(expr_H(A) == A, PSD(A))
}

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

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

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

#' Condition number of a PSD matrix
#'
#' Computes the condition number lambda_max(A) / lambda_min(A) for a
#' positive semidefinite matrix A. This is a quasiconvex atom.
#'
#' @param A A square matrix expression (must be PSD)
#' @returns An expression representing the condition number of A
#' @export
condition_number <- function(A) {
  ConditionNumber(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.