R/119_atoms_dist_ratio.R

Defines functions dist_ratio

Documented in dist_ratio

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

## CVXPY SOURCE: atoms/dist_ratio.py
## DistRatio -- distance ratio: norm(x - a)_2 / norm(x - b)_2


DistRatio <- new_class("DistRatio", parent = Atom, package = "CVXR",
  properties = list(
    a = class_any,
    b = class_any
  ),
  constructor = function(x, a, b, id = NULL) {
    if (is.null(id)) id <- next_expr_id()
    x_expr <- as_expr(x)
    a_expr <- as_expr(a)
    b_expr <- as_expr(b)

    ## CVXPY: a and b must be constants (validated after construction)
    if (!is_constant(a_expr)) {
      cli_abort("{.arg a} must be a constant in {.fn dist_ratio}.")
    }
    if (!is_constant(b_expr)) {
      cli_abort("{.arg b} must be a constant in {.fn dist_ratio}.")
    }

    shape <- c(1L, 1L)

    obj <- new_object(S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x_expr, a_expr, b_expr),
      shape = shape,
      a     = value(a_expr),
      b     = value(b_expr)
    )
    obj
  }
)

# -- shape --------------------------------------------------------
## CVXPY: dist_ratio.py lines 43-46 -- returns tuple()
method(shape_from_args, DistRatio) <- function(x) c(1L, 1L)

# -- sign ---------------------------------------------------------
## CVXPY: dist_ratio.py lines 48-52 -- (True, False) -- always nonneg
method(sign_from_args, DistRatio) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature ----------------------------------------------------
## CVXPY: dist_ratio.py lines 54-72 -- not convex, not concave,
## quasiconvex but NOT quasiconcave
method(is_atom_convex, DistRatio) <- function(x) FALSE
method(is_atom_concave, DistRatio) <- function(x) FALSE
method(is_atom_quasiconvex, DistRatio) <- function(x) TRUE
method(is_atom_quasiconcave, DistRatio) <- function(x) FALSE

# -- monotonicity -------------------------------------------------
## CVXPY: dist_ratio.py lines 74-82 -- not monotone
method(is_incr, DistRatio) <- function(x, idx, ...) FALSE
method(is_decr, DistRatio) <- function(x, idx, ...) FALSE

# -- numeric ------------------------------------------------------
## CVXPY: dist_ratio.py lines 37-41
method(numeric_value, DistRatio) <- function(x, values, ...) {
  xv <- values[[1L]]
  a <- x@a
  b <- x@b
  matrix(sqrt(sum((xv - a)^2)) / sqrt(sum((xv - b)^2)), 1L, 1L)
}

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

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

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

#' Distance ratio
#'
#' Computes norm(x - a)_2 / norm(x - b)_2, where a and b are constants.
#' This is a quasiconvex atom.
#'
#' @param x A vector expression
#' @param a A numeric constant vector
#' @param b A numeric constant vector
#' @returns An expression representing the distance ratio
#' @export
dist_ratio <- function(x, a, b) {
  DistRatio(x, a, b)
}

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.