Tree-Form of math formula (as a dot string)

Viewed 397
1 Answers

To do this, we can use sympy to generate the tree and Graphviz to render it. Below is the code to generate the digraph data and render it into a pdf. Note that in order to use Graphviz you need to install the Graphviz executables as well as installing through pip. Hope that helps!

from sympy import symbols, cos, dotprint
from graphviz import Source

a, b, c, x= symbols('a b c x')
expr = a * b * cos( c * x ) + b

src = Source(dotprint(expr))

src.render('output.gv', view=True)

enter image description here

Related