Skip to content

Commit aa697a0

Browse files
authored
Fix addition/substraction between variable and complex number (#2890)
1 parent 11d3527 commit aa697a0

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/operators.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ _float(J::UniformScaling) = _float(J.λ)
3030
# _Constant--_Constant obviously already taken care of!
3131
# _Constant--VariableRef
3232
function Base.:+(lhs::_Constant, rhs::AbstractVariableRef)
33-
return _build_aff_expr(_float(lhs), 1.0, rhs)
33+
constant = _float(lhs)
34+
return _build_aff_expr(constant, one(constant), rhs)
3435
end
3536
function Base.:-(lhs::_Constant, rhs::AbstractVariableRef)
36-
return _build_aff_expr(_float(lhs), -1.0, rhs)
37+
constant = _float(lhs)
38+
return _build_aff_expr(constant, -one(constant), rhs)
3739
end
3840
function Base.:*(lhs::_Constant, rhs::AbstractVariableRef)
3941
if iszero(lhs)

test/complex.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ function test_complex_aff_expr()
2222
return
2323
end
2424

25+
function test_complex_plus_variable()
26+
model = Model()
27+
@variable(model, x)
28+
y = x + im
29+
@test typeof(y) == GenericAffExpr{Complex{Float64},VariableRef}
30+
@test y == im + x
31+
return
32+
end
33+
34+
function test_complex_minus_variable()
35+
model = Model()
36+
@variable(model, x)
37+
y = im - x
38+
@test typeof(y) == GenericAffExpr{Complex{Float64},VariableRef}
39+
@test -y == x - im
40+
return
41+
end
42+
2543
function test_complex_aff_expr_convert()
2644
model = Model()
2745
@variable(model, x)

0 commit comments

Comments
 (0)