# SciPy 2018 Tutorial Proposal: Code Generation with SymPy

Jason K. Moore edited this page Feb 15, 2018 · 10 revisions

# Title

Code Generation with SymPy

# Tutorial Topic

Computational Science and Numerical Techniques

# Student’s Python Knowledge Level

This tutorial will introduce code generation concepts using the SymPy library. SymPy is a pure Python library for symbolic mathematics. Code generation refers to the act of converting a symbolic expression into equivalent, usually numeric, code in some language. This allows one to use SymPy to symbolically define a mathematical model of a problem, and generate fast numerical code for specific platforms that execute that model. This is a powerful tool that is useful to scientists in many domains. Code generation allows users to speed up existing code, to deal only with the high level mathematical descriptions of a problem, avoids mathematical errors and typos, makes it possible to deal with expressions that would otherwise be too large to write by hand, and opens possibilities to perform automatic mathematical optimizations of expressions.

SymPy supports generating code for C, C++, Fortran, Matlab/Octave, Python, Cython, Julia, JavaScript, LLVM, Rust, Haskell, Mathematica, TensorFlow, and Theano, and can easily be extended to other languages. SymPy's code generation is used by libraries such as PyDy, pyodesys, sympybotics, pycalphad, and many other programs.

The full tutorial materials can be found at:

http://www.sympy.org/scipy-2017-codegen-tutorial/

## Learning objectives

Attendees will be able to:

• write SymPy expressions describing mathematical functions and identify the function arguments and outputs,
• use the SymPy code printers to transform SymPy expressions representing common domain specific functions into multiple output languages,
• use the SymPy code generation routines to output compilable C code and use Cython to access these functions in Python,
• generate custom vectorized functions with the three SymPy functions: lambdify, ufuncify, and autowrap,
• create both custom code printers that make use of specialized C libraries and common subexpression elimination (CSE),
• subclass the core SymPy printers and create a printer for a custom language.

## Outline

Intro to SymPy Expressions [20 minutes]

• Description: Writing common mathematical expressions with SymPy.
• Motivating Examples: Long expressions, Matrix operations, solving systems of equations, derivatives, and differential equations.

Code Printers [30 minutes]

• Description: Printing expressions in multiple languages (C, Fortran, Rust, Julia, Octave, JavaScript, etc)
• Motivating Example: 2D interactive plot in a Jupyter notebook by JavaScript injection

The Easy Way: High Level Generation (lambdify, ufuncify) [50 minutes]

• Description: Generate loop fused NumPy ufuncs and compare to automatically generated NumPy code. Show how you can extend lambdify with custom Python functions.
• Motivating Example: Generate a Jacobian function for a chemical kinetic problem.

The Harder Way: Code generation and compilation [50 minutes]

• Description: Write a tight low level loop with an indexed type and a long expressions with knowing C.
• Motivating Example: Evaluate the chemical kinetic differential equation and its Jacobian.

Cythonizing Your Code (manually and autowrap) [50 minutes]

• Description: Generate C code to evaluate the gradient and Jacobian of an ordinary differential equation and wrap it for use with SciPy's odeint function.

• Motivating Example: Evaluate the chemical kinetic differential equation and its Jacobian.

• Description: Show how to use external C libraries and optimize your code with common sub-expression elimination.

• Motivating Example: Evaluate the chemical kinetic differential equation and its Jacobian.

The attendees will come away with a powerful set of tools that will allow them to develop high performance numerical code using Python that compliments NumPy and SciPy. This tutorial will be ideal for users of the SciPy Stack that would like to increase the performance of their Python code, get into some of the depths of how low-level languages can interact and be used from Python, or to learn a new technique for expressing mathematical models in Python.

# Give us a short bio, including relevant teaching experience. If you have recorded talks or tutorials available online, please include links.

## Jason K. Moore

Jason is a professor at UC Davis in the Mechanical and Aerospace Engineering Department. He is also core developer with both the PyDy and SymPy projects. He utilizes both packages to run optimal control algorithms for biomechanical systems, in particular data driven powered prosthetic designs and human control identification. He is a strong proponent for Open Science and just bought his first new skateboard in over 10 years. Jason has given talks and tutorials at numerous conferences, is a Software Carpentry instructor, and gives 60 to 80 lectures a year while teaching. Some examples are:

## Aaron Meurer

Aaron is the lead developer of SymPy. He works in the ERGS group at the University of South Carolina. He has co-taught tutorials on SymPy as previous SciPy conferences:

Tutorials:

Talks:

## Kenneth R. Lyons

Kenny is a PhD candidate at UC Davis in the Mechanical and Aerospace Engineering Department, where he develops techniques for individuals with different kinds of mobility impairments to control devices like prosthetic limbs and computer communication interfaces. He has several years of teaching assistant experience for courses in engineering and has recently administered a JupyterHub deployment for an undergraduate mechanical vibrations course.

## Benjamin W. L. Margolis

Ben is a Ph.D. student with the Department of Mechanical and Aerospace Engineering at UC Davis. He holds a fellowship with the National Science Foundation Graduate Research Fellowship Program. He earned his B.S. in Engineering Science from Harvey Mudd College in 2010 and M.S. in Applied Math from Santa Clara University in 2016. His research is focused on computational methods for stochastic and optimal control. Between earning his bachelor's and beginning graduate school, Benjamin worked in industrial automation, web development, and medical devices. Ben has tutored subjects from kindergarten to graduate level engineering mathematics, taught as a teaching assistant, and given dozens of technical and business presentations.

## Please provide detailed setup instructions for all necessary software.

Full installation instructions can be found in the README for the tutorial materials:

https://github.com/sympy/scipy-2017-codegen-tutorial#software-installation

# If other topics are a prerequisite, please explain further.

This tutorial assumes a basic knowledge of the SymPy library (note: if a basic SymPy tutorial is submitted and accepted, it should be a prerequisite of this tutorial). We will be working with a number of different languages. Familiarity with the basics of IPython, Jupyter, NumPy, SciPy is also required. Familiarity with Cython, C, and JavaScript will be helpful, but not required.

# Please provide a short Python program that can be used to verify whether a participant’s environment is set up correctly.

The installation test program can be found here:

https://github.com/sympy/scipy-2017-codegen-tutorial/blob/master/test_installation.py

# All tutorials will be reviewed for completeness a week prior to the conference. Do you foresee any problems meeting that deadline?

We do not foresee any problems, especially since this tutorial has been given once in the past. We believe we have sufficient experience in preparation of teaching materials to guarantee everything will be in order by show time.

# Will you be available to help with setup instructions to your pre-tutorial email list in the week prior to your tutorial?

Yes.

##### Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.