Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I found a way to save the above tree to a list and reconstruct the original equation from its elements:

def fun_tree(expr,k=0):
    list_elements = []
    op = expr.operator()
    operands = expr.operands()
    if op:
        print k*' ---',op, operands
        list_elements.append((k,(op, operands)))
        k += 1
        for operand in operands:
    return list_elements


sage: var('a b c x')
sage: E = -a*x^3+x^2+b*(x-c)-a^x
sage: list_elements = fun_tree(E)
sage: level = 0
sage: opr = list_elements[level][1][0]
sage: ops = list_elements[level][1][1]
sage: t1 = ops[0]
sage: for i in srange(1,len(ops)):    t1 = opr(t1, ops[i])
sage: t1

-ax^3 - b(c - x) + x^2 - a^x