Skip to content

Commit e91c00b

Browse files
authored
Merge pull request #192 from JuliaSymbolics/sm/print
Fix printing, similarterm, and add tree printing
2 parents e8d56c3 + f6a1709 commit e91c00b

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.8.0"
4+
version = "0.8.1"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
8+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
89
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
910
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1011
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
@@ -17,6 +18,7 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
1718

1819
[compat]
1920
AbstractAlgebra = "0.9, 0.10, 0.11, 0.12"
21+
AbstractTrees = "0.3"
2022
Combinatorics = "1.0"
2123
DataStructures = "0.18"
2224
IfElse = "0.1"

src/types.jl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ function show_add(io, args)
370370
print_arg(io, -, t)
371371
else
372372
print(io, " - ")
373-
print_arg(io, +, -t)
373+
print_arg(IOContext(io, :paren=>true), +, -t)
374374
end
375375
end
376376
end
@@ -510,6 +510,11 @@ and the key (in Add) should instead be used to store the actual coefficient
510510
function makeadd(sign, coeff, xs...)
511511
d = sdict()
512512
for x in xs
513+
if x isa Add
514+
coeff += x.coeff
515+
_merge!(+, d, x.dict, filter=_iszero)
516+
continue
517+
end
513518
if x isa Number
514519
coeff += x
515520
continue
@@ -631,7 +636,7 @@ function makemul(coeff, xs...; d=sdict())
631636
coeff *= x
632637
elseif x isa Mul
633638
coeff *= x.coeff
634-
d = _merge(+, d, x.dict, filter=_iszero)
639+
_merge!(+, d, x.dict, filter=_iszero)
635640
else
636641
v = 1 + get(d, x, 0)
637642
if _iszero(v)
@@ -721,8 +726,9 @@ end
721726

722727
*(a::Pow, b::Mul) = b * a
723728

724-
function _merge(f, d, others...; filter=x->false)
725-
acc = copy(d)
729+
_merge(f, d, others...; filter=x->false) = _merge!(f, copy(d), others...; filter=filter)
730+
function _merge!(f, d, others...; filter=x->false)
731+
acc = d
726732
for other in others
727733
for (k, v) in other
728734
v = f(v)
@@ -770,3 +776,27 @@ function Base.hash(t::Union{Add,Mul}, u::UInt64)
770776
t.hash[] = h′
771777
return h′
772778
end
779+
780+
import AbstractTrees
781+
782+
struct TreePrint
783+
op
784+
x
785+
end
786+
AbstractTrees.children(x::Term) = arguments(x)
787+
AbstractTrees.children(x::Union{Add, Mul}) = map(y->TreePrint(x isa Add ? (:*) : (:^), y), collect(pairs(x.dict)))
788+
AbstractTrees.children(x::Union{Pow}) = [x.base, x.exp]
789+
AbstractTrees.children(x::TreePrint) = [x.x[1], x.x[2]]
790+
791+
print_tree(x; maxdepth=Inf, kw...) = print_tree(stdout, x; maxdepth=maxdepth, kw...)
792+
function print_tree(_io::IO, x::Union{Term, Add, Mul, Pow}; kw...)
793+
AbstractTrees.print_tree(_io, x; withinds=true, kw...) do io, y, inds
794+
if istree(y)
795+
print(io, operation(y))
796+
elseif y isa TreePrint
797+
print(io, "(", y.op, ")")
798+
else
799+
print(io, y)
800+
end
801+
end
802+
end

test/basics.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ end
111111
@test repr(Term(*, [1, 1])) == "*1"
112112
@test repr(Term(*, [2, 1])) == "2*1"
113113
@test repr((a + b) - (b + c)) == "a - c"
114+
@test repr(a + -1*(b + c)) == "a - (b + c)"
115+
@test repr(a + -1*b) == "a - b"
116+
end
117+
118+
@testset "similarterm with Add" begin
119+
@syms a b c
120+
@test isequal(SymbolicUtils.similarterm((b + c), +, [a, (b+c)]).dict, Dict(a=>1,b=>1,c=>1))
114121
end
115122

116123
toterm(t) = Term{symtype(t)}(operation(t), arguments(t))

0 commit comments

Comments
 (0)