
Standard R Functions for CVXR Expressions
Source:R/303_zzz_R_specific_math_atoms.R, R/304_zzz_R_specific_masking.R
math_atoms.RdCVXR registers methods so that standard R functions create the
appropriate atoms when applied to Expression objects.
For CVXR expressions, computes the norm atom with the same meaning as
base R norm for every type: a vector is a one-column
or one-row matrix. For any other input the call is forwarded as written
to Matrix::norm, so the result is exactly base R's (including its
default type "O").
For CVXR expressions, computes the standard deviation atom (ddof=0 by default,
matching CVXPY/numpy convention). For numeric inputs, falls through to
sd.
For CVXR expressions, computes the variance atom (ddof=0 by default).
For numeric inputs, falls through to var.
For CVXR expressions, computes the outer product of two vectors.
For other inputs, falls through to outer.
For CVXR expressions, dispatches to DiagVec (vector to
diagonal matrix) or DiagMat (extract diagonal from matrix),
matching CVXPY's cp.diag() behavior. For other inputs, falls
through to Matrix::diag, called exactly as written, which
dispatches via S4 for both Matrix and base matrix objects.
Usage
norm(x, type = "2", ...)
sd(x, ...)
var(x, ...)
outer(X, Y, ...)
diag(x = 1, nrow, ncol, names = TRUE, k = NULL)Arguments
- x
An Expression, matrix, vector, or scalar.
- type
Norm type, as in base R:
"O"/"o"/"1"(maximum absolute column sum),"I"/"i"(maximum absolute row sum),"F"/"f"/"E"/"e"(Frobenius),"M"/"m"(largest absolute entry),"2"(largest singular value; the default for an expression only). For an expression, any other value is passed tocvxr_normasp.- ...
For non-Expression inputs: passed to
outer. For expressions, onlyFUN = "*"is accepted; any otherFUNor argument is an error.- X
An Expression or numeric.
- Y
An Expression or numeric.
- nrow
For non-Expression: passed to
Matrix::diag; an error with an Expression.- ncol
For non-Expression: passed to
Matrix::diag; an error with an Expression.- names
For non-Expression: passed to
Matrix::diag; an error with an Expression.- k
Integer diagonal offset for Expressions only.
NULL(default) means0, the main diagonal.
Value
An Expression or numeric value.
An Expression or numeric value.
An Expression or numeric value.
An Expression or matrix.
An Expression, matrix, or vector.
Details
The k parameter (off-diagonal offset) is only available for
Expression inputs; giving it with a non-Expression x is an error.
Math group (elementwise, via S3 group generic)
abs(x)Absolute value (convex, nonneg)
exp(x)Exponential (convex, positive)
log(x)Natural logarithm (concave, domain x >= 0)
sqrt(x)Square root via
power(x, 0.5)(concave)log1p(x)log(1+x) compound expression (concave)
log2(x),log10(x)Base-2/10 logarithm
cumsum(x)Cumulative sum (affine)
cummax(x)Cumulative max (convex)
cumprod(x)Cumulative product
ceiling(x),floor(x)Round up/down (quasilinear, DQCP). As in CVXPY, the value rounds
xto 4 decimal places first, sofloor(-3e-10)of an expression is 0, not -1.sign(x)Sign (quasilinear for a scalar, DQCP). As in CVXPY, it is -1 for
x <= 0, sosign(0)of an expression is -1, not 0.
On a matrix expression, cumsum(), cummax() and
cumprod() run down each column, as in CVXPY; use
cumsum_axis(x, axis = NULL) for all entries in column-major
order.
Summary group (via S3 group generic)
sum(x)Sum all entries (affine)
max(x)Maximum entry (convex)
min(x)Minimum entry (concave)
prod(x)Product of entries
Each also takes axis and keepdims, as in CVXPY:
sum(x, axis = 2) is sum_entries(x, axis = 2), and
likewise max_entries, min_entries and
prod_entries.
S3 generic methods
mean(x)Arithmetic mean; pass
axis/keepdimsvia...diff(x)First-order differences; also
cvxr_diff
Masking wrappers
These mask the base/stats versions and dispatch on argument type:
norm(x)2-norm; use
typefor "1", "I" (infinity), "F" (Frobenius)sd(x)Standard deviation (ddof=0 for expressions)
var(x)Variance (ddof=0 for expressions)
outer(X, Y)Outer product of two vector expressions
Advanced usage
For axis-aware reductions, keepdims, or other options not available
through the standard interface, use the explicit functions:
cvxr_norm, cvxr_mean, cvxr_diff,
cvxr_std, cvxr_var, cvxr_outer.
See also
power, sum_entries,
max_entries, min_entries
cvxr_norm for the full-featured version with
axis and keepdims arguments
cvxr_std for the full-featured version
cvxr_var for the full-featured version
cvxr_outer for the CVXR-specific version