This repository is the result of my efforts to understand symbolic computation of functions factored as expression trees. With the right concepts at hands, a few lines of code are able compute numeric and symbolic gradients of arbitrary functions. Even expression simplification is easily accomplished. While this library is not complete (and will never be) it offers the interested reader some insights on how to perform symbolic computation.
The code is accompanied by a series of notebooks that explain fundamental concepts for developing your own library that performs symbolic computation:
Foundations
Applications
- Function Optimization view
- Signed Distance Functions and Particle Physics view
This was recently referenced by Donald House co-author of Foundations of Physically Based Modeling & Animation as additional reader supplied material. Signed distance functions are not covered in his book, which makes this contribution a good fit. Most of the math in the notebook used is directly linked with content of the book.
All notebook sources can be found inside the docs folder. There are certainly typos, grammatical errors and all kind of improvements possible. In case you have one for me, I'd be happy to see your pull requests or comments!
The code for symbolic computation contained in cgraph.py can be used as follows.
import cgraph as cg
x = cg.Symbol('x')
y = cg.Symbol('y')
z = cg.Symbol('z')
f = (x * y + 3) / (z - 2)
# Evaluate function
cg.value(f, {x:2, y:3, z:3}) # 9.0
# Partial derivatives (numeric)
d = cg.numeric_gradient(f, {x:2, y:3, z:3})
d[x] # df/dx 3.0
d[z] # df/dz -9.0
# Partial derivatives (symbolic)
d = cg.symbolic_gradient(f)
cg.simplify(d[x]) # df/dx (y*(1/(z - 2)))
cg.value(d[x], {x:2, y:3, z:3}) # df/dx 3.0
# Higher order derivatives
ddx = cg.symbolic_gradient(d[x])
cg.simplify(ddx[y]) # ddf/dxdy (1/(z - 2))
To install CGraph clone this repository and use pip
to install
from local sources.
pip install -e <path/to/setup.py>
Python 3.5/3.6 and numpy is required.
Branch | Status |
---|---|
master | |
develop |
If not otherwise stated all Material is licensed under BSD license.