avatarMathcube

Summary

The web content provides a tutorial on visualizing expression trees as graphs in SymPy, a computer algebra system.

Abstract

The article "How to Visualize Expression Trees as Graphs in SymPy" is part of a series aimed at helping users master computer algebra systems through practice. It addresses the advanced topic of expression manipulation in SymPy by focusing on the internal representation of expressions as expression trees. The author guides readers through creating a specific SymPy expression and then visualizing its complex internal structure as a graph using the networkx package and the graphviz layout function. The article includes code snippets and images to illustrate the process, emphasizing the utility of such visualization tools in understanding and working with expression trees. The author also encourages readers to install the necessary packages, use the provided code, and consider becoming a Medium member for full access to similar content. Additionally, the article promotes an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus for those interested in AI performance and functions.

Opinions

  • The author believes that visualizing expression trees is very helpful for advanced expression manipulation in SymPy.
  • They suggest that having a visualization function in one's toolbox is highly beneficial, even if the reader is not interested in the underlying code details.
  • The article implies that mastering computer algebra systems requires regular practice and exposure to common and uncommon problems.
  • The author endorses the AI service ZAI.chat, suggesting it offers similar performance to ChatGPT Plus (GPT-4) at a more affordable price.
  • There is an implicit opinion that Medium is a valuable platform for both readers and writers, with the author inviting readers to support them by becoming Medium members.

How to Visualize Expression Trees as Graphs in SymPy

Your Daily Dose of Computer Algebra

About this series: Learning to use computer algebra systems with ease requires a lot of practice. To help you on your journey to mastery, follow me in this series and solve common and not-so-common problems using systems like SymPy, Sage, or Mathematica.

Today’s Problem

Advanced expression manipulation in SymPy requires knowledge about the internal representation of expressions in terms of an expression tree. It is very helpful to visualize such a representation. This is what today’s problem is about.

Create the following SymPy expression

and visualize its internal representation as a graph like in the header of this article.

Solution

The easy part is to create the expression itself. Just define the symbols and build the expression:

The internal representation as a string can easily be obtained from SymPy’s sreprfunction:

Looks pretty complicated, right? That’s why a visualization is quite handy. We have to turn that into a graph. To do that, write a little function to scrape the nodes and then we will convert that into a graph using the networkx package. If you don't have that installed, do it with

pip install networkx

Once you have copied and pasted the code for our little function at the end of this article, you can use it like that:

Most of the content of the plot_graph function is just bookkeeping. We defined some helper classes for representing nodes and node-IDs, but the essential part is the internal _walk function, which recursively goes over the expression and creates nodes and links between the nodes. We store all that in a JSON format, which can be read by networkxand use its graphviz layout function to draw the graph as a tree structure. If you are not interested in the details of the code, that's fine, but I highly recommend having such a visualization function in your toolbox, so you may want to grab the code and simply use it for yourself.

And finally, here is the code:

If you found this article useful, you may want to consider becoming a Medium member to get unlimited access to all Medium articles. By registering using this link you can even support me as a writer.

Mathematics
Mathematics Education
Programming
Python
Science
Recommended from ReadMedium