Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SymPy Code Generation

SymPy Code Generation

SymPy code generation talk given at SciPy 2016. Full slides with animations are at https://github.com/asmeurer/SciPy-2016-Talk

Aaron Meurer

July 14, 2016
Tweet

More Decks by Aaron Meurer

Other Decks in Science

Transcript

  1. SymPy • Powerful computer algebra system (CAS) written in pure

    Python • BSD licensed • Usable as a library • Just released version 1.0
  2. Code Generation • Translate SymPy expressions to another language •

    Example: translate |sin(π⋅x)| to C >>> ccode(abs(sin(pi*x))) 'fabs(sin(M_PI*x))'
  3. Languages supported • C • Fortran • MATLAB/Octave • Python

    (NumPy/SciPy) • Julia • Mathematica • Javascript • LLVM • Rust • Theano • Easy to extend to others
  4. Code generation workflow Derive mathematical formulas symbolically Convert symbolic expressions

    to numeric function Use numeric function to solve some problem SymPy Code generation Domain specific
  5. Code generation workflow Derive mathematical formulas symbolically Convert symbolic expressions

    to numeric function Use numeric function to solve some problem SymPy Code generation Domain specific
  6. Code generation levels of abstraction Code printers codegen ufuncify lambdify

    'fabs(sin(M_PI*x))' #include "f.h" #include <math.h> double f(double x) { double f_result; f_result = fabs(sin(M_PI*x)); return f_result; } f = ufuncify(x, abs(sin(pi*x))) Expression Larger block of code Python callable
  7. Why do code generation? 1. SymPy can deal with mathematical

    expressions in a high-level way. For example, it can take symbolic derivatives.
  8. Why do code generation? 1. SymPy can deal with mathematical

    expressions in a high-level way. For example, it can take symbolic derivatives. 2. Using code generation avoids mistakes that come from translating mathematics into low level code.
  9. Why do code generation? 1. SymPy can deal with mathematical

    expressions in a high-level way. For example, it can take symbolic derivatives. 2. Using code generation avoids mistakes that come from translating mathematics into low level code. 3. It’s possible to deal with expressions that are otherwise too large to write by hand.
  10. Why do code generation? 1. SymPy can deal with mathematical

    expressions in a high-level way. For example, it can take symbolic derivatives. 2. Using code generation avoids mistakes that come from translating mathematics into low level code. 3. It’s possible to deal with expressions that are otherwise too large to write by hand. 4. Some “mathematical” optimizations are possible, which a normal compiler would not be able to do.
  11. Iodine-131 • Iodine-131 has a half-life of 8.0197 days •

    Cells in the thyroid absorb Iodine • Radioactive Iodine-131 destroys thyroid cells by short-range beta radiation
  12. • I-131 decays with a half-life of 8.02 days 131I

    ⟶ β- + 131*Xe 131I ⟶ β- + 131Xe 131*Xe ⟶ 131Xe @xi @t = ixi( t ) + X i6=j j j!ixj( t )
  13. • This is a simple example, because the decay of

    131I only results in three species, 131I, 131*Xe, and 131Xe • A typical decay chain may result in hundreds of species • With SymPy, we can avoid mistakes by representing the decay equations in a high level way, and deriving the low level representations
  14. PyDy • Multibody dynamics • SymPy is used to derive

    the equations of motion for a mechanical system • The resulting equations are a large system of nonlinear ODEs which need to be integrated (F=Ma) • Code generation allows creating fast callbacks which can be integrated over many time steps
  15. • Needs to be fast because: • Realtime simulation •

    Optimal control • Stiff systems require more time steps
  16. • trigsimp() can simplify the equations of motion significantly •

    In this example, 
 sin(x)⋅cos(y) - sin(y)⋅cos(x) ⟶ sin(x - y) • The equations must be evaluated at each time step, so this can make a significant difference in performance
  17. How does it work? • SymPy expressions are stored as

    trees • Example: x2 + xy Add Mul Pow Symbol(‘x’) 2 Symbol(‘y’) Symbol(‘x’)
  18. How does it work? • Every expression stores its children

    expression in .args >>> (x**2 + x*y).args (x**2, x*y) >>> (x**2 + x*y).args[0].args (x, 2)
  19. How does it work? class CCodePrinter(CodePrinter): def _print_Rational(self, expr): p,

    q = int(expr.p), int(expr.q) return '%d.0L/%d.0L' % (p, q) def _print_Exp1(self, expr): return "M_E" def _print_Pi(self, expr): return 'M_PI'
  20. How does it work? • Printer subclasses walk the expression

    tree and call methods corresponding to children (visitor pattern) • Subclass CodePrinter, define methods for the expression types to code generate • Easy to write your own code printers, or to extend existing code printers to do the things you need
  21. Some other libraries that use SymPy code generation • Chemreac

    • python library for solving chemical kinetics problems with possible diffusion and drift contributions • SymPyBotics • Symbolic Framework for Modeling and Identification of Robot Dynamics
  22. Takeaways 1. SymPy can deal with mathematical expressions in a

    high-level way. For example, it can take symbolic derivatives. 2. Using code generation avoids mistakes that come from translating mathematics into low level code. 3. It’s possible to deal with expressions that are otherwise too large to write by hand. 4. Some “mathematical” optimizations are possible, which a normal compiler would not be able to do.
  23. • Mailing list: http://groups.google.com/group/ sympy • https://github.com/sympy/sympy • @asmeurer, @SymPy

    on Twitter • These slides are at https://github.com/asmeurer/ SciPy-2016-Talk • I’ll be at the sprints (and other SymPy developers)