R/116_atoms_length.R

Defines functions length_expr

Documented in length_expr

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

## CVXPY SOURCE: atoms/length.py
## Length -- index of the last nonzero element (1-based), a DQCP atom

# ===================================================================
# Length -- quasiconvex atom: last nonzero index of a vector
# ===================================================================
## CVXPY SOURCE: length.py class length(Atom)

Length <- new_class("Length", parent = Atom, package = "CVXR",
  constructor = function(x) {
    x <- as_expr(x)
    id <- next_expr_id()
    shape <- c(1L, 1L)
    obj <- new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = shape
    )
    validate_arguments(obj)
    obj
  }
)

## validate: argument must be a vector
## CVXPY SOURCE: length.py __init__
method(validate_arguments, Length) <- function(x) {
  arg <- x@args[[1L]]
  if (!is_vector(arg)) {
    cli_abort("{.fn length_expr} can only be applied to vectors.")
  }
  invisible(NULL)
}

## shape: always scalar
## CVXPY SOURCE: length.py shape_from_args
method(shape_from_args, Length) <- function(x) c(1L, 1L)

## sign: always nonnegative
## CVXPY SOURCE: length.py sign_from_args
method(sign_from_args, Length) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

## numeric: index of last nonzero element (1-based)
## CVXPY SOURCE: length.py numeric -- np.max(np.nonzero(|x| > tol)) + 1
method(numeric_value, Length) <- function(x, values, ...) {
  outside_tol <- abs(values[[1L]]) > ATOM_EVAL_TOL
  nz <- which(outside_tol)
  if (length(nz) == 0L) return(0L)
  max(nz)
}

## NOT convex, NOT concave
method(is_atom_convex, Length) <- function(x) FALSE
method(is_atom_concave, Length) <- function(x) FALSE

## NOT log-log
method(is_atom_log_log_convex, Length) <- function(x) FALSE
method(is_atom_log_log_concave, Length) <- function(x) FALSE

## Quasiconvex only (NOT quasiconcave)
method(is_atom_quasiconvex, Length) <- function(x) TRUE
method(is_atom_quasiconcave, Length) <- function(x) FALSE

## Monotonicity: increasing when arg is nonneg, decreasing when arg is nonpos
## CVXPY SOURCE: length.py is_incr/is_decr
method(is_incr, Length) <- function(x, idx, ...) is_nonneg(x@args[[1L]])
method(is_decr, Length) <- function(x, idx, ...) is_nonpos(x@args[[1L]])

## Gradient: undefined (step function)
method(grad, Length) <- function(x) NULL

## get_data: no extra data
method(get_data, Length) <- function(x) NULL

# -- User-facing constructor --------------------------------------

#' Length of a Vector (Last Nonzero Index)
#'
#' Returns the index of the last nonzero element of a vector (1-based).
#' This atom is quasiconvex but NOT convex, so it can only be used in
#' DQCP problems (solved with \code{qcp = TRUE}).
#'
#' @param x A CVXR vector expression.
#' @returns A \code{Length} expression (scalar).
#' @seealso \code{\link{ceil_expr}}, \code{\link{floor_expr}}
#' @export
length_expr <- function(x) Length(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.