What can you do if you represent a mathematical function as a graph of its constituent operations as nodes?
Probably not an original idea, but I was inspired by graph representations of neural networks.
Still, the end result is pretty cool :D.
I also have no idea what to do with it but want to share it anyways.
Requires Graphviz and pydot
(pip install pydot
).
Define a function:
from mathgraph import compile_operation
@compile_operation
def f(a, b):
return (a + b) ** 2 + b
Evaluate the function:
>>> f(a=2, b=3)
Constant(28)
Visualise the function:
>>> f.visualise().write_png('mathgraph_f.png')
Partially evaluate the function:
>>> g = f(a=5)
>>> g(b=6)
Constant(127)
>>> g.visualise().write_png('mathgraph_g.png')
Differentiate the function:
>>> dg_db = g.gradient('b').simplified()
>>> dg_db(b=6)
Constant(23)
>>> dg_db.visualise().write_png('mathgraph_dg_db.png')
- Graph simplification (e.g., combining chained powers, etc.)
- Non-constant exponents (e.g.,
e^x
) - Print mathematical formula as text