Skip to content

puct9/mathgraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Math graph

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).

Demo

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')

mathgraph_f.png

Partially evaluate the function:

>>> g = f(a=5)
>>> g(b=6)
Constant(127)
>>> g.visualise().write_png('mathgraph_g.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')

mathgraph_dg_db.png

Needs work (but I have no time and am too lazy)

  • Graph simplification (e.g., combining chained powers, etc.)
  • Non-constant exponents (e.g., e^x)
  • Print mathematical formula as text

About

Representing mathematical formulae as graphs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages