Skip to content

Commit

Permalink
fix printing of arrays of affine expressions (#123)
Browse files Browse the repository at this point in the history
* fix printing of arrays of affine expressions

* bumped patch version
  • Loading branch information
lucaferranti authored Jan 30, 2022
1 parent e1689d8 commit 224b9fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
50 changes: 29 additions & 21 deletions src/pils/affine_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
#########################
Expand Down
7 changes: 3 additions & 4 deletions test/test_pils/test_linexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 224b9fe

@lucaferranti
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53483

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 224b9fe6292d517048a195176a00b1fe17af066e
git push origin v0.1.5

Please sign in to comment.