Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions src/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ function _shorten(options::_PrintOptions, x::Float64)
return string(x)
end

_shorten(::_PrintOptions, x) = string("(", x, ")")

"""
_to_string(options::_PrintOptions, c::Float64, x::String)
_to_string(options::_PrintOptions, c::Real, x::String)

Write a coefficient-name pair to string. There are a few cases to handle.

Expand All @@ -126,12 +128,7 @@ Write a coefficient-name pair to string. There are a few cases to handle.
+1.0x | "x" | " + x"
-1.0x | "-x" | " - x"
"""
function _to_string(
options::_PrintOptions,
c::Float64,
x::String;
is_first::Bool,
)
function _to_string(options::_PrintOptions, c::Real, x::String; is_first::Bool)
prefix = if is_first
c < 0 ? "-" : ""
else
Expand All @@ -145,6 +142,15 @@ function _to_string(
end
end

function _to_string(
options::_PrintOptions,
c::Number,
x::String;
is_first::Bool,
)
return string(is_first ? "" : " + ", _shorten(options, c), " ", x)
end

function _to_string(
options::_PrintOptions,
model::MOI.ModelLike,
Expand Down
35 changes: 35 additions & 0 deletions test/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ function test_numbers()
@test MOIU._to_string(options, -1.2, "x"; is_first = false) == " - 1.2 x"
end

function test_complex_numbers()
x = 1.0 + 1.0im
options =
MOIU._PrintOptions(MIME("text/plain"); simplify_coefficients = true)
@test MOIU._to_string(options, x, "x"; is_first = true) == "(1.0 + 1.0im) x"
@test MOIU._to_string(options, x, "x"; is_first = false) ==
" + (1.0 + 1.0im) x"
@test MOIU._to_string(options, -x, "x"; is_first = true) ==
"(-1.0 - 1.0im) x"
@test MOIU._to_string(options, -x, "x"; is_first = false) ==
" + (-1.0 - 1.0im) x"
return
end

function test_rational_numbers()
x = 1 // 2
options =
MOIU._PrintOptions(MIME("text/plain"); simplify_coefficients = true)
@test MOIU._to_string(options, x, "x"; is_first = true) == "(1//2) x"
@test MOIU._to_string(options, x, "x"; is_first = false) == " + (1//2) x"
@test MOIU._to_string(options, -x, "x"; is_first = true) == "-(1//2) x"
@test MOIU._to_string(options, -x, "x"; is_first = false) == " - (1//2) x"
return
end

function test_variable()
model = MOIU.Model{Float64}()
x = MOI.add_variable(model)
Expand Down Expand Up @@ -99,6 +124,16 @@ function test_ScalarAffineFunction()
@test MOIU._to_string(LATEX, model, f) == "1.4 - 1.2 x + 1.3 x"
end

function test_ScalarAffineFunction_complex()
model = MOI.Utilities.UniversalFallback(MOIU.Model{Float64}())
x = MOI.add_variable(model)
MOI.set(model, MOI.VariableName(), x, "x")
f = (1.0 + 1.0im)x
s = MOI.EqualTo(1.0 + 1.0im)
@test MOIU._to_string(PLAIN, model, f) == "(0.0 + 0.0im) + (1.0 + 1.0im) x"
@test MOIU._to_string(LATEX, model, f) == "(0.0 + 0.0im) + (1.0 + 1.0im) x"
end

function test_ScalarQuadraticTerm()
model = MOIU.Model{Float64}()
x = MOI.add_variable(model)
Expand Down