Skip to content

Commit 8a3e843

Browse files
ericphansonararslan
authored andcommitted
Allow fix!ing complex variables to complex numbers (#308)
1 parent 3835ff8 commit 8a3e843

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/variable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ function fix!(x::Variable)
120120
end
121121
function fix!(x::Variable, v::AbstractArray)
122122
size(x) == size(v) || throw(DimensionMismatch("Variable and value sizes do not match!"))
123-
x.value = convert(Array{Float64}, v)
123+
x.value = sign(x) == ComplexSign() ? convert(Array{ComplexF64}, v) : convert(Array{Float64}, v)
124124
fix!(x)
125125
end
126126

127127
function fix!(x::Variable, v::Number)
128128
size(x) == (1,1) || throw(DimensionMismatch("Variable and value sizes do not match!"))
129-
x.value = Float64(v)
129+
x.value = sign(x) == ComplexSign() ? convert(ComplexF64, v) : convert(Float64, v)
130130
fix!(x)
131131
end
132132

test/test_const.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,23 @@
5959
@test evaluate( tr(p*x) ) 2.0 atol = TOL
6060
end
6161

62+
@testset "fix! with complex numbers" begin
63+
x = ComplexVariable()
64+
fix!(x, 1.0 + im*1.0)
65+
y = Variable()
66+
prob = minimize( real(x*y), [ y >= .5, real(x) >= .5, imag(x) >= 0])
67+
solve!(prob, solver)
68+
@test prob.optval .5 atol=TOL
69+
@test evaluate(real(x*y)) .5 atol=TOL
70+
@test evaluate(y) 0.5 atol=TOL
71+
72+
free!(x)
73+
fix!(y)
74+
solve!(prob, solver)
75+
@test prob.optval 0.25 atol=TOL
76+
@test evaluate(real(x*y)) 0.25 atol=TOL
77+
@test real(evaluate(x)) 0.5 atol=TOL
78+
@test evaluate(y) 0.5 atol=TOL
79+
end
6280

6381
end

0 commit comments

Comments
 (0)