R/259_zzz_R_specific_masking.R

Defines functions outer var sd norm

Documented in norm outer sd var

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/zzz_R_specific/masking.R
#####

## R-SPECIFIC: Type-dispatching wrappers that mask base/stats functions
## These give CVXR its DSL feel: norm(x), sd(x), var(x), outer(x, y) Just Work.
##
## norm() cannot use S7 method() because base::norm is a plain function
## (not S3 generic). It becomes S4 only when Matrix loads, but S7's method<-
## requires an S3 generic at source-evaluation time.

#' Matrix norm
#'
#' For CVXR expressions, computes the matrix/vector norm atom.
#' For other inputs, falls through to \code{\link[base]{norm}}.
#'
#' @param x An Expression or matrix.
#' @param type Norm type: \code{"1"}, \code{"2"} (default), \code{"I"}/\code{"i"}
#'   (infinity), \code{"F"}/\code{"f"} (Frobenius).
#' @param ... Additional arguments (unused for expressions).
#' @returns An Expression or numeric value.
#' @seealso \code{\link{cvxr_norm}} for the full-featured version with
#'   \code{axis} and \code{keepdims} arguments
#' @rdname math_atoms
#' @export
norm <- function(x, type = "2", ...) {
  if (inherits(x, "CVXR::Expression")) {
    p <- switch(type,
      "1" = 1, "2" = 2, "I" =, "i" = Inf, "F" =, "f" = "fro",
      as.numeric(type)
    )
    cvxr_norm(x, p = p)
  } else {
    base::norm(x, type, ...)
  }
}

#' Standard deviation
#'
#' For CVXR expressions, computes the standard deviation atom (ddof=0 by default,
#' matching CVXPY/numpy convention). For numeric inputs, falls through to
#' \code{\link[stats]{sd}}.
#'
#' @param x An Expression or numeric vector.
#' @param ... For expressions: passed to \code{\link{cvxr_std}}
#'   (axis, keepdims, ddof). For numeric: passed to \code{\link[stats]{sd}}.
#' @returns An Expression or numeric value.
#' @seealso \code{\link{cvxr_std}} for the full-featured version
#' @rdname math_atoms
#' @export
sd <- function(x, ...) {
  if (inherits(x, "CVXR::Expression")) cvxr_std(x, ...) else stats::sd(x, ...)
}

#' Variance
#'
#' For CVXR expressions, computes the variance atom (ddof=0 by default).
#' For numeric inputs, falls through to \code{\link[stats]{var}}.
#'
#' @param x An Expression or numeric.
#' @param ... For expressions: passed to \code{\link{cvxr_var}}.
#'   For numeric: passed to \code{\link[stats]{var}}.
#' @returns An Expression or numeric value.
#' @seealso \code{\link{cvxr_var}} for the full-featured version
#' @rdname math_atoms
#' @export
var <- function(x, ...) {
  if (inherits(x, "CVXR::Expression")) cvxr_var(x, ...) else stats::var(x, ...)
}

#' Outer product
#'
#' For CVXR expressions, computes the outer product of two vectors.
#' For other inputs, falls through to \code{\link[base]{outer}}.
#'
#' @param X An Expression or numeric.
#' @param Y An Expression or numeric.
#' @param ... For non-Expression inputs: passed to \code{\link[base]{outer}}.
#' @returns An Expression or matrix.
#' @seealso \code{\link{cvxr_outer}} for the CVXR-specific version
#' @rdname math_atoms
#' @export
outer <- function(X, Y, ...) {
  if (inherits(X, "CVXR::Expression") || inherits(Y, "CVXR::Expression")) {
    cvxr_outer(X, Y)
  } else {
    base::outer(X, Y, ...)
  }
}

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.