You might look at the preorder_traversal() and postorder_traversal() functions in sympy/utilities/iterables.py:
In [4]: a = (x + 2)*(x + 1)**2 In [5]: from sympy.utilities.iterables import preorder_traversal, postorder_traversal In [6]: preorder_traversal(a) Out[6]: <generator object preorder_traversal at 0x102b099b0> In [7]: list(preorder_traversal(a)) Out[7]: ⎡ 2 2 ⎤ ⎣(1 + x) ⋅(2 + x), (1 + x) , 1 + x, 1, x, 2, 2 + x, 2, x⎦ In [8]: list(postorder_traversal(a)) Out[8]: ⎡ 2 2 ⎤ ⎣1, x, 1 + x, 2, (1 + x) , 2, x, 2 + x, (1 + x) ⋅(2 + x)⎦ Also, if you are only interested in visualizing, the print_tree() function can help: In [9]: print_tree(a) Mul: (1 + x)**2*(2 + x) +-Pow: (1 + x)**2 | +-Add: 1 + x | | +-One: 1 | | | prime: True | | | real: True | | | comparable: True | | | finite: True | | | commutative: True | | | infinitesimal: False | | | nonpositive: False | | | composite: False | | | positive: True | | | noninteger: False | | | negative: False | | | nonnegative: True | | | zero: False | | | complex: True | | | bounded: True | | | rational: True | | | unbounded: False | | | integer: True | | | imaginary: False | | | nonzero: True | | | irrational: False | | +-Symbol: x | | comparable: False | +-Integer: 2 | real: True | nonzero: True | comparable: True | commutative: True | nonpositive: False | unbounded: False | noninteger: False | complex: True | nonnegative: True | zero: False | infinitesimal: False | bounded: True | negative: False | rational: True | positive: True | integer: True | imaginary: False | finite: True | irrational: False +-Add: 2 + x +-Integer: 2 | real: True | nonzero: True | comparable: True | commutative: True | nonpositive: False | unbounded: False | noninteger: False | complex: True | nonnegative: True | zero: False | infinitesimal: False | bounded: True | negative: False | rational: True | positive: True | integer: True | imaginary: False | finite: True | irrational: False +-Symbol: x comparable: False If you don't want to see all the assumptions stuff, you will have to edit the code in sympy/printing/tree.py. Aaron Meurer On Jun 22, 2010, at 1:36 AM, nelson wrote: > Thank you, it worked well. I can't find how in sympy i can obtain the > entire expression parsing tree. I want to be able to navigate from my > app, selecting part of the expression (having the expression tree and > navigation functions, this would be very easy), and applying to this > expression some operation. > > Is there some common approach to this problem? > > thanks > > On Jun 15, 11:15 pm, "Aaron S. Meurer" <asmeu...@gmail.com> wrote: >> Hi. >> >> The easiest way to do this is to use .args: >> >> In [1]: a = (x + 2)*(x + 1)**2 >> >> In [2]: a >> Out[2]: >> 2 >> (1 + x) ⋅(2 + x) >> >> In [3]: a.args >> Out[3]: >> ⎛ 2 ⎞ >> ⎝(1 + x) , 2 + x⎠ >> >> In [4]: a.args[0] >> Out[4]: >> 2 >> (1 + x) >> >> In [5]: a.args[0].expand()*a.args[1] >> Out[5]: >> ⎛ 2⎞ >> (2 + x)⋅⎝1 + 2⋅x + x ⎠ >> >> Aaron Meurer >> On Jun 15, 2010, at 12:31 PM, nelson wrote: >> >> >> >> >> >> >> >>> hi all! >>> i am evaluating sympy for a project i'm doing. Basically i want to >>> be able to extract a part of an expression, for examplre (x+1)^2 out >>> of (x+2)*(x+1)^2 and performing an operation on it. For example,, >>> expanding the (x+1)^2 term wold return the expression >>> (x+2)*(x^2+2*x+1) >> >>> is it possibile using sympy? how can i accomplish this task? >> >>> thanks in advance, >>> Andrea >> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "sympy" group. >>> To post to this group, send email to sy...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> sympy+unsubscr...@googlegroups.com. >>> For more options, visit this group >>> athttp://groups.google.com/group/sympy?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to sy...@googlegroups.com. > To unsubscribe from this group, send email to > sympy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sy...@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.