CVXR Functions

Author

Anqi Fu and Balasubramanian Narasimhan

Functions

Here we describe the functions that can be applied to CVXR expressions. CVXR uses the function information in this section and the Disciplined Convex Programming tools to mark expressions with a sign and curvature.

Operators

The infix operators +, -, *, %*%, / are treated as functions. + and - are affine functions. * and / are affine in CVXR because expr1*expr2 and expr1 %*% expr2 are allowed only when one of the expressions is constant and expr1/expr2 is allowed only when expr2 is a scalar constant.

Comparison operators

The comparison operators ==, <=, and >= are overloaded to create constraint objects:

x == y   # Equality constraint
x <= y   # Inequality constraint (convex <= concave)
x >= y   # Inequality constraint (concave >= convex)

Note that strict inequalities < and > are not supported, nor is !=.

Semidefinite operators

The operators %>>% and %<<% create positive semidefinite (PSD) and negative semidefinite (NSD) constraints, respectively:

X %>>% 0   # X is PSD
X %<<% 0   # X is NSD
Warning

Beware of operator precedence: A - B %>>% 0 is parsed as A - (B %>>% 0). Use (A - B) %>>% 0 instead.

Boolean operators

The operators &, |, and ! are overloaded for boolean variables to create logical constraints:

x & y    # equivalent to And(x, y)
x | y    # equivalent to Or(x, y)
!x       # equivalent to Not(x)

R math and summary functions

CVXR overloads many standard R functions so they can be applied directly to expressions:

  • Math: abs(), exp(), log(), sqrt(), cumsum(), cummax(), cumprod(), ceiling(), floor(), log2(), log10(), log1p()
  • Summary: sum(), max(), min(), prod(), mean()
  • Complex: Re(), Im(), Conj(), Mod()
  • Masked from base/stats: norm(), outer(), sd(), var()

These dispatch to the corresponding CVXR atoms (e.g., abs(x) calls Abs, sum(x) calls SumEntries).

Note

R Math group functions not listed above (e.g., sign(), round(), sin(), cos()) are not supported and will produce a clear error message if used on CVXR expressions.

Indexing and slicing

All non-scalar expressions can be indexed using the syntax expr[i, j]. Indexing is an affine function. The syntax expr[i] can be used as a shorthand for expr[i, 1] when expr is a column vector. Similarly, expr[i] is shorthand for expr[1, i] when expr is a row vector.

Non-scalar expressions can also be sliced into using the standard R slicing syntax. For example, expr[i:j, r] selects rows i through j of column r and returns a vector.

CVXR supports advanced indexing using lists of indices or boolean arrays. The semantics are the same as in R. Any time R might return a numeric vector, CVXR returns a column vector.

Transpose

The transpose of any expression can be obtained using the syntax t(expr). Transpose is an affine function.

Power

For any CVXR expression expr, the power operator expr^p is equivalent to the function power(expr, p).

Scalar functions

A scalar function takes one or more scalars, vectors, or matrices as arguments and returns a scalar.

Norms

Function Meaning Curvature Sign Domain
norm1(x) i|xi| convex 0
norm2(x) ixi2 convex 0
norm_inf(x) maxi|xi| convex 0
p_norm(x, p), p1 (i|xi|p)1/p convex 0
p_norm(x, p), 0<p<1 (i|xi|p)1/p concave 0 x0
cvxr_norm(X, "fro") ijXij2 convex 0
cvxr_norm(X, "nuc") iσi(X) convex 0
norm_nuc(X) iσi(X) convex 0
cvxr_norm(X, 2) σmax(X) convex 0
sigma_max(X) σmax(X) convex 0
mixed_norm(X, p, q) (k(j|Xjk|p)q/p)1/q convex 0

Aggregation and reduction

Function Meaning Curvature Sign Domain
sum_entries(X) ijXij affine unknown
sum_squares(x) ixi2 convex 0
sum_largest(x, k) sum of k largest xi convex unknown k{1,,n}
sum_smallest(x, k) sum of k smallest xi concave unknown k{1,,n}
max_entries(x) maxixi convex unknown
min_entries(x) minixi concave unknown
prod_entries(x) ixi log-log concave 0 x>0
cvxr_mean(x) 1nixi affine unknown
cvxr_var(x) 1ni(xix¯)2 convex 0
cvxr_std(x) 1ni(xix¯)2 convex 0
ptp(x) max(x)min(x) convex 0
cvar(x, β) conditional value-at-risk at level β convex unknown β(0,1]
dotsort(x, w) w[1]Tx[1]++w[n]Tx[n] (sorted) convex unknown w constant

Matrix and quadratic functions

Function Meaning Curvature Sign Domain
matrix_trace(X) tr(X) affine unknown
log_det(X) logdet(X) concave unknown XS++n
lambda_max(X) λmax(X) convex unknown XSn
lambda_min(X) λmin(X) concave unknown XSn
lambda_sum_largest(X, k) sum of k largest eigenvalues of X convex unknown XSn
lambda_sum_smallest(X, k) sum of k smallest eigenvalues of X concave unknown XSn
matrix_frac(x, P) xTP1x convex 0 P0
quad_form(x, P) xTPx convex (P0) 0 P0 or P0
quad_over_lin(x, y) x22/y convex 0 y>0
tr_inv(X) tr(X1) convex 0 X0
geo_mean(x) (ixi)1/n concave 0 x0
harmonic_mean(x) ni1/xi concave 0 x>0
log_sum_exp(x) log(iexi) convex unknown
scalar_product(x, c) cTx where c is constant affine unknown
vdot(x, y) xTy affine unknown
tv(x) / total_variation(x) i|xi+1xi| convex 0
inv_prod(x) 1/ixi convex 0 x>0
perspective(f, s) sf(x/s) convex varies s>0

Clarifications

The domain Sn refers to the set of symmetric matrices. The domains S+n and Sn refer to the set of positive semi-definite and negative semi-definite matrices, respectively. Similarly, S++n and Sn refer to the set of positive definite and negative definite matrices, respectively.

For a vector expression x, norm2(x) gives the Euclidean norm. For a matrix expression X, cvxr_norm(X, 2) and sigma_max(X) give the spectral norm (largest singular value).

The function cvxr_norm(X, "fro") is called the Frobenius norm and cvxr_norm(X, "nuc") (or norm_nuc(X)) the nuclear norm. The nuclear norm can also be defined as the sum of X’s singular values.

The functions max_entries and min_entries give the largest and smallest entry, respectively, in a single expression. These functions should not be confused with max_elemwise and min_elemwise (see Elementwise functions). Use max_elemwise and min_elemwise to find the maximum or minimum of a list of scalar expressions.

The function sum_entries sums all the entries in a single expression. The built-in R sum should be used to add together a list of expressions. For example, the following code sums three expressions:

expr_sum <- sum(expr1, expr2, expr3)

Functions along an axis

The functions sum_entries, cvxr_norm, max_entries, and min_entries can be applied along an axis. Given an m by n expression expr, the syntax func(expr, axis=1) applies func to each row, returning a m by 1 expression. The syntax func(expr, axis=2) applies func to each column, returning a 1 by n expression. For example, the following code sums along the columns and rows of a matrix variable:

X <- Variable(c(5, 4))
row_sums <- sum_entries(X, axis=1) # Has size (5, 1)
col_sums <- sum_entries(X, axis=2) # Has size (1, 4)

Note that the use of axis differs from its use in CVXPY where axis=0 implies the rows. In CVXR, we align our implementation with the base::apply function. The default in most cases is axis = NULL, which treats a matrix as one long vector, basically the same as apply with c(1,2). The exception is cumsum_axis (see below), which cannot take axis = NULL; it will throw an error.

Elementwise functions

These functions operate on each element of their arguments. For example, if X is a 5 by 4 matrix variable, then abs(X) is a 5 by 4 matrix expression. abs(X)[1, 2] is equivalent to abs(X[1, 2]).

Elementwise functions that take multiple arguments, such as max_elemwise and multiply, operate on the corresponding elements of each argument. For example, if X and Y are both 3 by 3 matrix variables, then max_elemwise(X, Y) is a 3 by 3 matrix expression. max_elemwise(X, Y)[2, 1] is equivalent to max_elemwise(X[2, 1], Y[2, 1]). This means all arguments must have the same dimensions or be scalars, which are promoted.

Function Meaning Curvature Sign Domain
abs(x) |x| convex 0
entr(x) xlogx concave unknown x>0
exp(x) ex convex 0
huber(x, M) 2(1+(x/M)21)M2 convex 0 M>0
inv_pos(x) 1/x convex 0 x>0
kl_div(x, y) xlog(x/y)x+y convex 0 x>0,y>0
log(x) logx concave unknown x>0
log1p_atom(x) log(1+x) concave unknown x>1
log_normcdf(x) logΦ(x) (Φ = standard normal CDF) concave 0
logistic(x) log(1+ex) convex 0
loggamma(x) logΓ(x) convex unknown x>0
max_elemwise(x, y, ...) max(x,y,) convex unknown
min_elemwise(x, y, ...) min(x,y,) concave unknown
multiply(c, x) cx (c constant) affine unknown
neg(x) max(x,0) convex 0
pos(x) max(x,0) convex 0
diff_pos(x, y) max(xy,0) convex 0
one_minus_pos(x) max(1x,0) convex 0
power(x, 0) 1 constant 0
power(x, 1) x affine unknown
power(x, p), p1 xp convex 0 x0
power(x, p), 0<p<1 xp concave 0 x0
power(x, p), p<0 xp convex 0 x>0
rel_entr(x, y) xlog(x/y) convex unknown x>0,y>0
scalene(x, α, β) αpos(x)+βneg(x) convex 0 α,β0
sqrt(x) x concave 0 x0
square(x) x2 convex 0
xexp(x) xex convex unknown

DQCP elementwise functions

The following functions are quasiconvex or quasiconcave. They can be used in DQCP problems (pass qcp = TRUE to psolve()).

Function Meaning Curvature Sign Domain
ceil_expr(x) x quasiconvex unknown
floor_expr(x) x quasiconcave unknown

Vector/matrix functions

A vector/matrix function takes one or more scalars, vectors, or matrices as arguments and returns a vector or matrix.

Function Meaning Curvature Sign Domain
bmat(list(list(X, Y), ...)) block matrix from list of lists affine unknown
conv(c, x) cx (discrete convolution) affine unknown c constant
cumsum_axis(x, axis) cumulative sum along axis affine unknown axis required
cummax_expr(x, axis) cumulative max along axis convex unknown axis required
cvxr_diff(x) kth order differences affine unknown
cvxr_outer(x, y) xyT (outer product) affine (one arg constant) unknown one arg must be constant
diag(x) / diag(X) diagonal vector diagonal matrix affine unknown
hstack(x, y, ...) horizontal stack affine unknown
kronecker(X, Y) XY (Kronecker product) affine unknown one arg must be constant
reshape_expr(x, c(m, n)) reshape to m×n affine unknown
upper_tri(X) upper triangular elements as vector affine unknown X square
vec(X) vectorize (column-major) affine unknown
vec_to_upper_tri(x) vector to upper triangular matrix affine unknown
vstack(x, y, ...) vertical stack affine unknown

DGP-specific functions

The following functions are for use in Disciplined Geometric Programming (DGP) problems. They are log-log convex or log-log concave.

Function Meaning Curvature Domain
pf_eigenvalue(X) Perron-Frobenius eigenvalue log-log convex X0
eye_minus_inv(X) (IX)1 log-log convex spectral radius <1
resolvent(X, s) (sIX)1 log-log convex spectral radius <s
gmatmul(A, x) generalized matrix multiply (DGP) log-log affine A constant, x>0

Boolean logic atoms

The following atoms are used for boolean constraints in mixed-integer problems. They operate on boolean variables.

Function Meaning Domain
And(x, y) xy boolean variables
Or(x, y) xy boolean variables
Not(x) ¬x boolean variables
Xor(x, y) xy boolean variables
implies(x, y) xy boolean variables
iff(x, y) xy boolean variables

Constraint constructors

These create constraint objects directly.

Function Meaning
SOC(t, x) x2t (second-order cone)
PSD(X) X0 (positive semidefinite)
ExpCone(x, y, z) yex/yz,y>0 (exponential cone)
PowCone3D(x, y, z, alpha) zxαy1α (power cone)
FiniteSet(x, S) xS (finite set membership)

Session Info

R version 4.5.2 (2025-10-31)
Platform: aarch64-apple-darwin20
Running under: macOS Tahoe 26.3

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.4.0

loaded via a namespace (and not attached):
 [1] vctrs_0.7.1        svglite_2.2.2      cli_3.6.5          knitr_1.51        
 [5] rlang_1.1.7        xfun_0.56          stringi_1.8.7      otel_0.2.0        
 [9] textshaping_1.0.4  jsonlite_2.0.0     glue_1.8.0         htmltools_0.5.9   
[13] scales_1.4.0       rmarkdown_2.30     evaluate_1.0.5     fastmap_1.2.0     
[17] yaml_2.3.12        lifecycle_1.0.5    stringr_1.6.0      compiler_4.5.2    
[21] RColorBrewer_1.1-3 htmlwidgets_1.6.4  rstudioapi_0.18.0  systemfonts_1.3.1 
[25] farver_2.1.2       digest_0.6.39      viridisLite_0.4.3  R6_2.6.1          
[29] dichromat_2.0-0.1  magrittr_2.0.4     tools_4.5.2        xml2_1.5.2