def test_second(self): result = subtrees(self.t2_1) expected = LinearCombination() expected[(self.et, self.t2_1)] = 1 expected[(Forest((self.t1_1, )), self.t1_1)] = 1 expected[(Forest((self.t2_1, )), self.et)] = 1 self.assertEqual(expected, result)
def test_equality(self): tree1 = BiColouredTree.basetree(black) tree2 = BiColouredTree(black, Forest()) self.assertEqual(tree1, tree2) tree1 = BiColouredTree.basetree(white) tree2 = BiColouredTree(white, Forest()) self.assertEqual(tree1, tree2)
def test_eighth(self): result = _subtrees_for_antipode(self.t4_4) expected = LinearCombination() expected[(Forest((self.t1_1, self.t1_1, self.t1_1)), self.t1_1)] = 1 expected[(Forest((self.t1_1, self.t1_1)), self.t2_1)] = 3 expected[(Forest((self.t1_1, )), self.t3_2)] = 3 self.assertEqual(expected, result)
def test_fourth(self): result = antipode_ck(self.t3_2) expected = LinearCombination() expected[Forest((self.t3_2, ))] = -1 expected[Forest((self.t2_1, self.t1_1))] = 2 expected[Forest((self.t1_1, self.t1_1, self.t1_1))] = -1 self.assertEqual(expected, result)
def test_equality(self): tree2 = UnorderedTree(Forest()) forest2 = Forest([tree2, tree2]) tree3 = UnorderedTree(forest2) self.assertEqual(tree3, self.tree) tree4 = UnorderedTree('[[],[]]') self.assertEqual(tree4, self.tree) tree5 = UnorderedTree('[[[],[]]]')
def test_fourth(self): result = subtrees(self.t3_2) expected = LinearCombination() expected[(Forest((self.t3_2, )), self.et)] = 1 expected[(Forest((self.t1_1, self.t1_1)), self.t1_1)] = 1 expected[(Forest((self.t1_1, )), self.t2_1)] = 2 expected[(self.et, self.t3_2)] = 1 self.assertEqual(expected, result)
def setUp(self): tmp_b = BiColouredTree.basetree(black) tmp_w = BiColouredTree.basetree(white) forest_b = Forest([tmp_b]) forest_w = Forest([tmp_w]) self.tree1 = BiColouredTree(black, forest_b) self.tree2 = BiColouredTree(black, forest_w) self.tree3 = BiColouredTree(white, forest_b) self.tree4 = BiColouredTree(white, forest_w)
def test_sixth(self): result = _subtrees_for_antipode(self.t4_2) expected = LinearCombination() expected[(Forest((self.t3_2, )), self.t1_1)] = 1 expected[(Forest((self.t1_1, self.t1_1)), self.t2_1)] = 1 expected[(Forest((self.t1_1, )), self.t3_1)] = 2 print(expected) print(result) self.assertEqual(expected, result)
def test_memoization(self): """ Performing a split has an influence on the modified equation computation? """ tmp = UnorderedTree(Forest([leaf])) t = UnorderedTree(Forest([tmp, leaf])) split(t) a = exponential c = modified_equation(a) c
def subtrees(tree): """Return the HCK coproduct. This is function does the heavy lifting when composing B-series. The return value is a :class:`LinearCombination` of 2 tuples. The 0th element in the tuples are the forests of cutting, while the 1st element is the subtree. """ result = LinearCombination() if tree == empty_tree: result += (empty_tree, empty_tree) return result elif isinstance(tree, Forest): if tree.number_of_trees() == 1: for elem in tree: return subtrees(elem) else: # several trees. for elem in tree: amputated_forest = tree.sub(elem) break for pair1, multiplicity1 in subtrees(elem).items(): for pair2, multiplicity2 in subtrees(amputated_forest).items(): if isinstance(pair1[1], UnorderedTree): pair1_1 = Forest((pair1[1], )) else: pair1_1 = pair1[1] if isinstance(pair2[1], UnorderedTree): pair2_1 = Forest((pair2[1], )) else: pair2_1 = pair2[1] # TODO: Nasty workaround. pair = (pair1[0] * pair2[0], pair1_1 * pair2_1) result[pair] += multiplicity1 * multiplicity2 return result result[(Forest((tree, )), empty_tree)] = 1 if tree == leaf: result[(empty_tree, tree)] = 1 return result tmp = [subtrees(child_tree) for child_tree in tree.elements()] # TODO: more efficient looping. # TODO: The multiplicities in "tree" are accounted for by "elements()". tmp = [elem.items() for elem in tmp] # TODO: Try using iterators. for item in product(*tmp): # iterator over all combinations. tensorproducts, factors = zip(*item) multiplicity = 1 for factor in factors: multiplicity *= factor cuttings, to_be_grafted = zip(*tensorproducts) with Forest().clone() as forest_of_cuttings: for forest in cuttings: forest_of_cuttings.inplace_multiset_sum(forest) result[(forest_of_cuttings, UnorderedTree(to_be_grafted))] += \ multiplicity return result
def _subtrees_for_antipode(tree): r"""Slightly modified edition of ``subtrees`` used by ``antipode_ck`` Does not include :math:`\tau \otimes \emptyset` and :math:`\emptyset \otimes \tau`. """ result = LinearCombination() tmp = [subtrees(child_tree) for child_tree in tree.elements()] # TODO: more efficient looping. if tmp: tmp = [elem.items() for elem in tmp] # TODO: Try using iterators. for item in product(*tmp): # iterator over all combinations. tensorproducts, factors = zip(*item) multiplicity = 1 for factor in factors: multiplicity *= factor cuttings, to_be_grafted = zip(*tensorproducts) with Forest().clone() as forest_of_cuttings: for forest in cuttings: forest_of_cuttings.inplace_multiset_sum(forest) result[(forest_of_cuttings, UnorderedTree(to_be_grafted))] += multiplicity result[(empty_tree, tree)] = 0 # TODO: FIND NICER WAY. return result
def antipode_ck(tree): """Return the antipode in the Butcher, Connes, Kreimer Hopf-algebra.""" # TODO: Should be memoized, but linearCOmbination is mutable. # Make LinComb clonable?? result = LinearCombination() if tree == empty_tree: result[empty_tree] = 1 return result elif isinstance(tree, Forest): result[empty_tree] = 1 for tree1, multiplicity in tree.items(): for i in range(multiplicity): tmp = LinearCombination() for forest1, multiplicity1 in antipode_ck(tree1).items(): for forest2, multiplicity2 in result.items(): tmp[forest1 * forest2] += multiplicity1 * multiplicity2 result = tmp return result # TODO: implement multiplication of LinComb. result[Forest((tree, ))] -= 1 for (forest, subtree), multiplicity in _subtrees_for_antipode(tree).items(): for forest2, coefficient in antipode_ck(forest).items(): result[forest2.add(subtree)] -= coefficient * multiplicity return result
def test_first_second(self): tree1 = leaf tree2 = list(D(tree1).keys())[0] expected = LinearCombination() forest1 = Forest([tree1, tree1]) tree3 = UnorderedTree(forest1) expected -= tree3 result = tree_commutator(tree2, tree1) self.assertEqual(result, expected)
def test_RK_series(self): import pybs.rungekutta.methods rule1 = pybs.rungekutta.methods.RKeuler.phi() self.assertEqual(1, rule1(empty_tree)) self.assertEqual(1, rule1(leaf)) tree2 = leaf forest2 = Forest([tree2]) tree3 = UnorderedTree(forest2) result = rule1(tree3) self.assertEqual(0, result)
print('The first tree is: ButcherTree() =', ButcherTree()) print('And the empty tree is: ButcherEmptyTree() =', ButcherEmptyTree()) print( 'And since F(ButcherEmptyTree()) =', F(ButcherEmptyTree()), ', we have D(ButcherEmptyTree) =', str(D(ButcherEmptyTree())) + '. Note: The result is a "LinearCombination" containing one "' + str(ButcherTree.basetree()) + '".') print( 'These two first trees can be accessed through ButcherTree.emptytree() =', ButcherTree.emptytree(), 'and ButcherTree.basetree() =', str(ButcherTree.basetree()) + '.') print('Some methods for constructing new trees are:') print(' 1. by specifying the forest of child trees to the constructor.') tree = ButcherTree(Forest([ButcherTree.basetree(), ButcherTree.basetree()])) print( ' Ex.: tree = ButcherTree(Forest([ButcherTree.basetree(), ButcherTree.basetree()])) = ', tree) print(' 2. by grafting one tree onto another:') print(' Ex.: graft(tree, ButcherTree.basetree()) = ', graft(tree, ButcherTree.basetree())) print(' 3. by taking the derivative of existing trees.') print(' Ex.: D(ButcherTree.basetree()) =', D(ButcherTree.basetree())) print( 'It is also possible to take the derivative of all the trees in a LinearCOmbination. Starting at the empty tree,' ) print( 'the n-th derivative is a LinearCombination of all trees of order n, complete with alpha as the multiplicity:' )
def test_initialisation(self): self.assertIsInstance(BiColouredTree(black, Forest()), BiColouredTree) self.assertIsInstance(BiColouredTree(white, Forest()), BiColouredTree)
def setUp(self): tree1 = BiColouredTree.basetree(black) forest1 = Forest([tree1, tree1]) self.tree = BiColouredTree(white, forest1)
def setUp(self): tree1 = leaf forest1 = Forest([tree1]) self.tree = UnorderedTree(forest1)
def test_first(self): result = antipode_ck(self.t1_1) expected = LinearCombination() expected[Forest((leaf, ))] = -1 self.assertEqual(expected, result)
def test_third(self): result = _subtrees_for_antipode(self.t3_1) expected = LinearCombination() expected[(Forest((self.t2_1, )), self.t1_1)] = 1 expected[(Forest((self.t1_1, )), self.t2_1)] = 1 self.assertEqual(expected, result)
def test_second(self): result = antipode_ck(self.t2_1) expected = LinearCombination() expected[Forest((self.t2_1, ))] = -1 expected[Forest((self.t1_1, self.t1_1))] = 1 self.assertEqual(expected, result)