Skip to content

Commit 08275a9

Browse files
pw0lfranocha
andauthored
Added feature that generates the elementary weight of a rooted tree as a latexstring (#155)
* Working version of elm_weights * Final changes for pr * Delete elm_weights.ipynb * Fixed typo * Added changes by JuliaFormatter * Added test for codecoverage * Renamed elementary_differential to elementary_differential_latexstring and deprecated the old one * Changed version number * Forgot to export the new funciton * Added changes of the Formatter * Update Project.toml Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com> * Update src/RootedTrees.jl Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com> * Added picture for docu * Deleted redundant part of code --------- Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
1 parent e9698cf commit 08275a9

File tree

4 files changed

+156
-23
lines changed

4 files changed

+156
-23
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RootedTrees"
22
uuid = "47965b36-3f3e-11e9-0dcf-4570dfd42a8c"
33
authors = ["Hendrik Ranocha <mail@ranocha.de> and contributors"]
4-
version = "2.20.0"
4+
version = "2.21.0"
55

66
[deps]
77
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"

docs/src/tutorials/basics.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,27 @@ savefig("basics_tree.png"); nothing # hide
7373

7474
![](basics_tree.png)
7575

76-
To get the elementary differential, corresponding to a `RootedTree`, as a [`LaTeXString`](https://github.com/JuliaStrings/LaTeXStrings.jl), you can use [`elementary_differential`](@ref).
76+
To get the elementary differential, corresponding to a `RootedTree`, as a [`LaTeXString`](https://github.com/JuliaStrings/LaTeXStrings.jl), you can use [`elementary_differential_latexstring`](@ref).
7777

7878
```@example basics
7979
for t in RootedTreeIterator(4)
80-
println(elementary_differential(t))
80+
println(elementary_differential_latexstring(t))
8181
end
8282
```
8383
In LaTeX this results in the following output:
8484

8585
![latex-elementary_differentials](https://user-images.githubusercontent.com/125130707/282897199-4967fe07-a370-4d64-b671-84f578a52391.png)
8686

87+
Similarly, to get the elementary weight, corresponding to a `RootedTree`, as a [`LaTeXString`](https://github.com/JuliaStrings/LaTeXStrings.jl), you can use [`elementary_weight_latexstring`](@ref).
88+
89+
```@example basics
90+
for t in RootedTreeIterator(4)
91+
println(elementary_weight_latexstring(t))
92+
end
93+
```
94+
95+
![latex elemenary weights](https://private-user-images.githubusercontent.com/125130707/298310491-8a035faf-fd1a-4fc0-92be-c3387eb53177.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MDYxNjY1ODAsIm5iZiI6MTcwNjE2NjI4MCwicGF0aCI6Ii8xMjUxMzA3MDcvMjk4MzEwNDkxLThhMDM1ZmFmLWZkMWEtNGZjMC05MmJlLWMzMzg3ZWI1MzE3Ny5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwMTI1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDEyNVQwNzA0NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1kYzQyYjM0MWY0MDU5YWZlYzBmODA4MjFiZGIxN2E3YjhkYTdmZDNkYTU5NmI5OTEwNWFiZjg0OGZjNDg1MzZhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.GB-PigOlQqenzgruzWg19qslzM6RXeX4xWwCNreOvNY)
96+
8797

8898
## Number of trees
8999

src/RootedTrees.jl

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ end
1717
export RootedTree, rootedtree, rootedtree!, RootedTreeIterator,
1818
ColoredRootedTree, BicoloredRootedTree, BicoloredRootedTreeIterator
1919

20-
export butcher_representation, elementary_differential
20+
export butcher_representation, elementary_differential_latexstring,
21+
elementary_weight_latexstring
2122

2223
export α, β, γ, density, σ, symmetry, order, root_color
2324

@@ -1417,14 +1418,16 @@ function butcher_representation(t::RootedTree, normalize::Bool = true)
14171418
return result
14181419
end
14191420

1421+
@deprecate elementary_differential(t::RootedTree) elementary_differential_latexstring(t)
1422+
14201423
"""
1421-
elementary_differential(t::RootedTree)
1424+
elementary_differential_latexstring(t::RootedTree)
14221425
14231426
Returns the elementary differential as a `LaTeXString`
14241427
from the package [LaTeXStrings.jl](https://github.com/JuliaStrings/LaTeXStrings.jl).
14251428
14261429
"""
1427-
function elementary_differential(t::RootedTree)
1430+
function elementary_differential_latexstring(t::RootedTree)
14281431
return latexstring(rec_elementary_differential(t))
14291432
end
14301433

@@ -1453,6 +1456,85 @@ function rec_elementary_differential(t::RootedTree)
14531456
return el_diff
14541457
end
14551458

1459+
"""
1460+
elementary_weight_latexstring(t::RootedTree)
1461+
1462+
Returns the elementary_weight as a `LaTeXString`
1463+
from the package [LaTeXStrings.jl](https://github.com/JuliaStrings/LaTeXStrings.jl).
1464+
"""
1465+
function elementary_weight_latexstring(t::RootedTree)
1466+
# Creating the alphabet for the indices
1467+
a = collect('d':'z') # all letters except of a,b and c
1468+
p = order(t)
1469+
n = ceil(Int, p / 23)
1470+
alphabet = String[]
1471+
# if the tree could need more than the 23 available letters, the letter get a number as a suffix
1472+
if n == 1
1473+
alphabet = [string(letter) for letter in a]
1474+
else
1475+
for i in 1:n
1476+
alphabet = vcat(alphabet, [letter * string(i) for letter in a])
1477+
end
1478+
end
1479+
1480+
substrings = String[]
1481+
first_index = splice!(alphabet, 1)
1482+
indices = [first_index]
1483+
1484+
substring, idx, _ = elm_weights_rec!(t, first_index, alphabet) # Call the recursive function
1485+
indices = vcat(indices, idx)
1486+
push!(substrings, substring)
1487+
1488+
pushfirst!(substrings, "\\sum_{$(join(indices,", "))}b_{$(first_index)}")
1489+
return latexstring(join(substrings))
1490+
end
1491+
1492+
# Function used to go recursively through the RootedTree
1493+
# to generate the elementary weight of the tree.
1494+
function elm_weights_rec!(t::RootedTree, last_index, a)
1495+
alphabet = copy(a)
1496+
# leaf-node
1497+
if length(t.level_sequence) == 1 && t.level_sequence[1] != 1
1498+
return "c_{$(last_index)}", [], alphabet
1499+
else
1500+
substrings = String[]
1501+
indices = String[]
1502+
if t.level_sequence[1] != 1 # Specialcase: Complete Tree
1503+
push!(substrings, "a_{$(last_index),$(alphabet[1])}")
1504+
index = splice!(alphabet, 1)
1505+
push!(indices, index)
1506+
else
1507+
index = last_index
1508+
end
1509+
# counting identical trees
1510+
prev_tree = 0
1511+
tree_count = 1
1512+
for subtree in SubtreeIterator(t)
1513+
if subtree == prev_tree
1514+
tree_count += 1
1515+
continue
1516+
# When reaching the last one of identical trees, the substring gets the number of trees as an exponent
1517+
elseif tree_count > 1
1518+
substrings[end] = "($(substrings[end]))^{$tree_count}"
1519+
end
1520+
prev_tree = copy(subtree)
1521+
tree_count = 1
1522+
substring, idx, alphabet = elm_weights_rec!(subtree, index, alphabet)
1523+
indices = vcat(indices, idx)
1524+
push!(substrings, substring)
1525+
end
1526+
# Specialcase: The identical trees were the last ones
1527+
if tree_count > 1
1528+
if length(prev_tree.level_sequence) == 1
1529+
substrings[end] = "$(substrings[end])^{$tree_count}"
1530+
else
1531+
substrings[end] = "($(substrings[end]))^{$tree_count}"
1532+
end
1533+
end
1534+
return join(substrings), indices, alphabet
1535+
end
1536+
end
1537+
14561538
include("colored_trees.jl")
14571539
include("latexify.jl")
14581540
include("plot_recipes.jl")

test/runtests.jl

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ using Aqua: Aqua
303303
@test isempty(RootedTrees.subtrees(t1))
304304
@test butcher_representation(empty(t1)) == ""
305305
@test RootedTrees.latexify(empty(t1)) == "\\varnothing"
306-
@test elementary_differential(t1) == L"$f$"
306+
@test elementary_differential_latexstring(t1) == L"$f$"
307+
@test elementary_weight_latexstring(t1) == L"$\sum_{d}b_{d}$"
307308

308309
@inferred order(t1)
309310
@inferred σ(t1)
@@ -324,7 +325,8 @@ using Aqua: Aqua
324325
@test RootedTrees.latexify(t2) == latex_string
325326
@test latexify(t2) == latex_string
326327
@test RootedTrees.subtrees(t2) == [rootedtree([2])]
327-
@test elementary_differential(t2) == L"$f^{\prime}f$"
328+
@test elementary_differential_latexstring(t2) == L"$f^{\prime}f$"
329+
@test elementary_weight_latexstring(t2) == L"$\sum_{d}b_{d}c_{d}$"
328330

329331
t3 = rootedtree([1, 2, 2])
330332
@test order(t3) == 3
@@ -338,7 +340,8 @@ using Aqua: Aqua
338340
@test RootedTrees.latexify(t3) == latex_string
339341
@test latexify(t3) == latex_string
340342
@test RootedTrees.subtrees(t3) == [rootedtree([2]), rootedtree([2])]
341-
@test elementary_differential(t3) == L"$f^{\prime\prime}(f, f)$"
343+
@test elementary_differential_latexstring(t3) == L"$f^{\prime\prime}(f, f)$"
344+
@test elementary_weight_latexstring(t3) == L"$\sum_{d}b_{d}c_{d}^{2}$"
342345

343346
t4 = rootedtree([1, 2, 3])
344347
@test order(t4) == 3
@@ -352,7 +355,8 @@ using Aqua: Aqua
352355
@test RootedTrees.latexify(t4) == latex_string
353356
@test latexify(t4) == latex_string
354357
@test RootedTrees.subtrees(t4) == [rootedtree([2, 3])]
355-
@test elementary_differential(t4) == L"$f^{\prime}f^{\prime}f$"
358+
@test elementary_differential_latexstring(t4) == L"$f^{\prime}f^{\prime}f$"
359+
@test elementary_weight_latexstring(t4) == L"$\sum_{d, e}b_{d}a_{d,e}c_{e}$"
356360

357361
t5 = rootedtree([1, 2, 2, 2])
358362
@test order(t5) == 4
@@ -364,7 +368,9 @@ using Aqua: Aqua
364368
@test butcher_representation(t5) == "[τ³]"
365369
@test RootedTrees.subtrees(t5) ==
366370
[rootedtree([2]), rootedtree([2]), rootedtree([2])]
367-
@test elementary_differential(t5) == L"$f^{\prime\prime\prime}(f, f, f)$"
371+
@test elementary_differential_latexstring(t5) ==
372+
L"$f^{\prime\prime\prime}(f, f, f)$"
373+
@test elementary_weight_latexstring(t5) == L"$\sum_{d}b_{d}c_{d}^{3}$"
368374

369375
t6 = rootedtree([1, 2, 2, 3])
370376
@inferred RootedTrees.subtrees(t6)
@@ -376,7 +382,10 @@ using Aqua: Aqua
376382
@test t6 == t2 t2 == t4 t1
377383
@test butcher_representation(t6) == "[[τ]τ]"
378384
@test RootedTrees.subtrees(t6) == [rootedtree([2, 3]), rootedtree([2])]
379-
@test elementary_differential(t6) == L"$f^{\prime\prime}(f^{\prime}f, f)$"
385+
@test elementary_differential_latexstring(t6) ==
386+
L"$f^{\prime\prime}(f^{\prime}f, f)$"
387+
@test elementary_weight_latexstring(t6) ==
388+
L"$\sum_{d, e}b_{d}a_{d,e}c_{e}c_{d}$"
380389

381390
t7 = rootedtree([1, 2, 3, 3])
382391
@test order(t7) == 4
@@ -385,7 +394,9 @@ using Aqua: Aqua
385394
@test β(t7) == α(t7) * γ(t7)
386395
@test t7 == t1 t3
387396
@test butcher_representation(t7) == "[[τ²]]"
388-
@test elementary_differential(t7) == L"$f^{\prime}f^{\prime\prime}(f, f)$"
397+
@test elementary_differential_latexstring(t7) ==
398+
L"$f^{\prime}f^{\prime\prime}(f, f)$"
399+
@test elementary_weight_latexstring(t7) == L"$\sum_{d, e}b_{d}a_{d,e}c_{e}^{2}$"
389400

390401
t8 = rootedtree([1, 2, 3, 4])
391402
@test order(t8) == 4
@@ -394,7 +405,10 @@ using Aqua: Aqua
394405
@test α(t8) == 1
395406
@test t8 == t1 t4
396407
@test butcher_representation(t8) == "[[[τ]]]"
397-
@test elementary_differential(t8) == L"$f^{\prime}f^{\prime}f^{\prime}f$"
408+
@test elementary_differential_latexstring(t8) ==
409+
L"$f^{\prime}f^{\prime}f^{\prime}f$"
410+
@test elementary_weight_latexstring(t8) ==
411+
L"$\sum_{d, e, f}b_{d}a_{d,e}a_{e,f}c_{f}$"
398412

399413
t9 = rootedtree([1, 2, 2, 2, 2])
400414
@test order(t9) == 5
@@ -404,7 +418,8 @@ using Aqua: Aqua
404418
@test β(t9) == α(t9) * γ(t9)
405419
@test t9 == t5 t1
406420
@test butcher_representation(t9) == "[τ⁴]"
407-
@test elementary_differential(t9) == L"$f^{(4)}(f, f, f, f)$"
421+
@test elementary_differential_latexstring(t9) == L"$f^{(4)}(f, f, f, f)$"
422+
@test elementary_weight_latexstring(t9) == L"$\sum_{d}b_{d}c_{d}^{4}$"
408423

409424
t10 = rootedtree([1, 2, 2, 2, 3])
410425
@test order(t10) == 5
@@ -414,8 +429,10 @@ using Aqua: Aqua
414429
@test β(t10) == α(t10) * γ(t10)
415430
@test t10 == t3 t2 == t6 t1
416431
@test butcher_representation(t10) == "[[τ]τ²]"
417-
@test elementary_differential(t10) ==
432+
@test elementary_differential_latexstring(t10) ==
418433
L"$f^{\prime\prime\prime}(f^{\prime}f, f, f)$"
434+
@test elementary_weight_latexstring(t10) ==
435+
L"$\sum_{d, e}b_{d}a_{d,e}c_{e}c_{d}^{2}$"
419436

420437
t11 = rootedtree([1, 2, 2, 3, 3])
421438
@test order(t11) == 5
@@ -424,8 +441,10 @@ using Aqua: Aqua
424441
@test α(t11) == 4
425442
@test t11 == t2 t3 == t7 t1
426443
@test butcher_representation(t11) == "[[τ²]τ]"
427-
@test elementary_differential(t11) ==
444+
@test elementary_differential_latexstring(t11) ==
428445
L"$f^{\prime\prime}(f^{\prime\prime}(f, f), f)$"
446+
@test elementary_weight_latexstring(t11) ==
447+
L"$\sum_{d, e}b_{d}a_{d,e}c_{e}^{2}c_{d}$"
429448

430449
t12 = rootedtree([1, 2, 2, 3, 4])
431450
@test order(t12) == 5
@@ -435,8 +454,10 @@ using Aqua: Aqua
435454
@test β(t12) == α(t12) * γ(t12)
436455
@test t12 == t2 t4 == t8 t1
437456
@test butcher_representation(t12) == "[[[τ]]τ]"
438-
@test elementary_differential(t12) ==
457+
@test elementary_differential_latexstring(t12) ==
439458
L"$f^{\prime\prime}(f^{\prime}f^{\prime}f, f)$"
459+
@test elementary_weight_latexstring(t12) ==
460+
L"$\sum_{d, e, f}b_{d}a_{d,e}a_{e,f}c_{f}c_{d}$"
440461

441462
t13 = rootedtree([1, 2, 3, 2, 3])
442463
@test order(t13) == 5
@@ -446,8 +467,10 @@ using Aqua: Aqua
446467
@test β(t13) == α(t13) * γ(t13)
447468
@test t13 == t4 t2
448469
@test butcher_representation(t13) == "[[τ][τ]]"
449-
@test elementary_differential(t13) ==
470+
@test elementary_differential_latexstring(t13) ==
450471
L"$f^{\prime\prime}(f^{\prime}f, f^{\prime}f)$"
472+
@test elementary_weight_latexstring(t13) ==
473+
L"$\sum_{d, e}b_{d}(a_{d,e}c_{e})^{2}$"
451474

452475
t14 = rootedtree([1, 2, 3, 3, 3])
453476
@test order(t14) == 5
@@ -457,8 +480,10 @@ using Aqua: Aqua
457480
@test β(t14) == α(t14) * γ(t14)
458481
@test t14 == t1 t5
459482
@test butcher_representation(t14) == "[[τ³]]"
460-
@test elementary_differential(t14) ==
483+
@test elementary_differential_latexstring(t14) ==
461484
L"$f^{\prime}f^{\prime\prime\prime}(f, f, f)$"
485+
@test elementary_weight_latexstring(t14) ==
486+
L"$\sum_{d, e}b_{d}a_{d,e}c_{e}^{3}$"
462487

463488
t15 = rootedtree([1, 2, 3, 3, 4])
464489
@test order(t15) == 5
@@ -468,8 +493,10 @@ using Aqua: Aqua
468493
@test β(t15) == α(t15) * γ(t15)
469494
@test t15 == t1 t6
470495
@test butcher_representation(t15) == "[[[τ]τ]]"
471-
@test elementary_differential(t15) ==
496+
@test elementary_differential_latexstring(t15) ==
472497
L"$f^{\prime}f^{\prime\prime}(f^{\prime}f, f)$"
498+
@test elementary_weight_latexstring(t15) ==
499+
L"$\sum_{d, e, f}b_{d}a_{d,e}a_{e,f}c_{f}c_{e}$"
473500

474501
t16 = rootedtree([1, 2, 3, 4, 4])
475502
@test order(t16) == 5
@@ -479,8 +506,10 @@ using Aqua: Aqua
479506
@test β(t16) == α(t16) * γ(t16)
480507
@test t16 == t1 t7
481508
@test butcher_representation(t16) == "[[[τ²]]]"
482-
@test elementary_differential(t16) ==
509+
@test elementary_differential_latexstring(t16) ==
483510
L"$f^{\prime}f^{\prime}f^{\prime\prime}(f, f)$"
511+
@test elementary_weight_latexstring(t16) ==
512+
L"$\sum_{d, e, f}b_{d}a_{d,e}a_{e,f}c_{f}^{2}$"
484513

485514
t17 = rootedtree([1, 2, 3, 4, 5])
486515
@test order(t17) == 5
@@ -490,8 +519,20 @@ using Aqua: Aqua
490519
@test β(t17) == α(t17) * γ(t17)
491520
@test t17 == t1 t8
492521
@test butcher_representation(t17) == "[[[[τ]]]]"
493-
@test elementary_differential(t17) ==
522+
@test elementary_differential_latexstring(t17) ==
494523
L"$f^{\prime}f^{\prime}f^{\prime}f^{\prime}f$"
524+
@test elementary_weight_latexstring(t17) ==
525+
L"$\sum_{d, e, f, g}b_{d}a_{d,e}a_{e,f}a_{f,g}c_{g}$"
526+
527+
# test elementary_weight_latexstring which needs more than 23 indices
528+
529+
t18 = rootedtree(collect(1:25))
530+
@test elementary_weight_latexstring(t18) ==
531+
L"$\sum_{d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1, q1, r1, s1, t1, u1, v1, w1, x1, y1, z1, d2}b_{d1}a_{d1,e1}a_{e1,f1}a_{f1,g1}a_{g1,h1}a_{h1,i1}a_{i1,j1}a_{j1,k1}a_{k1,l1}a_{l1,m1}a_{m1,n1}a_{n1,o1}a_{o1,p1}a_{p1,q1}a_{q1,r1}a_{r1,s1}a_{s1,t1}a_{t1,u1}a_{u1,v1}a_{v1,w1}a_{w1,x1}a_{x1,y1}a_{y1,z1}a_{z1,d2}c_{d2}$"
532+
533+
t19 = rootedtree([1, 2, 2, 3, 2, 3])
534+
@test elementary_weight_latexstring(t19) ==
535+
L"$\sum_{d, e}b_{d}(a_{d,e}c_{e})^{2}c_{d}$"
495536

496537
# test non-canonical representation
497538
level_sequence = [1, 2, 3, 2, 3, 4, 2, 3, 2, 3, 4, 5, 6, 2, 3, 4]

0 commit comments

Comments
 (0)