# Paper examples

using Convex, ECOS

Summation.

println("Summation example")
x = Variable();
e = 0;
@time begin
for i in 1:1000
global e
e += x
end
p = minimize(e, x >= 1)
end
@time solve!(p, ECOS.Optimizer; silent = true)
Problem statistics
problem is DCP         : true
number of variables    : 1 (1 scalar elements)
number of constraints  : 1 (1 scalar elements)
number of coefficients : 2
number of atoms        : 2

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 1000.0

Expression graph
minimize
└─ + (affine; real)
├─ [0;;]
├─ real variable (id: 205…761)
├─ real variable (id: 205…761)
⋮
subject to
└─ ≥ constraint (affine)
└─ + (affine; real)
├─ real variable (id: 205…761)
└─ [-1;;]


Indexing.

println("Indexing example")
x = Variable(1000, 1);
e = 0;
@time begin
for i in 1:1000
global e
e += x[i]
end
p = minimize(e, x >= ones(1000, 1))
end
@time solve!(p, ECOS.Optimizer; silent = true)
Problem statistics
problem is DCP         : true
number of variables    : 1 (1_000 scalar elements)
number of constraints  : 1 (1_000 scalar elements)
number of coefficients : 1_001
number of atoms        : 1_002

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 1000.0

Expression graph
minimize
└─ + (affine; real)
├─ [0;;]
├─ index (affine; real)
│  └─ 1000-element real variable (id: 103…651)
├─ index (affine; real)
│  └─ 1000-element real variable (id: 103…651)
⋮
subject to
└─ ≥ constraint (affine)
└─ + (affine; real)
├─ 1000-element real variable (id: 103…651)
└─ 1000×1 Matrix{Float64}


Matrix constraints.

println("Matrix constraint example")
n, m, p = 100, 100, 100
X = Variable(m, n);
A = randn(p, m);
b = randn(p, n);
@time begin
p = minimize(norm(X), A * X == b)
end
@time solve!(p, ECOS.Optimizer; silent = true)
Problem statistics
problem is DCP         : true
number of variables    : 1 (10_000 scalar elements)
number of constraints  : 1 (10_000 scalar elements)
number of coefficients : 20_000
number of atoms        : 4

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 610.6659

Expression graph
minimize
└─ norm2 (convex; positive)
└─ reshape (affine; real)
└─ 100×100 real variable (id: 386…231)
subject to
└─ == constraint (affine)
└─ + (affine; real)
├─ * (affine; real)
│  ├─ …
│  └─ …
└─ 100×100 Matrix{Float64}


Transpose.

println("Transpose example")
X = Variable(5, 5);
A = randn(5, 5);
@time begin
p = minimize(norm2(X - A), X' == X)
end
@time solve!(p, ECOS.Optimizer; silent = true)

n = 3
A = randn(n, n);
#@time begin
X = Variable(n, n);
p = minimize(norm(X' - A), X[1, 1] == 1);
solve!(p, ECOS.Optimizer; silent = true)
#end
Problem statistics
problem is DCP         : true
number of variables    : 1 (9 scalar elements)
number of constraints  : 1 (1 scalar elements)
number of coefficients : 19
number of atoms        : 8

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 1.0682

Expression graph
minimize
└─ norm2 (convex; positive)
└─ reshape (affine; real)
└─ + (affine; real)
├─ …
└─ …
subject to
└─ == constraint (affine)
└─ + (affine; real)
├─ index (affine; real)
│  └─ …
└─ [-1;;]