def symmetry(self): r""" The symmetry $\\sigma(t)$ of a rooted tree is... **Examples**:: >>> from nodepy import rooted_trees as rt >>> tree=rt.RootedTree('{T^2{T{T}}}') >>> tree.symmetry() 2 **Reference**: - [butcher2003]_ p. 127, eq. 301(b) """ from nodepy.strmanip import getint if self=='T': return 1 sigma=1 if self[1]=='T': try: sigma=factorial(getint(self[3:])) except: pass nleaves,subtrees=self._parse_subtrees() while len(subtrees)>0: st=subtrees[0] nst=subtrees.count(st) sigma*=factorial(nst)*st.symmetry()**nst while st in subtrees: subtrees.remove(st) return sigma
def _parse_subtrees(self): """ Returns the number of leaves and a list of the subtrees, for a given rooted tree. OUTPUT: nleaves -- number of leaves attached directly to the root subtrees -- list of non-leaf subtrees attached to the root The method can be thought of as returning what remains if the root of the tree is removed. For efficiency, instead of returning possibly many copies of 'T', the leaves are just returned as a number. """ from nodepy.strmanip import get_substring, open_to_close, getint if str(self)=='T' or str(self)=='': return 0,[] pos=0 #Count leaves at current level if self[1]=='T': if self[2]=='^': nleaves=getint(self[3:]) else: nleaves=1 else: nleaves=0 subtrees=[] while pos!=-1: pos=self.find('{',pos+1) if pos!=-1: subtrees.append(RootedTree(get_substring(self,pos))) pos=open_to_close(self,pos) return nleaves,subtrees
def _parse_subtrees(self): """ Returns the number of leaves and a list of the subtrees, for a given rooted tree. OUTPUT: nleaves -- number of leaves attached directly to the root subtrees -- list of non-leaf subtrees attached to the root The method can be thought of as returning what remains if the root of the tree is removed. For efficiency, instead of returning possibly many copies of 'T', the leaves are just returned as a number. """ from nodepy.strmanip import get_substring, open_to_close, getint if str(self) == 'T' or str(self) == '': return 0, [] pos = 0 #Count leaves at current level if self[1] == 'T': if self[2] == '^': nleaves = getint(self[3:]) else: nleaves = 1 else: nleaves = 0 subtrees = [] while pos != -1: pos = self.find('{', pos + 1) if pos != -1: subtrees.append(RootedTree(get_substring(self, pos))) pos = open_to_close(self, pos) return nleaves, subtrees
def symmetry(self): r""" The symmetry $\\sigma(t)$ of a rooted tree is... **Examples**:: >>> from nodepy import rooted_trees as rt >>> tree=rt.RootedTree('{T^2{T{T}}}') >>> tree.symmetry() 2 **Reference**: - [butcher2003]_ p. 127, eq. 301(b) """ from nodepy.strmanip import getint if self == 'T': return 1 sigma = 1 if self[1] == 'T': try: sigma = factorial(getint(self[3:])) except: pass nleaves, subtrees = self._parse_subtrees() while len(subtrees) > 0: st = subtrees[0] nst = subtrees.count(st) sigma *= factorial(nst) * st.symmetry()**nst while st in subtrees: subtrees.remove(st) return sigma
def order(self): """ The order of a rooted tree, denoted $r(t)$, is the number of vertices in the tree. **Examples**:: >>> from nodepy import rooted_trees as rt >>> tree=rt.RootedTree('{T^2{T{T}}}') >>> tree.order() 7 """ from nodepy.strmanip import getint if self == 'T': return 1 if self == '': return 0 r = self.count('{') pos = 0 while pos != -1: pos = self.find('T', pos + 1) if pos != -1: try: r += getint(self[pos + 2:]) except: r += 1 return r
def __mul__(self,tree2): """ Returns Butcher's product: t*u is the tree obtained by attaching the root of u as a child to the root of t. """ from nodepy.strmanip import getint if self=='T': return RootedTree('{'+tree2+'}') if tree2=='T': # We're just adding a leaf to self nleaves,subtrees=self._parse_subtrees() if nleaves==0: return RootedTree(self[0]+'T'+self[1:]) if nleaves==1: return RootedTree(self[0]+'T^2'+self[2:]) if nleaves>1: n = getint(self[3:]) return RootedTree(self[0:3]+str(n+1)+self[(3+len(str(n))):]) else: return RootedTree(self[:-1]+tree2+'}') # tree2 wasn't just 'T'
def order(self): """ The order of a rooted tree, denoted $r(t)$, is the number of vertices in the tree. **Examples**:: >>> from nodepy import rooted_trees as rt >>> tree=rt.RootedTree('{T^2{T{T}}}') >>> tree.order() 7 """ from nodepy.strmanip import getint if self=='T': return 1 if self=='': return 0 r=self.count('{') pos=0 while pos!=-1: pos=self.find('T',pos+1) if pos!=-1: try: r+=getint(self[pos+2:]) except: r+=1 return r