ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 06 Dec 2012 15:36:07 -0600Getting a rooted graph from a nested list of listshttp://ask.sagemath.org/question/9614/getting-a-rooted-graph-from-a-nested-list-of-lists/At [Trac 9329](http://trac.sagemath.org/sage_trac/ticket/9329), a very simple expression tree walker is defined.
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree,expr.operands())
This gives a nested list of lists, like
sage: tree(2*(x+2^x))
[<built-in function add>, [<built-in function mul>, [<built-in function pow>, 2, x], 2],
[<built-in function mul>, x, 2]]
I'm trying to build a similar walker which would (roughly speaking) give the following dictionary which I could plot as a tree.
Graph({'add':['mul-lev1-1','mul-lev1-2'],'mul-lev1-1':['pow-lev2-1','2-lev2-1'],
'pow-lev2-1':['2-lev3','x-lev3'],'mul-lev1-2':['x-lev2','2-lev2-2']}).show(
layout='tree',tree_root='add')
![Binary Tree of this expression](/upfiles/13548511202509835.png)
Naturally, the same element could appear as many times as one liked in any level of the tree, so the naming scheme probably would be overly ponderous... but I'm wondering whether anyone who understands `Converter` better than I do (which isn't saying much) can get around the problem that dictionaries of dictionaries don't work here in any case to build a nice tree walker that can make such graph-able dictionaries. 'Cause doing it by hand is not particularly fun.kcrismanThu, 06 Dec 2012 15:36:07 -0600http://ask.sagemath.org/question/9614/Traversing sage's symbolic expression trees in pythonhttp://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/I'm writing some python code around sage and I need to build an expression tree of the following basic form:
- each node represents an operation
- each child tree represents an expression tree to which the operation applies
Can anyone point me in the right direction as to how to traverse an instance of `sage.symbolic.expression.Expression`, so as to extract this kind of semantic information? rmp251Mon, 12 Nov 2012 11:16:40 -0600http://ask.sagemath.org/question/9503/