R/113_atoms_pf_eigenvalue.R

Defines functions pf_eigenvalue

Documented in pf_eigenvalue

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

## CVXPY SOURCE: atoms/pf_eigenvalue.py
## PfEigenvalue -- Perron-Frobenius eigenvalue of a positive matrix
## Scalar output, log-log convex (T/F), sign always positive.


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

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

# -- validate -------------------------------------------------------
method(validate_arguments, PfEigenvalue) <- function(x) {
  X <- x@args[[1L]]
  if (length(X@shape) != 2L || X@shape[1L] != X@shape[2L]) {
    cli_abort("The argument to {.fn pf_eigenvalue} must be a square matrix.")
  }
  invisible(NULL)
}

# -- shape: scalar --------------------------------------------------
method(shape_from_args, PfEigenvalue) <- function(x) c(1L, 1L)

# -- sign: always positive -----------------------------------------
method(sign_from_args, PfEigenvalue) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave --------------------------
method(is_atom_convex, PfEigenvalue) <- function(x) FALSE
method(is_atom_concave, PfEigenvalue) <- function(x) FALSE

# -- log-log curvature: convex (T/F) -------------------------------
method(is_atom_log_log_convex, PfEigenvalue) <- function(x) TRUE
method(is_atom_log_log_concave, PfEigenvalue) <- function(x) FALSE

# -- monotonicity: increasing --------------------------------------
method(is_incr, PfEigenvalue) <- function(x, idx, ...) TRUE
method(is_decr, PfEigenvalue) <- function(x, idx, ...) FALSE

# -- numeric --------------------------------------------------------
method(numeric_value, PfEigenvalue) <- function(x, values, ...) {
  eigs <- eigen(values[[1L]], only.values = TRUE)$values
  matrix(max(abs(eigs)), 1L, 1L)
}

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

# -- graph_implementation: stub -------------------------------------
method(graph_implementation, PfEigenvalue) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls PfEigenvalue} not yet implemented.")
}

#' Perron-Frobenius eigenvalue of a positive matrix
#'
#' Log-log convex atom for DGP. Solve with
#' \code{psolve(problem, gp = TRUE)}.
#'
#' @param X An Expression (positive square matrix)
#' @returns A PfEigenvalue atom (scalar)
#' @examples
#' X <- Variable(c(2, 2), pos = TRUE)
#' prob <- Problem(Minimize(pf_eigenvalue(X)),
#'                 list(X[1,1] >= 0.1, X[2,2] >= 0.1))
#' \dontrun{psolve(prob, gp = TRUE)}
#' @export
pf_eigenvalue <- function(X) {
  PfEigenvalue(X)
}

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.