Can I get a tree containing the order of operations for an expression?

67 views
Skip to first unread message

Nathan Hayden

unread,
Apr 6, 2016, 5:24:55 PM4/6/16
to sympy
Hi, I'm new to SymPy, but I'm using it to do some simple things already.

I'm wondering, can I feed SymPy an expression and get a tree representation back showing me the order of operations used to evaluate the expression?  I know this is all handled under the hood, but I don't know if it's exposed.


For example, for the expression:   4 * 5 + 6 * (1 + 2)

I'd like to get something like:

                       +
                    /      \
                  *         *
               /    \     /    \
             4     5   6      +
                                /  \
                              1    2

Is this a thing?

Nathan Goldbaum

unread,
Apr 6, 2016, 5:32:32 PM4/6/16
to sy...@googlegroups.com
from sympy.printing.tree import print_tree
from sympy import S

print_tree(S(' 4 * 5 + 6 * (1 + 2)', evaluate=False))

I'm not sure there's a way to make it print like you want, but you should be able to whip something up following this:


(particularly the bits toward the bottom).

-Nathan
 

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/90597353-0ff0-42c8-a554-59f4b069cba2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christophe Bal

unread,
Apr 6, 2016, 5:42:50 PM4/6/16
to sympy-list
Hello.

It should be fairly easy to transform the sympy output into a Graphviz file.


Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French teacher of math in a high school and amateur Python developer

Nathan Hayden

unread,
Apr 6, 2016, 6:02:07 PM4/6/16
to sympy
Excellent!  This is exactly what I was looking for.

Does S() return the tree in memory?

Aaron Meurer

unread,
Apr 6, 2016, 6:04:46 PM4/6/16
to sy...@googlegroups.com

Aaron Meurer

unread,
Apr 6, 2016, 6:05:37 PM4/6/16
to sy...@googlegroups.com
S() converts the string into a SymPy expression (short for sympify()).
SymPy expressions are always stored as trees in memory (that's how
they are represented, see
http://docs.sympy.org/latest/tutorial/manipulation.html).

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/96bc2f21-03a4-4cf7-8567-93bc43446c08%40googlegroups.com.

Nathan Goldbaum

unread,
Apr 6, 2016, 6:07:20 PM4/6/16
to sy...@googlegroups.com


On Wednesday, April 6, 2016, Nathan Hayden <nathan...@gmail.com> wrote:
Excellent!  This is exactly what I was looking for.

Does S() return the tree in memory?

S() is just an alias for sympify. It returns an Expression object, in this case.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/96bc2f21-03a4-4cf7-8567-93bc43446c08%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages