Skip to content

Commit b527714

Browse files
authored
Merge pull request #359 from ericphanson/quadform
Fix #341, add news
2 parents d645486 + be7bbe7 commit b527714

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changes in v0.13.0
2+
3+
* The intermediate layer has changed from MathProgBase.jl to
4+
[MathOptInterface.jl](https://github.com/JuliaOpt/MathOptInterface.jl)
5+
([#330](https://github.com/JuliaOpt/Convex.jl/pull/330)).
6+
* `lambdamin` and `lambdamax` have been deprecated in favor of `eigmin` and
7+
`eigmax`. ([#357](https://github.com/JuliaOpt/Convex.jl/pull/357))
8+
* `evaluate(x::Variable)` and `evaluate(c::Constant)` now return scalars and
9+
vectors as appropriate, instead of `(1,1)`- and `(d,1)`-matrices.
10+
([#359](https://github.com/JuliaOpt/Convex.jl/pull/359)). This affects
11+
functions which used to return `(1,1)`-matrices; e.g., now
12+
`evaluate(quadform(...))` yields a scalar.

src/constant.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,20 @@ end
3838

3939
vexity(::Constant) = ConstVexity()
4040

41-
evaluate(x::Constant) = x.value
41+
# Lower (1,1)-matrices to scalars and (d,1)-matrices to vectors, for outputting to the user.
42+
function output(x::Value)
43+
if size(x, 2) == 1
44+
if size(x, 1) == 1
45+
return x[]
46+
else
47+
return vec(x)
48+
end
49+
else
50+
return x
51+
end
52+
end
53+
54+
evaluate(x::Constant) = output(x.value)
4255

4356
sign(x::Constant) = x.sign
4457

src/problem_depot/problems/sdp.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ end
383383
handle_problem!(p)
384384
if test
385385
@test p.optval 0 atol=atol rtol=rtol
386-
@test evaluate(objective) zeros(1, 1) atol=atol rtol=rtol
386+
@test evaluate(objective) 0.0 atol=atol rtol=rtol
387387

388388
real_diff = real.(x.value) - real.(a)
389389
imag_diff = imag.(x.value) - imag.(a)
@@ -393,7 +393,7 @@ end
393393
end
394394

395395
@add_problem sdp function sdp_socp_abs_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test}
396-
a = [5-4im]
396+
a = 5-4im
397397
x = ComplexVariable()
398398
objective = abs(a-x)
399399
c1 = real(x)>=0
@@ -402,12 +402,12 @@ end
402402
handle_problem!(p)
403403
if test
404404
@test p.optval 0 atol=atol rtol=rtol
405-
@test evaluate(objective) zeros(1) atol=atol rtol=rtol
405+
@test evaluate(objective) 0.0 atol=atol rtol=rtol
406406

407-
real_diff = real(x.value) .- real(a)
408-
imag_diff = imag(x.value) .- imag(a)
409-
@test real_diff zeros(1) atol=atol rtol=rtol
410-
@test imag_diff zeros(1) atol=atol rtol=rtol
407+
real_diff = real(x.value) - real(a)
408+
imag_diff = imag(x.value) - imag(a)
409+
@test real_diff 0.0 atol=atol rtol=rtol
410+
@test imag_diff 0.0 atol=atol rtol=rtol
411411
end
412412
end
413413

src/problem_depot/problems/socp.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ end
8181
end
8282

8383
@add_problem socp function socp_quad_over_lin_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test}
84-
x = Variable(3, 1)
84+
x = Variable(3)
8585
A = [2 -3 5; -2 9 -3; 5 -8 3]
8686
b = [-3; 9; 5]
87-
c = [3 2 4]
87+
c = [3, 2, 4]
8888
d = -3
89-
p = minimize(quadoverlin(A*x + b, c*x + d); numeric_type = T)
89+
p = minimize(quadoverlin(A*x + b, dot(c, x) + d); numeric_type = T)
9090

9191
if test
9292
@test vexity(p) == ConvexVexity()
9393
end
9494
handle_problem!(p)
9595
if test
9696
@test p.optval 17.7831 atol=atol rtol=rtol
97-
@test (evaluate(quadoverlin(A * x + b, c * x + d)))[1] 17.7831 atol=atol rtol=rtol
97+
@test evaluate(quadoverlin(A * x + b, dot(c, x) + d)) 17.7831 atol=atol rtol=rtol
9898
end
9999
end
100100

@@ -230,7 +230,7 @@ end
230230
handle_problem!(p)
231231
if test
232232
@test p.optval 6.1464 atol=atol rtol=rtol
233-
@test (evaluate(quadform(x, A)))[1] 6.1464 atol=atol rtol=rtol
233+
@test evaluate(quadform(x, A)) 6.1464 atol=atol rtol=rtol
234234
end
235235

236236
x = Variable(3, 1)
@@ -244,7 +244,7 @@ end
244244
handle_problem!(p)
245245
if test
246246
@test p.optval 3.7713 atol=atol rtol=rtol
247-
@test (evaluate(quadform(x, A)))[1] -1 atol=atol rtol=rtol
247+
@test evaluate(quadform(x, A)) -1 atol=atol rtol=rtol
248248
end
249249
end
250250

src/variable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function vexity(x::Variable)
5555
end
5656

5757
function evaluate(x::Variable)
58-
return x.value === nothing ? error("Value of the variable is yet to be calculated") : x.value
58+
return x.value === nothing ? error("Value of the variable is yet to be calculated") : output(x.value)
5959
end
6060

6161
function sign(x::Variable)

test/test_utilities.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ using Convex: AbstractExpr, ConicObj
196196
@test Convex.imag_conic_form(Constant([1.0, 2.0])) == [0.0, 0.0]
197197
end
198198

199+
@testset "#341: Evaluate for constants" begin
200+
A = rand(4,4)
201+
@test evaluate(Constant(A)) copy(A)
202+
@test Constant(A).size == (4,4)
203+
b = rand(4)
204+
@test evaluate(Constant(b)) copy(b)
205+
@test Constant(b).size == (4,1)
206+
c = 1.0
207+
@test evaluate(Constant(c)) c
208+
@test Constant(c).size == (1,1)
209+
end
210+
199211
@testset "Base.vect" begin
200212
# Issue #223: ensure we can make vectors of variables
201213
@test size([Variable(2), Variable(3,4)]) == (2,)

0 commit comments

Comments
 (0)