Skip to content

Commit 97324e6

Browse files
YingboMashashi
andcommitted
add print_tree
Co-authored-by: "Shashi Gowda" <[email protected]>
1 parent e8d56c3 commit 97324e6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,25 @@ function Base.hash(t::Union{Add,Mul}, u::UInt64)
770770
t.hash[] = h′
771771
return h′
772772
end
773+
774+
import AbstractTrees
775+
776+
struct TreePrint
777+
op
778+
x
779+
end
780+
AbstractTrees.children(x::Term) = arguments(x)
781+
AbstractTrees.children(x::Union{Add, Mul}) = map(y->TreePrint(x isa Add ? (:*) : (:^), y), collect(pairs(x.dict)))
782+
AbstractTrees.children(x::Union{Pow}) = [x.base, x.exp]
783+
AbstractTrees.children(x::TreePrint) = [x.x[1], x.x[2]]
784+
function print_tree(x::Union{Term, Add, Mul, Pow})
785+
AbstractTrees.print_tree(stdout, x, withinds=true) do io, y, inds
786+
if istree(y)
787+
print(io, operation(y))
788+
elseif y isa TreePrint
789+
print(io, "(", y.op, ")")
790+
else
791+
print(io, y)
792+
end
793+
end
794+
end

0 commit comments

Comments
 (0)