diff --git a/Project.toml b/Project.toml index 82cb9560..5e76dfaf 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "IntervalLinearAlgebra" uuid = "92cbe1ac-9c24-436b-b0c9-5f7317aedcd5" authors = ["Luca Ferranti"] -version = "0.1.4" +version = "0.1.5" [deps] CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" diff --git a/src/pils/affine_expressions.jl b/src/pils/affine_expressions.jl index 19c72855..48c00ac4 100644 --- a/src/pils/affine_expressions.jl +++ b/src/pils/affine_expressions.jl @@ -88,30 +88,38 @@ end (ae::AffineExpression)(p::Vector{<:Number}) = dot(ae.coeffs[1:end-1], p) + ae.coeffs[end] -## printing -function _tostring(ae::AffineExpression) - iszero(ae.coeffs) && return "0" - str = "" - @inbounds for (i, x) in enumerate(_vars_dict[:vars]) - c = ae.coeffs[i] - iszero(c) && continue - sgn = c > 0 ? "+" : "-" - c = abs(c) == 1 ? "" : "$(abs(c))" - str *= sgn * c * "$(x)" - end - - c = last(ae.coeffs) - if !iszero(c) - sgn = c > 0 ? "+" : "" - str *= sgn * "$(c)" +function show(io::IO, ae::AffineExpression) + first_printed = false + if iszero(ae.coeffs) + print(io, 0) + else + @inbounds for (i, x) in enumerate(_vars_dict[:vars]) + c = ae.coeffs[i] + iszero(c) && continue + if c > 0 + if first_printed + print(io, "+") + end + else + print(io, "-") + end + if abs(c) != 1 + print(io, abs(c)) + end + print(io, x) + first_printed = true + end + + c = last(ae.coeffs) + if !iszero(c) + if c > 0 && first_printed + print(io, "+") + end + print(io, c) + end end - str = (str[1] == '+' ? str[2:end] : str) - return str end -show(io::IO, ae::AffineExpression) = print(io, _tostring(ae)) - - ######################### # BASIC FUNCTIONS # ######################### diff --git a/test/test_pils/test_linexpr.jl b/test/test_pils/test_linexpr.jl index b931af63..cd64bfce 100644 --- a/test/test_pils/test_linexpr.jl +++ b/test/test_pils/test_linexpr.jl @@ -25,11 +25,10 @@ end @affinevars x y z p1 = x - y + 3z - @test IntervalLinearAlgebra._tostring(p1) == "x-y+3z" + @test string(p1) == "x-y+3z" p2 = y + x - z - 2 - @test IntervalLinearAlgebra._tostring(p2) == "x+y-z-2" - @test IntervalLinearAlgebra._tostring(p1 - p1) == "0" - + @test string(p2) == "x+y-z-2" + @test string(p1 - p1) == "0" @test +p1 == p1 @test -p1 == -x + y - 3z @test p2([1, 1, 1]) == -1