Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for Base.complex #3734

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,40 @@ function Base.:*(
)
return x * A
end

function Base.complex(
r::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
i::Real,
)
return r + im * i
end

function Base.complex(
r::Real,
i::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
)
return r + im * i
end

function Base.complex(
r::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
i::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
)
return r + im * i
end
16 changes: 16 additions & 0 deletions test/test_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,20 @@ function test_matrix_abstractscalar_add()
return
end

function test_base_complex()
model = Model()
@variable(model, x)
for f in (x, 1.0 * x, x^2)
@test isequal_canonical(complex(3, f), 3 + im * f)
@test isequal_canonical(complex(f, 2.0), f + 2.0im)
end
for f in (x, 1.0 * x, x^2), g in (x, 1.0 * x, x^2)
@test isequal_canonical(complex(f, g), f + im * g)
end
@test_throws MethodError complex(x, 2im)
@test_throws MethodError complex(2im, x)
@test_throws MethodError complex(2.0 + x, im * x)
return
end

end
Loading