x <- Variable(3, name = "x")
prob1 <- Problem(Minimize(p_norm(x, 2)), list(x >= 1))
is_dcp(prob1)[1] TRUE
Anqi Fu, Balasubramanian Narasimhan, Steven Diamond, Stephen Boyd
The visualize() function generates interactive visualizations of CVXR optimization problems, showing the expression tree with DCP curvature annotations. It is useful for understanding how CVXR decomposes a problem and for diagnosing DCP violations.
The key arguments:
output: format for the visualization. "html" produces a self-contained interactive HTML page; "text" prints to the consolesolver: solver to use for generating standard form and solver data. Set to TRUE to auto-select a solver, or pass a solver name (e.g., "ECOS", "OSQP"). When specified, the HTML output includes two additional tabs: Standard Form and Solver Data. Omit or set to NULL to show only the expression tree and Smith formsfile: path to write the output (used with "html")open: whether to open the file in a browser (default TRUE in interactive sessions)doc_base: base URL for documentation links in the visualizationTwo helper features for richer output:
latex_name argument in Variable() and Parameter() sets the display name in LaTeX-rendered visualizationsset_label(constraint, label) attaches a human-readable label to a constraintThe visualization uses
The table below lists the
These atoms preserve curvature (both convex and concave). Their Smith form uses equality (
| Symbol | CVXR Function | Meaning |
|---|---|---|
+ |
Addition | |
-x |
Negation | |
%*% |
Matrix multiplication | |
* (elementwise) |
Hadamard (elementwise) product | |
/ |
Division by constant | |
t() |
Transpose | |
reshape_expr(), vec() |
Vectorization / reshape | |
sum(), sum_entries() |
Sum of entries |
These atoms have specific curvature. In the relaxed Smith form, convex atoms use
| Symbol | CVXR Function | Meaning | Curvature |
|---|---|---|---|
abs() |
Absolute value | convex | |
p_norm(x, 2) |
Euclidean norm | convex | |
p_norm(x, p) |
General |
convex ( |
|
power(x, 2), x^2 |
Square | convex | |
power(x, p), x^p |
General power | varies with |
|
quad_over_lin() |
convex | ||
quad_form(x, P) |
convex ( |
||
max_entries(), maximum() |
Maximum | convex | |
min_entries(), minimum() |
Minimum | concave |
| Symbol | Node Type | Description |
|---|---|---|
Variable |
Optimization variable | |
Parameter |
Named parameter (value set at solve time) | |
| literal value | Constant |
Numeric constant (shown as value or |
any other Atom |
Generic fallback for atoms without custom annotation |
Atoms that appear in the examples on this page but use the generic fallback include entr(), huber(), log_sum_exp(), lambda_max(), and pos().
The examples below show visualize() output for problems that satisfy the DCP rules. Each tree node is annotated with its curvature and sign using color-coded borders: blue for convex, red for concave, green for affine, and grey for constant. These colors indicate curvature, not validity — a red border on a concave node is expected, not an error. DCP violations are shown differently, with a red background and a banner (see the Non-DCP examples below).
A basic second-order cone program. The objective p_norm(x, 2) is convex and the constraint is affine, so the problem is DCP-compliant.
The classic unconstrained least squares problem. sum_squares() is a convex atom applied to an affine argument.
A Markowitz portfolio problem with a risk-aversion parameter latex_name for display customization and set_label() for named constraints. Note that parameter values must be set (via value()<-) before calling visualize() with solver = TRUE, so that the problem data matrices can be compiled for the Standard Form and Solver Data tabs.
w <- Variable(4, name = "w", latex_name = "\\mathbf{w}")
mu <- Parameter(4, name = "mu", latex_name = "\\boldsymbol{\\mu}")
gamma <- Parameter(1, name = "gamma", nonneg = TRUE, latex_name = "\\gamma")
Sigma <- matrix(c(1,.5,.3,.1, .5,1,.4,.2, .3,.4,1,.5, .1,.2,.5,1), 4, 4)
## Set parameter values so the solver tabs can compile problem data
value(mu) <- c(0.12, 0.10, 0.07, 0.03)
value(gamma) <- 0.5
prob3 <- Problem(
Maximize(t(mu) %*% w - gamma * quad_form(w, Sigma)),
list(
set_label(sum(w) == 1, "budget"),
set_label(w >= 0, "long only")
)
)
is_dcp(prob3)[1] TRUE
Maximum entropy distribution. The entr() atom is concave, so maximizing a sum of concave atoms is DCP-compliant.
The Huber function is convex; minimizing its sum is DCP-compliant. This is an unconstrained problem.
log_sum_exp() is a convex atom. The constraint is affine.
An SDP with a symmetric matrix variable. lambda_max() is convex and the equality constraint is affine.
A combination of max_entries(abs(x)) (convex) and sum_entries(pos(x)) (convex). The sum of convex functions is convex.
The following problems violate the DCP rules. The visualization highlights the offending nodes in red, making it easy to pinpoint where the violation occurs.
DCP violation: The product visualize() marks the objective node in red.
DCP violation: Equality constraints must be affine. Here
For quick inspection in the console, use output = "text" (the default):
visualize(prob1, output = "text")
## ── Expression Tree (MINIMIZE) ──────────────────────────────────────────────────
## t_1 = PnormApprox(...) [convex, \mathbb{R}_+, 1x1]
## \-- x [affine, 3x1]
## ── Constraints ─────────────────────────────────────────────────────────────────
## ✓ [1] Inequality 3x1
## 1 [constant, 1x1]
## x [affine, 3x1]
## ── SMITH FORM ──────────────────────────────────────────────────────────────────
## t_{1} = phi^{||.||_2}({x})
## ── RELAXED SMITH FORM ──────────────────────────────────────────────────────────
## t_{1} >= phi^{||.||_2}({x})
## ── CONIC FORM ──────────────────────────────────────────────────────────────────
## (t_{1}, {x}) in Q^{n+1}visualize() is designed for small to moderate problems — the kind you build during modeling and debugging. Problems with many variables, large data matrices, or hundreds of constraints will produce unwieldy expression trees and oversized conic forms that obscure rather than illuminate. Keep the problem small and representative when using this tool.
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] CVXR_1.8.1
loaded via a namespace (and not attached):
[1] slam_0.1-55 cli_3.6.5 knitr_1.51 ECOSolveR_0.6.1
[5] rlang_1.1.7 xfun_0.56 clarabel_0.11.2 otel_0.2.0
[9] gurobi_13.0-1 Rglpk_0.6-5.1 highs_1.12.0-3 cccp_0.3-3
[13] scs_3.2.7 S7_0.2.1 jsonlite_2.0.0 Rcplex_0.3-8
[17] backports_1.5.0 rprojroot_2.1.1 htmltools_0.5.9 Rmosek_11.1.1
[21] gmp_0.7-5.1 piqp_0.6.2 rmarkdown_2.30 grid_4.5.2
[25] evaluate_1.0.5 fastmap_1.2.0 yaml_2.3.12 compiler_4.5.2
[29] codetools_0.2-20 htmlwidgets_1.6.4 Rcpp_1.1.1 here_1.0.2
[33] osqp_1.0.0 lattice_0.22-9 digest_0.6.39 checkmate_2.3.4
[37] Matrix_1.7-4 tools_4.5.2