ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Feb 2020 23:46:52 +0100Acess bracked elements in free lie algebra element?https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/I'm trying to access the left and right elements in a free lie algebra element, whose monomials are stored as binary trees. Thus I want a method (like exists in the source for lie_algebra_element) which would return something like
sage: L = LieAlgebra(QQ, 3, 'x')
sage: x0,x1,x2 = L.gens()
sage: Lyn = FL.Lyndon()
sage: a = Lyn.graded_basis(3)[2]; a
[[x0, x1], x1]
sage: a._left
[x0,x1]
sage: a._right
x1
Of course the last 4 lines are fake. One way of seeing this is the following:
sage: isinstance(a, LyndonBracket)
False
sage: isinstance(a, LieBracket)
False
How do I fix this "the right way" ? My current solution is just to set
sage: a_tree = eval(repr(a)); a_tree
[[x0, x1], x1]
But this feels extremely wrong.Thu, 13 Feb 2020 04:34:45 +0100https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/Answer by rburing for <p>I'm trying to access the left and right elements in a free lie algebra element, whose monomials are stored as binary trees. Thus I want a method (like exists in the source for lie_algebra_element) which would return something like</p>
<pre><code>sage: L = LieAlgebra(QQ, 3, 'x')
sage: x0,x1,x2 = L.gens()
sage: Lyn = FL.Lyndon()
sage: a = Lyn.graded_basis(3)[2]; a
[[x0, x1], x1]
sage: a._left
[x0,x1]
sage: a._right
x1
</code></pre>
<p>Of course the last 4 lines are fake. One way of seeing this is the following:</p>
<pre><code>sage: isinstance(a, LyndonBracket)
False
sage: isinstance(a, LieBracket)
False
</code></pre>
<p>How do I fix this "the right way" ? My current solution is just to set</p>
<pre><code>sage: a_tree = eval(repr(a)); a_tree
[[x0, x1], x1]
</code></pre>
<p>But this feels extremely wrong.</p>
https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/?answer=49906#post-id-49906There seems to be room for improvement in the design here. Basis elements apparently have the same parent as ordinary elements, and `a.monomials()` returns them as such, which makes it hard to get at the underlying tree.
From the source code, accessed by `a??`, we find that we can access the actual monomial by e.g.:
sage: a_tree = a.list()[0][0]; a_tree
[[x0, x1], x1]
sage: isinstance(a_tree, sage.algebras.lie_algebras.lie_algebra_element.LyndonBracket)
True
sage: a_tree._left
[x0, x1]
sage: a_tree._right
x1
You may want to discuss this on [sage-devel](https://groups.google.com/forum/#!forum/sage-devel).Thu, 13 Feb 2020 11:14:15 +0100https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/?answer=49906#post-id-49906Comment by samdehority for <p>There seems to be room for improvement in the design here. Basis elements apparently have the same parent as ordinary elements, and <code>a.monomials()</code> returns them as such, which makes it hard to get at the underlying tree.</p>
<p>From the source code, accessed by <code>a??</code>, we find that we can access the actual monomial by e.g.:</p>
<pre><code>sage: a_tree = a.list()[0][0]; a_tree
[[x0, x1], x1]
sage: isinstance(a_tree, sage.algebras.lie_algebras.lie_algebra_element.LyndonBracket)
True
sage: a_tree._left
[x0, x1]
sage: a_tree._right
x1
</code></pre>
<p>You may want to discuss this on <a href="https://groups.google.com/forum/#!forum/sage-devel">sage-devel</a>.</p>
https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/?comment=49916#post-id-49916I went ahead and posted a topic on sage-devel [here](https://groups.google.com/forum/#!topic/sage-devel/wNvNSqMmL44). Your code works but there is still some unfortunate behavior, namely
sage: a_tree._right == x1
False
which can be worked around via something like
sage: a_tree._right == x1.list()[0][0]
True
Just paying careful attention to which objects are Lyndon basis elements and which are the binary trees. This has worked for my code but it would be way nicer if there was a cleaner way.Thu, 13 Feb 2020 23:46:52 +0100https://ask.sagemath.org/question/49897/acess-bracked-elements-in-free-lie-algebra-element/?comment=49916#post-id-49916