-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbasics.md
203 lines (149 loc) · 5.29 KB
/
basics.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Basics, printing, and visualization
As described in the [introduction](@ref introduction), [`RootedTree`](@ref)s
are represented using level sequences, i.e., `AbstractVector`s containing
the distances of the nodes from the root. For example,
```@example basics
using RootedTrees
for t in RootedTreeIterator(4)
println(t)
end
```
## Visualization of trees
Depending on your background, you may be more familiar with the classical
notation used in the books of Butcher or Hairer & Wanner. You can get these
representation via [`butcher_representation`](@ref).
```@example basics
for t in RootedTreeIterator(4)
println(butcher_representation(t))
end
```
Remember that you can change the printing style globally via
[`RootedTrees.set_printing_style`](@ref).
When working with LaTeX, it can be convenient to use the LaTeX package
[forest](https://ctan.org/pkg/forest) to draw trees. You can find more
information about this in the docstring of [`RootedTrees.latexify`](@ref).
For example,
```@example basics
for t in RootedTreeIterator(4)
println(RootedTrees.latexify(t))
end
```
This results in the following LaTeX output:

To get a human-readable output, you can use
[`RootedTrees.set_latexify_style`](@ref). This can be particularly helpful when
working in Jupyter notebooks, e.g., by passing the output of `latexify` to
`IPython.display.Latex`.
```@example basics
RootedTrees.set_latexify_style("butcher")
for t in RootedTreeIterator(4)
println(RootedTrees.latexify(t))
end
RootedTrees.set_latexify_style("forest")
for t in RootedTreeIterator(4)
println(RootedTrees.latexify(t))
end
```
If you want to visualize individual trees, you can also use our plot recipes
for [Plots.jl](https://github.com/JuliaPlots/Plots.jl).
```@example basics
using Plots
t = rootedtree([1, 2, 3, 3, 2])
plot(t)
savefig("basics_tree.png"); nothing # hide
```

To get the elementary differential, corresponding to a `RootedTree`, as a [`LaTeXString`](https://github.com/JuliaStrings/LaTeXStrings.jl), you can use [`elementary_differential`](@ref).
```@example basics
for t in RootedTreeIterator(4)
println(elementary_differential(t))
end
```
In LaTeX this results in the following output:

## Number of trees
The number of rooted trees grows exponentially. Please consider this when
iterating over some set of rooted trees. The implementations in
[RootedTrees.jl](https://github.com/SciML/RootedTrees.jl)
are reasonably efficient, but an exponential growth will always win in the end.
The function [`count_trees`](@ref) iterates over rooted trees explicitly. Thus,
it provides a lower bound on the computational complexity of operations on all
trees. For example,
```@repl
using RootedTrees
@time count_trees(10)
@time count_trees(20)
```
A nice way to create and print tables of properties of trees is by using
the Julia package [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl).
```@repl
using RootedTrees, PrettyTables
orders = 1:10
pretty_table(hcat(orders, count_trees.(orders)), header=["Order", "# Trees"])
```
To get the corresponding number of Runge-Kutta (RK) order conditions, we must
sum up the number of trees, i.e.,
```@repl
using RootedTrees, PrettyTables
orders = 1:10
pretty_table(hcat(orders, cumsum(count_trees.(orders))), header=["Order", "# RK Order Conditions"])
```
We can also visualize the exponential growth.
```@example basics
using Plots
orders = 1:15
scatter(orders, count_trees.(orders), yscale=:log10,
xguide="Order", yguide="Number of Trees")
savefig("basics_count_trees.png"); nothing # hide
```

## Colored trees
A lot of the same functionality is also available for colored trees.
Note that the additional choice of different colors increases the number of
trees significantly. For example, the number of trees of order 3 increases from
```@example basics
for t in RootedTreeIterator(3)
println(t)
end
```
to
```@example basics
for t in BicoloredRootedTreeIterator(3)
println(t)
end
```
[`RootedTrees.latexify`](@ref) also supports bicolored rooted trees:
```@example basics
for t in BicoloredRootedTreeIterator(3)
println(RootedTrees.latexify(t))
end
```
The style can be adapted as well via [`RootedTrees.set_latexify_style`](@ref).
```@example basics
RootedTrees.set_latexify_style("butcher")
for t in BicoloredRootedTreeIterator(3)
println(RootedTrees.latexify(t))
end
RootedTrees.set_latexify_style("forest")
for t in BicoloredRootedTreeIterator(3)
println(RootedTrees.latexify(t))
end
```
Plotting is of course also implemented for colored rooted trees.
```@example basics
using Plots
t = rootedtree([1, 2, 3, 3, 2, 2], Bool[0, 0, 1, 0, 1, 0])
plot(t)
savefig("basics_bicolored_tree.png"); nothing # hide
```

The general implementation supports more than two colors, e.g.,
```@example basics
using Plots
t = rootedtree([1, 2, 3, 3, 2], [1, 2, 3, 4, 5])
plot(t)
savefig("basics_colored_tree.png"); nothing # hide
```

However, the support for multiple colors is limited at the moment, e.g.,
concerning efficient iterators.