All of the examples can be found in Jupyter notebook form here.

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_solver = true)
Summation example
  0.014649 seconds (4.61 k allocations: 11.759 MiB, 70.49% compilation time)
  7.238103 seconds (11.19 M allocations: 685.415 MiB, 2.46% gc time, 0.18% compilation time)

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_solver = true)
Indexing example
  0.113476 seconds (92.33 k allocations: 16.337 MiB, 56.06% compilation time)
  0.079735 seconds (193.09 k allocations: 65.442 MiB, 14.12% compilation time)

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(vec(X)), A * X == b)
end
@time solve!(p, ECOS.Optimizer; silent_solver = true)
Matrix constraint example
  0.026238 seconds (48.05 k allocations: 2.489 MiB, 99.72% compilation time)
  1.339347 seconds (381.38 k allocations: 196.511 MiB, 3.76% gc time, 0.58% compilation time)

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_solver = true)

n = 3
A = randn(n, n);
#@time begin
X = Variable(n, n);
p = minimize(norm(vec(X' - A)), X[1, 1] == 1);
solve!(p, ECOS.Optimizer; silent_solver = true)
#end
Transpose example
  0.038447 seconds (57.00 k allocations: 2.957 MiB, 99.61% compilation time)
  0.025229 seconds (23.54 k allocations: 1.216 MiB, 81.33% compilation time)

This page was generated using Literate.jl.