Skip to content

Commit ce29cc5

Browse files
authored
added butcher_representation (#8)
1 parent 166afbb commit ce29cc5

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ this tree can be written as `[[τ²] τ]` or `(τ ∘ τ) ∘ (τ ∘ τ)`, wher
3131
`` is the non-asociative Butcher product of `RootedTree`s, which is also
3232
implemented.
3333

34+
To get the representation of a `RootedTree` introduced by Butcher, use `butcher_representation`:
35+
```julia
36+
julia> t = rootedtree([1, 2, 3, 4, 3, 3, 2, 2, 2, 2, 2])
37+
RootedTree{Int64}: [1, 2, 3, 4, 3, 3, 2, 2, 2, 2, 2]
38+
39+
julia> butcher_representation(t)
40+
"[[[τ]τ²]τ⁵]"
41+
```
42+
3443
### Iteration over `RootedTree`s
3544

3645
A `RootedTreeIterator(order::Integer)` can be used to iterate efficiently
@@ -41,8 +50,7 @@ over all `RootedTree`s of a given `order`.
4150
The usual functions on `RootedTree`s are implemented, cf.
4251
[Butcher (Numerical Methods for ODEs, 2016)](https://doi.org/10.1002/9781119121534).
4352
- `order(t::RootedTree)`: The order of a `RootedTree`, i.e. the length of it's level sequence.
44-
- `σ(t::RootedTree)`: The order of a `symmetry`, i.e. the order of the group of automorphisms
45-
on a particular labelling (of the vertices) of `t`.
53+
- `σ(t::RootedTree)`:The symmetry `σ` of a rooted tree, i.e. the order of the group of automorphisms on a particular labelling (of the vertices) of `t`.
4654
- `γ(t::RootedTree)`: The density `γ(t)` of a rooted tree, i.e. the product over all vertices of `t` of the order of the subtree rooted at that vertex.
4755
- `α(t::RootedTree)`: The number of monotonic labellings of `t` not equivalent under the symmetry group.
4856
- `β(t::RootedTree)`: The total number of labellings of `t` not equivalent under the symmetry group.

src/RootedTrees.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ module RootedTrees
33

44
using LinearAlgebra
55

6-
import Base: show, isless, ==, iterate, copy
6+
import Base: show, isless, ==, iterate, copy
77

88

99
export rootedtree, RootedTreeIterator
1010

11+
export butcher_representation
12+
1113
export α, β, γ, σ, order, residual_order_condition, elementary_weight, derivative_weight
1214

1315
export count_trees
@@ -432,6 +434,8 @@ function residual_order_condition(t::RootedTree, A, b, c)
432434
end
433435

434436

437+
# additional representation and construction methods
438+
435439
"""
436440
t1 ∘ t2
437441
@@ -450,4 +454,50 @@ function Base.:∘(t1::RootedTree, t2::RootedTree)
450454
end
451455

452456

457+
"""
458+
butcher_represetation(t::RootedTree)
459+
460+
Returns the representation of `t::RootedTree` as introduced by Butcher.
461+
462+
Reference: Section 301 of
463+
Butcher, John Charles.
464+
Numerical methods for ordinary differential equations.
465+
John Wiley & Sons, 2008.
466+
"""
467+
function butcher_representation(t::RootedTree)
468+
if order(t) == 1
469+
return "τ"
470+
end
471+
472+
subtr = Subtrees(t)
473+
result = ""
474+
for i in eachindex(subtr)
475+
result = result * butcher_representation(subtr[i])
476+
end
477+
result = "[" * result * "]"
478+
479+
# normalise the result by grouping repeated occurences of τ
480+
# TODO: Decide whether powers should also be used for subtrees,
481+
# e.g. "[[τ]²]" instead of "[[τ][τ]]"
482+
# for rootedtree([1, 2, 3, 2, 3]).
483+
# Currently, powers are only used for τ.
484+
for n in order(t):-1:2
485+
n_str = string(n)
486+
n_str = replace(n_str, "1" => "¹")
487+
n_str = replace(n_str, "2" => "²")
488+
n_str = replace(n_str, "3" => "³")
489+
n_str = replace(n_str, "4" => "")
490+
n_str = replace(n_str, "5" => "")
491+
n_str = replace(n_str, "6" => "")
492+
n_str = replace(n_str, "7" => "")
493+
n_str = replace(n_str, "8" => "")
494+
n_str = replace(n_str, "9" => "")
495+
n_str = replace(n_str, "0" => "")
496+
result = replace(result, "τ"^n => "τ"*n_str)
497+
end
498+
499+
return result
500+
end
501+
502+
453503
end # module

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ t1 = rootedtree([1])
3434
@test γ(t1) == 1
3535
@test α(t1) == 1
3636
@test β(t1) == α(t1)*γ(t1)
37+
@test butcher_representation(t1) == "τ"
3738

3839
@inferred order(t1)
3940
@inferred σ(t1)
@@ -49,6 +50,7 @@ t2 = rootedtree([1, 2])
4950
@test α(t2) == 1
5051
@test β(t2) == α(t2)*γ(t2)
5152
@test t2 == t1 t1
53+
@test butcher_representation(t2) == "[τ]"
5254

5355
t3 = rootedtree([1, 2, 2])
5456
@test order(t3) == 3
@@ -57,6 +59,7 @@ t3 = rootedtree([1, 2, 2])
5759
@test α(t3) == 1
5860
@test β(t3) == α(t3)*γ(t3)
5961
@test t3 == t2 t1
62+
@test butcher_representation(t3) == "[τ²]"
6063

6164
t4 = rootedtree([1, 2, 3])
6265
@test order(t4) == 3
@@ -65,6 +68,7 @@ t4 = rootedtree([1, 2, 3])
6568
@test α(t4) == 1
6669
@test β(t4) == α(t4)*γ(t4)
6770
@test t4 == t1 t2
71+
@test butcher_representation(t4) == "[[τ]]"
6872

6973
t5 = rootedtree([1, 2, 2, 2])
7074
@test order(t5) == 4
@@ -73,6 +77,7 @@ t5 = rootedtree([1, 2, 2, 2])
7377
@test α(t5) == 1
7478
@test β(t5) == α(t5)*γ(t5)
7579
@test t5 == t3 t1
80+
@test butcher_representation(t5) == "[τ³]"
7681

7782
t6 = rootedtree([1, 2, 2, 3])
7883
@inferred RootedTrees.subtrees(t6)
@@ -82,20 +87,23 @@ t6 = rootedtree([1, 2, 2, 3])
8287
@test α(t6) == 3
8388
@test β(t6) == α(t6)*γ(t6)
8489
@test t6 == t2 t2 == t4 t1
90+
@test butcher_representation(t6) == "[[τ]τ]"
8591

8692
t7 = rootedtree([1, 2, 3, 3])
8793
@test order(t7) == 4
8894
@test σ(t7) == 2
8995
@test γ(t7) == 12
9096
@test β(t7) == α(t7)*γ(t7)
9197
@test t7 == t1 t3
98+
@test butcher_representation(t7) == "[[τ²]]"
9299

93100
t8 = rootedtree([1, 2, 3, 4])
94101
@test order(t8) == 4
95102
@test σ(t8) == 1
96103
@test γ(t8) == 24
97104
@test α(t8) == 1
98105
@test t8 == t1 t4
106+
@test butcher_representation(t8) == "[[[τ]]]"
99107

100108
t9 = rootedtree([1, 2, 2, 2, 2])
101109
@test order(t9) == 5
@@ -104,6 +112,7 @@ t9 = rootedtree([1, 2, 2, 2, 2])
104112
@test α(t9) == 1
105113
@test β(t9) == α(t9)*γ(t9)
106114
@test t9 == t5 t1
115+
@test butcher_representation(t9) == "[τ⁴]"
107116

108117
t10 = rootedtree([1, 2, 2, 2, 3])
109118
@test order(t10) == 5
@@ -112,13 +121,15 @@ t10 = rootedtree([1, 2, 2, 2, 3])
112121
@test α(t10) == 6
113122
@test β(t10) == α(t10)*γ(t10)
114123
@test t10 == t3 t2 == t6 t1
124+
@test butcher_representation(t10) == "[[τ]τ²]"
115125

116126
t11 = rootedtree([1, 2, 2, 3, 3])
117127
@test order(t11) == 5
118128
@test σ(t11) == 2
119129
@test γ(t11) == 15
120130
@test α(t11) == 4
121131
@test t11 == t2 t3 == t7 t1
132+
@test butcher_representation(t11) == "[[τ²]τ]"
122133

123134
t12 = rootedtree([1, 2, 2, 3, 4])
124135
@test order(t12) == 5
@@ -127,6 +138,7 @@ t12 = rootedtree([1, 2, 2, 3, 4])
127138
@test α(t12) == 4
128139
@test β(t12) == α(t12)*γ(t12)
129140
@test t12 == t2 t4 == t8 t1
141+
@test butcher_representation(t12) == "[[[τ]]τ]"
130142

131143
t13 = rootedtree([1, 2, 3, 2, 3])
132144
@test order(t13) == 5
@@ -135,6 +147,7 @@ t13 = rootedtree([1, 2, 3, 2, 3])
135147
@test α(t13) == 3
136148
@test β(t13) == α(t13)*γ(t13)
137149
@test t13 == t4 t2
150+
@test butcher_representation(t13) == "[[τ][τ]]"
138151

139152
t14 = rootedtree([1, 2, 3, 3, 3])
140153
@test order(t14) == 5
@@ -143,6 +156,7 @@ t14 = rootedtree([1, 2, 3, 3, 3])
143156
@test α(t14) == 1
144157
@test β(t14) == α(t14)*γ(t14)
145158
@test t14 == t1 t5
159+
@test butcher_representation(t14) == "[[τ³]]"
146160

147161
t15 = rootedtree([1, 2, 3, 3, 4])
148162
@test order(t15) == 5
@@ -151,6 +165,7 @@ t15 = rootedtree([1, 2, 3, 3, 4])
151165
@test α(t15) == 3
152166
@test β(t15) == α(t15)*γ(t15)
153167
@test t15 == t1 t6
168+
@test butcher_representation(t15) == "[[[τ]τ]]"
154169

155170
t16 = rootedtree([1, 2, 3, 4, 4])
156171
@test order(t16) == 5
@@ -159,6 +174,7 @@ t16 = rootedtree([1, 2, 3, 4, 4])
159174
@test α(t16) == 1
160175
@test β(t16) == α(t16)*γ(t16)
161176
@test t16 == t1 t7
177+
@test butcher_representation(t16) == "[[[τ²]]]"
162178

163179
t17 = rootedtree([1, 2, 3, 4, 5])
164180
@test order(t17) == 5
@@ -167,6 +183,7 @@ t17 = rootedtree([1, 2, 3, 4, 5])
167183
@test α(t17) == 1
168184
@test β(t17) == α(t17)*γ(t17)
169185
@test t17 == t1 t8
186+
@test butcher_representation(t17) == "[[[[τ]]]]"
170187

171188

172189
# see butcher2008numerical, Table 302(I)

0 commit comments

Comments
 (0)