uconf.core.trees¶
Rooted tree utilities for bar/cobar constructions.
Trees are represented using the RootedTree class:
A leaf is a plain
intin{1, …, n}(the arity).An internal vertex is a
RootedTreeinstance holding adecoration(an operad/cooperad basis key, always a tuple) and an ordered sequence ofchildren(each a leaf or anotherRootedTree). For connected operads, every vertex has at least 2 children.
RootedTree is immutable and hashable, making it suitable as a
basis key in SageMath’s CombinatorialFreeModule and as an argument to
@cached_method.
Structural properties that the old tuple encoding recomputed from scratch
on every call — weight, leaves, min_leaf, tree_arity — are
now eagerly cached at construction time and available in O(1).
Example (arity 3, weight 2):
RootedTree((1, 2), RootedTree((1,), 1, 2), 3)
represents a tree with root decorated by the Lie basis key (1, 2)
(a binary bracket), whose first child is another internal vertex
decorated by (1,) with leaves 1 and 2, and whose second child is leaf 3.
Functions
|
Total cobar degree of internal vertices after leaf_i in DFS order. |
|
Return the children of an internal vertex as a tuple. |
|
Contract one internal edge, merging parent and child vertices. |
|
Create a tree with the same structure but new decorations. |
|
Return the decoration (operad basis key) of an internal vertex. |
Enumerate DFS-canonical planar trees with an arbitrary per-vertex degree offset. |
|
|
Enumerate planar-decorated trees in |
Enumerate shuffle trees of |
|
Enumerate shuffle trees of |
|
Enumerate shuffle trees with an arbitrary per-vertex degree offset. |
|
|
Enumerate all shuffle-tree basis elements of |
|
Expand a vertex into two vertices connected by an edge. |
|
Graft |
|
Enumerate all internal edges (parent-child pairs between internal vertices). |
|
Return True if |
|
Return True if |
|
Check if a tree is in shuffle form. |
|
Return the (frozen)set of all leaf labels in the tree. |
|
Return the minimum leaf label in a tree or subtree. |
|
Apply a leaf relabeling according to |
|
Replace the decoration of a specific vertex in the tree. |
|
Split the tree at a given internal vertex. |
|
Normalize a tree to shuffle form for the bar construction B(P). |
|
Normalize a tree to shuffle form for the cobar construction Ω(C). |
|
Return the arity of the tree (number of leaves). |
|
Return a LaTeX representation of a decorated rooted tree. |
|
Return a human-readable string representation of a decorated tree. |
|
Validate a tree for use in bar/cobar constructions. |
|
Return the arity (number of children) of an internal vertex. |
|
Return all internal vertices in depth-first (pre-order) traversal. |
|
Return the weight (number of internal vertices) of the tree. |
Classes
|
Immutable rooted tree with eagerly cached structural properties. |
- class uconf.core.trees.RootedTree(decoration, *children)[source]¶
Bases:
objectImmutable rooted tree with eagerly cached structural properties.
Parameters¶
- decorationtuple
Operad/cooperad basis key decorating this vertex.
*childrenint | RootedTreeOrdered children. Each child is either a leaf (
int) or anotherRootedTree.
Cached attributes (O(1) access after construction):
_decoration: the vertex decoration._children: tuple of children._arity: number of children (vertex arity)._weight: total number of internal vertices in this subtree._leaves:frozensetof all leaf labels._min_leaf: minimum leaf label._tree_arity: total number of leaves.
RootedTreeis immutable: all__slots__are set once in__init__and__setattr__is overridden to prevent mutation.- Parameters:
decoration (tuple)
children (int | RootedTree)
- uconf.core.trees.is_leaf(node)[source]¶
Return True if
nodeis a leaf (an integer).- Return type:
bool
- uconf.core.trees.is_internal(node)[source]¶
Return True if
nodeis an internal vertex.- Return type:
bool
- uconf.core.trees.decoration(vertex)[source]¶
Return the decoration (operad basis key) of an internal vertex.
- Parameters:
vertex (RootedTree)
- Return type:
tuple
- uconf.core.trees.children(vertex)[source]¶
Return the children of an internal vertex as a tuple.
- Parameters:
vertex (RootedTree)
- Return type:
tuple
- uconf.core.trees.vertex_arity(vertex)[source]¶
Return the arity (number of children) of an internal vertex.
- Parameters:
vertex (RootedTree)
- Return type:
int
- uconf.core.trees.leaves(tree)[source]¶
Return the (frozen)set of all leaf labels in the tree.
O(1) for
RootedTreenodes (cached); O(n) for bareintleaves.- Return type:
frozenset[int]
- uconf.core.trees.weight(tree)[source]¶
Return the weight (number of internal vertices) of the tree.
O(1) for
RootedTreenodes (cached).- Return type:
int
- uconf.core.trees.tree_arity(tree)[source]¶
Return the arity of the tree (number of leaves).
O(1) for
RootedTreenodes (cached).- Return type:
int
- uconf.core.trees.vertices_dfs(tree)[source]¶
Return all internal vertices in depth-first (pre-order) traversal.
Each entry is the
RootedTreeobject itself (identity-safe).- Return type:
list[RootedTree]
- uconf.core.trees.after_cobar_deg(tree, leaf_i, cooperad_cls, base_ring)[source]¶
Total cobar degree of internal vertices after leaf_i in DFS order.
In the DFS of a shuffle tree, the root vertex is visited first, then children are recursed left-to-right. A leaf is visited in place.
after_cobar_degreturns the sum of(deg_C(v) - 1)for every internal vertexvwhose DFS visit occurs after the visit of leaf_i.For a corolla (single internal vertex) this is always 0, since the root is visited before any leaf.
- Parameters:
leaf_i (int)
- Return type:
int
- uconf.core.trees.internal_edges_dfs(tree)[source]¶
Enumerate all internal edges (parent-child pairs between internal vertices).
Returns a list of
(parent_vertex, child_position, child_vertex)tuples wherechild_positionis 1-indexed (the slot in the operad composition).- Return type:
list[tuple[RootedTree, int, RootedTree]]
- uconf.core.trees.contract_edge(tree, parent_vertex, child_pos, new_decoration)[source]¶
Contract one internal edge, merging parent and child vertices.
The
child_pos-th child ofparent_vertexis an internal vertex. Replace both with a single vertex havingnew_decorationand the combined children (children of parent before child_pos, children of child, children of parent after child_pos).This function recursively finds and contracts the specified edge in the tree.
- Parameters:
tree (RootedTree)
parent_vertex (RootedTree)
child_pos (int)
new_decoration (tuple)
- Return type:
- uconf.core.trees.graft(tree_top, i, tree_bot, relabel_bot=None)[source]¶
Graft
tree_botonto leafioftree_top.If
relabel_botis provided, it maps each leaf oftree_botto its new label in the grafted tree. If not provided, leaves oftree_botare relabeled to{i, i+1, ..., i+n_bot-1}and leaves > i intree_topare shifted byn_bot - 1.Returns the grafted tree.
- Parameters:
i (int)
relabel_bot (dict | None)
- uconf.core.trees.relabel_leaves(tree, mapping)[source]¶
Apply a leaf relabeling according to
mapping.- Parameters:
mapping (dict)
- uconf.core.trees.split_at_vertex(tree, target_vertex)[source]¶
Split the tree at a given internal vertex.
Returns
(tree_top, position, tree_bot)where: -tree_botis the subtree rooted attarget_vertex-tree_topis the tree withtarget_vertexreplaced by a single leaf -positionis the position of that leaf in the grafting senseReturns None if
target_vertexis the root (can’t split at root).- Parameters:
tree (RootedTree)
target_vertex (RootedTree)
- Return type:
tuple[RootedTree | int, int, RootedTree] | None
- uconf.core.trees.validate_tree(tree, arity, operad_cls, base_ring)[source]¶
Validate a tree for use in bar/cobar constructions.
Checks: - Leaves are exactly {1, …, arity} - All internal vertices have arity >= 2 (connected assumption) - All decorations are valid for the given operad/cooperad
Returns a validated tree with cleaned decorations, raises if invalid, or None if decorations are invalid but the tree structure is fine.
- Parameters:
arity (int)
- Return type:
RootedTree | Literal[1] | None
- uconf.core.trees.enumerate_planar_trees_in_degree(arity, weight_bound, operad_cls, base_ring, target_degree)[source]¶
Enumerate planar-decorated trees in
B(P)(arity)of bar degreetarget_degree.A tree is planar (in the quasi-planar cooperad sense) when:
All vertex decorations are planar elements of the base operad.
Children at each vertex have consecutive leaf-label ranges, so that
planarize(T) = T ⊗ id(the global permutation is the identity).
Condition 2 means that the tree is a standard planar tree: leaf labels run
1, …, nstrictly left-to-right, and each child subtree occupies a contiguous block. All such trees are automatically in shuffle form.Uses the operad’s
planar_basis_iterat the exact required degree for each vertex, so this is efficient even for operads with many basis elements per degree (e.g. Barratt–Eccles).Requires the operad to implement
planar_basis_iter.The enumeration mirrors the binary-root topologies produced by
enumerate_trees_by_weight, extended to planar decorations and restricted to consecutive child-leaf ranges.- Parameters:
arity (int) – Number of leaves (arity of the bar-construction component).
weight_bound (int) – Maximum number of internal vertices. For connected operads, callers should pass
arity - 1(the hard upper bound from the branching constraintsum(m_v - 1) = n - 1).operad_cls (Any) – Operad factory; must supply
planar_basis_iter.base_ring (Any) – Coefficient ring.
target_degree (int) – Exact bar degree to enumerate. May be any integer, including zero or negative, when the base operad has elements of negative degree (e.g. a shifted operad).
- Return type:
Iterator[RootedTree]
- uconf.core.trees.tree_to_string(tree, decoration_formatter)[source]¶
Return a human-readable string representation of a decorated tree.
- Parameters:
tree – A rooted tree (
RootedTreeor leafint).decoration_formatter (Callable[[tuple, int], str]) – Optional callback
(decoration, arity) -> str. When provided, it is used to render each vertex decoration.
- Return type:
str
- uconf.core.trees.tree_to_latex(tree, decoration_formatter)[source]¶
Return a LaTeX representation of a decorated rooted tree.
- Parameters:
tree – A rooted tree (
RootedTreeor leafint).decoration_formatter (Callable[[tuple, int], str]) – Optional callback
(decoration, arity) -> str. When provided, it is used to render each vertex decoration.
- Return type:
str
- uconf.core.trees.copy_tree_structure(old_tree, new_decorations)[source]¶
Create a tree with the same structure but new decorations.
new_decorationsshould be in DFS order matchingvertices_dfs(old_tree).- Parameters:
new_decorations (list[tuple])
- Return type:
- uconf.core.trees.replace_vertex_decoration(tree, target, new_decoration)[source]¶
Replace the decoration of a specific vertex in the tree.
- Parameters:
tree (RootedTree)
target (RootedTree)
new_decoration (tuple)
- uconf.core.trees.expand_vertex(tree, target_vertex, child_pos, left_decoration, right_decoration, left_arity, right_arity)[source]¶
Expand a vertex into two vertices connected by an edge.
Used in the cobar differential
d_2. The infinitesimal cocomposition Delta^{l; a, b}(c) splits an arity-k vertex (k = a + b - 1) into: - Top vertex with arity a, decoration c_L - Bottom vertex with arity b, decoration c_R - They connect at position l of the top vertex - Original children 1..l-1 go to top positions 1..l-1 - Original children l..l+b-1 go to bottom positions 1..b - Original children l+b..k go to top positions l+1..a- Parameters:
tree (RootedTree)
target_vertex (RootedTree)
child_pos (int)
left_decoration (tuple)
right_decoration (tuple)
left_arity (int)
right_arity (int)
- Return type:
- uconf.core.trees.min_leaf(tree)[source]¶
Return the minimum leaf label in a tree or subtree.
O(1) for
RootedTreenodes (cached).- Return type:
int
- uconf.core.trees.is_shuffle_tree(tree)[source]¶
Check if a tree is in shuffle form.
A shuffle tree has, at each internal vertex, children ordered so that the minimum leaf label in each child subtree increases from left to right.
Example
RootedTree((), 1, 2)is shuffle (min({1})=1 < min({2})=2)RootedTree((), 2, 1)is NOT shuffle (min({2})=2 > min({1})=1)- Return type:
bool
- uconf.core.trees.to_shuffle_tree_bar(tree, operad_cls, base_ring)[source]¶
Normalize a tree to shuffle form for the bar construction B(P).
Returns a list of
(shuffle_tree, coeff)pairs representing a (possibly multi-term) linear combination.At each vertex, children are sorted by min leaf. The decoration is acted on by the sorting permutation (using the operad’s permute method), and Koszul signs are computed based on bar-degrees of subtrees.
When the operad’s
permuteproduces multiple basis terms (e.g. for Lie-based operads), each term gives a separate output tree.
- uconf.core.trees.enumerate_shuffle_trees_in_degree(arity, weight_bound, operad_cls, base_ring, target_degree)[source]¶
Enumerate all shuffle-tree basis elements of
B(P)(arity)in bar degree target_degree.A shuffle tree is a rooted tree with leaves
{1, ..., arity}in which children at every vertex are sorted by their minimum leaf label. Together with one decoration per vertex drawn from any basis ofP(k), shuffle trees form a vector-space basis for the bar construction of any connected symmetric operad — not just quasi-planar ones.Unlike
enumerate_planar_trees_in_degree(), this function does not require the base operad to implementplanarizeorplanar_basis_iter. It relies only onoperad_cls(k, base_ring)having abasis_itermethod (degree-aware or not) or Sage’sbasis()family.- Parameters:
arity (int) – Number of leaves (arity of the bar-construction component).
weight_bound (int) – Maximum number of internal vertices. Pass
arity - 1for connected operads (the hard upper bound fromsum(m_v - 1) = n - 1).operad_cls (Any) – Operad factory.
base_ring (Any) – Coefficient ring.
target_degree (int) – Exact bar degree to enumerate. May be any integer, including zero or negative, when the base operad has elements of negative degree (e.g. a shifted operad).
- Yields:
Decorated shuffle trees as nested tuples valid as basis keys of
BarConstruction(operad_cls)(arity).- Return type:
Iterator[tuple]
- uconf.core.trees.enumerate_planar_trees_generic_in_degree(arity, weight_bound, operad_cls, base_ring, target_degree, vertex_offset, use_planar_decs=True)[source]¶
Enumerate DFS-canonical planar trees with an arbitrary per-vertex degree offset.
A planar tree in the DFS-canonical sense has leaves
1, …, nin strict left-to-right order and every child subtree occupies a contiguous block of leaf labels. Together with planar vertex decorations (one perS_k-orbit), these trees form a basis for the planar partP_pl(n)of a quasi-planar symmetric sequenceP.Unlike
enumerate_shuffle_trees_generic_in_degree(), which generates all shuffle trees (and thus over-counts orbits when the canonical form ofP(n)underplanarizeis DFS-canonical rather than shuffle), this function yields exactly one representative perS_n-orbit, matching the output ofP.Component.planarize(T)whensigma_global == id.The three canonical choices for
vertex_offsetare:+1: bar degreeΣ (deg_P(v) + 1).0: free degreeΣ deg_P(v)(no suspension).-1: cobar degreeΣ (deg_C(v) − 1).
- Parameters:
arity (int) – Number of leaves.
weight_bound (int) – Maximum number of internal vertices.
operad_cls (Any) – Operad or cooperad factory for vertex decorations.
base_ring (Any) – Coefficient ring.
target_degree (int) – Exact total degree to enumerate.
vertex_offset (int) – Per-vertex degree contribution.
use_planar_decs (bool) – When
True(default), restrict vertex decorations to the planar basis viaplanar_basis_iter.
- Yields:
Decorated planar trees (nested tuples) as valid tree basis keys.
- Return type:
Iterator[tuple]
- uconf.core.trees.enumerate_shuffle_trees_free_in_degree(arity, weight_bound, operad_cls, base_ring, target_degree)[source]¶
Enumerate shuffle trees of
arityleaves with free degree target_degree.The free degree of a decorated tree is
Σ_v deg_P(dec(v))— the sum of operad/cooperad element degrees over all internal vertices, without the per-vertex+1offset used by the bar construction. This is the natural degree for the free P-algebra composite productP ∘ Mand the cofree conilpotent C-coalgebraT^c_C(M).- Parameters:
arity (int) – Number of leaves.
weight_bound (int) – Maximum number of internal vertices (pass
arity - 1for connected operads).operad_cls (Any) – Operad or cooperad factory used for vertex decorations.
base_ring (Any) – Coefficient ring.
target_degree (int) – Exact free degree to enumerate.
- Yields:
Decorated shuffle trees (nested tuples) as valid tree basis keys.
- Return type:
Iterator[tuple]
- uconf.core.trees.enumerate_shuffle_trees_cobar_in_degree(arity, weight_bound, cooperad_cls, base_ring, target_degree)[source]¶
Enumerate shuffle trees of
arityleaves with cobar degree target_degree.The cobar degree of a decorated tree is
Σ_v (deg_C(dec(v)) - 1)— the sum of desuspended cooperad element degrees over all internal vertices. This is the natural degree for the cobar constructionΩ(C)and the cobar complexΩ_C(V).- Parameters:
arity (int) – Number of leaves.
weight_bound (int) – Maximum number of internal vertices (pass
arity - 1for connected cooperads).cooperad_cls (Any) – Cooperad factory used for vertex decorations.
base_ring (Any) – Coefficient ring.
target_degree (int) – Exact cobar degree to enumerate. May be negative when the cooperad has elements of low degree (e.g.
CoAssociativein degree 0 contributes-1per vertex).
- Yields:
Decorated shuffle trees (nested tuples) as valid tree basis keys.
- Return type:
Iterator[tuple]
- uconf.core.trees.enumerate_shuffle_trees_generic_in_degree(arity, weight_bound, operad_cls, base_ring, target_degree, vertex_offset, use_planar_decs=False)[source]¶
Enumerate shuffle trees with an arbitrary per-vertex degree offset.
The three canonical choices for
vertex_offsetare:+1: bar degreeΣ (deg_P(v) + 1).0: free degreeΣ deg_P(v)(no suspension).-1: cobar degreeΣ (deg_C(v) - 1).
- Parameters:
arity (int) – Number of leaves.
weight_bound (int) – Maximum number of internal vertices.
operad_cls (Any) – Operad or cooperad factory for vertex decorations.
base_ring (Any) – Coefficient ring.
target_degree (int) – Exact tree degree to enumerate.
vertex_offset (int) – Per-vertex degree offset.
use_planar_decs (bool) – When
True, restrict vertex decorations to the planar basis (viaplanar_basis_iter) instead of the full basis. Use this for composite-productP ∘ Menumeration to obtain one representative perS_n-orbit.
- Yields:
Decorated shuffle trees (nested tuples) as valid tree basis keys.
- Return type:
Iterator[tuple]
- uconf.core.trees.to_shuffle_tree_cobar(tree, cooperad_cls, base_ring)[source]¶
Normalize a tree to shuffle form for the cobar construction Ω(C).
Returns a list of
(shuffle_tree, coeff)pairs representing a (possibly multi-term) linear combination.For the implemented cobar grading, subtree degrees are computed by
subtree_degree_cobar(sum ofdeg_C(v) - 1over internal vertices).