Skip to content

Commit 224b9fe

Browse files
authored
fix printing of arrays of affine expressions (#123)
* fix printing of arrays of affine expressions * bumped patch version
1 parent e1689d8 commit 224b9fe

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IntervalLinearAlgebra"
22
uuid = "92cbe1ac-9c24-436b-b0c9-5f7317aedcd5"
33
authors = ["Luca Ferranti"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"

src/pils/affine_expressions.jl

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,38 @@ end
8888

8989
(ae::AffineExpression)(p::Vector{<:Number}) = dot(ae.coeffs[1:end-1], p) + ae.coeffs[end]
9090

91-
## printing
92-
function _tostring(ae::AffineExpression)
93-
iszero(ae.coeffs) && return "0"
94-
str = ""
95-
@inbounds for (i, x) in enumerate(_vars_dict[:vars])
96-
c = ae.coeffs[i]
97-
iszero(c) && continue
98-
sgn = c > 0 ? "+" : "-"
99-
c = abs(c) == 1 ? "" : "$(abs(c))"
100-
str *= sgn * c * "$(x)"
101-
end
102-
103-
c = last(ae.coeffs)
104-
if !iszero(c)
105-
sgn = c > 0 ? "+" : ""
106-
str *= sgn * "$(c)"
91+
function show(io::IO, ae::AffineExpression)
92+
first_printed = false
93+
if iszero(ae.coeffs)
94+
print(io, 0)
95+
else
96+
@inbounds for (i, x) in enumerate(_vars_dict[:vars])
97+
c = ae.coeffs[i]
98+
iszero(c) && continue
99+
if c > 0
100+
if first_printed
101+
print(io, "+")
102+
end
103+
else
104+
print(io, "-")
105+
end
106+
if abs(c) != 1
107+
print(io, abs(c))
108+
end
109+
print(io, x)
110+
first_printed = true
111+
end
112+
113+
c = last(ae.coeffs)
114+
if !iszero(c)
115+
if c > 0 && first_printed
116+
print(io, "+")
117+
end
118+
print(io, c)
119+
end
107120
end
108-
str = (str[1] == '+' ? str[2:end] : str)
109-
return str
110121
end
111122

112-
show(io::IO, ae::AffineExpression) = print(io, _tostring(ae))
113-
114-
115123
#########################
116124
# BASIC FUNCTIONS #
117125
#########################

test/test_pils/test_linexpr.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ end
2525
@affinevars x y z
2626

2727
p1 = x - y + 3z
28-
@test IntervalLinearAlgebra._tostring(p1) == "x-y+3z"
28+
@test string(p1) == "x-y+3z"
2929
p2 = y + x - z - 2
30-
@test IntervalLinearAlgebra._tostring(p2) == "x+y-z-2"
31-
@test IntervalLinearAlgebra._tostring(p1 - p1) == "0"
32-
30+
@test string(p2) == "x+y-z-2"
31+
@test string(p1 - p1) == "0"
3332
@test +p1 == p1
3433
@test -p1 == -x + y - 3z
3534
@test p2([1, 1, 1]) == -1

0 commit comments

Comments
 (0)