Skip to content

Commit ac92d4a

Browse files
authored
Test conic forms for constant values (#617)
1 parent ac382bb commit ac92d4a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/atoms/HuberAtom.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ curvature(::HuberAtom) = ConvexVexity()
2828

2929
function evaluate(x::HuberAtom)
3030
c = evaluate(x.children[1])
31+
if c isa Number
32+
c = [c]
33+
end
3134
for i in 1:length(c)
3235
if c[i] <= x.M
3336
c[i] = c[i]^2

src/real_operate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ end
315315
# Here we have our two complex -> real functions
316316
# These are allowed these inputs:
317317
const ComplexToRealInputs{T} =
318-
Union{ComplexTape{T},SparseTape{T},ComplexStructOfVec{T}}
318+
Union{ComplexTape{T},SparseTape{T},ComplexStructOfVec{T},Vector{T}}
319319

320320
# `real`
321321
function real_operate(

test/test_atoms.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,46 @@ function _test_atom(build_fn, target_string::String; value_type = Float64)
7474
@test Convex.monotonicity(atom) isa NTuple{N,Convex.Monotonicity}
7575
@test Convex.curvature(atom) isa Convex.Vexity
7676
_test_reformulation(build_fn, target_string; value_type)
77+
_test_constant_atom(build_fn; value_type)
78+
return
79+
end
80+
81+
function _to_constant(expr::Convex.AbstractVariable)
82+
if Convex.iscomplex(expr)
83+
val = rand(Float64, size(expr)) + im * rand(Float64, size(expr))
84+
else
85+
val = rand(Float64, size(expr))
86+
end
87+
if size(expr, 1) == size(expr, 2)
88+
val = val' * val
89+
end
90+
return constant(val)
91+
end
92+
93+
_to_constant(x::Convex.Value) = x
94+
_to_constant(x::Union{Convex.Constant,Convex.ComplexConstant}) = x
95+
96+
function _to_constant(e::Convex.AbstractExpr)
97+
e.children = map(_to_constant, e.children)
98+
return e
99+
end
100+
101+
function _test_constant_atom(build_fn; value_type)
102+
context = Convex.Context{value_type}(MOI.Utilities.Model{value_type})
103+
atom = _to_constant(build_fn(context))
104+
if Convex.iscomplex(atom) || any(Convex.iscomplex, atom.children)
105+
return
106+
end
107+
form = Convex.conic_form!(context, atom)
108+
answer = evaluate(atom)
109+
if !(form isa AbstractVector)
110+
return # The reformulation is still in terms of MOI variables.
111+
end
112+
if answer isa Number
113+
@test only(form) answer
114+
else
115+
@test form vec(answer)
116+
end
77117
return
78118
end
79119

0 commit comments

Comments
 (0)