<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatic Code Generation with SymPy\n",
    "\n",
    "This tutorial will introduce code generation concepts using the [SymPy](http://sympy.org) library. SymPy is a pure Python library for symbolic mathematics. Code generation refers to the act of converting a SymPy symbolic expression into equivalent code in some language, typically for numeric evaluation. This allows one to use SymPy to symbolically model a problem and generate fast numerical code for specific platforms that executes 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 mathematics 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.\n",
    "\n",
    "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.\n",
    "\n",
    "## Instructors\n",
    "\n",
    "- <NAME>, KTH Royal Institute of Technology\n",
    "- <NAME>, University of California, Davis\n",
    "- <NAME>, University of South Carolina\n",
    "- <NAME>, University of California, Davis\n",
    "\n",
    "## Learning objectives\n",
    "\n",
    "Attendees will be able to:\n",
    "- write SymPy expressions describing mathematical functions and identify the\n",
    "  function arguments and outputs,\n",
    "- use the SymPy code printers to transform SymPy expressions representing\n",
    "  common domain specific functions into multiple output languages,\n",
    "- use the SymPy code generation routines to output compilable C code and use\n",
    "  Cython to access these functions in Python,\n",
    "- generate custom vectorized functions with the three SymPy functions:\n",
    "  lambdify, ufuncify, and autowrap,\n",
    "- create both custom code printers that make use of specialized C libraries and\n",
    "  common subexpression elimination (CSE),\n",
    "- subclass the core SymPy printers and create a printer for a custom language.\n",
    "\n",
    "## Outline\n",
    "\n",
    "### [Introduction](intro-slides/intro-slides.html) [5 minutes]\n",
    "\n",
    "- Description: Introduction of the topic, speakers, and software.\n",
    "\n",
    "### [Intro to SymPy Expressions](notebooks/intro-sympy.ipynb) [30 minutes]\n",
    "\n",
    "- Description: Writing common domain specific mathematical expressions with\n",
    "  SymPy.\n",
    "- Motivating Examples: Long expressions, Matrix operations, and Loop Fusion\n",
    "  from classical mechanics, chemical kinetics, nuclear dynamics, and materials\n",
    "  science.\n",
    "\n",
    "### [Code Printers](notebooks/code-printers.ipynb) [30 minutes]\n",
    "\n",
    "- Description: Printing expressions in multiple languages (C, Fortran, Rust,\n",
    "  Julia, Octave, Javascript, etc)\n",
    "- Motivating Example: 2D interactive plot in a Jupyter notebook by Javascript\n",
    "  injection\n",
    "\n",
    "### The Easy Way: High Level Generation (lambdify, ufuncify) [1 hour]\n",
    "\n",
    "- Description: Generate loop fused NumPy ufuncs and compare to automatically\n",
    "  generated NumPy code. Show how you can extend lambdify with custom Python\n",
    "  functions.\n",
    "- Motivating Example: Generate a Jacobian function for a chemical kinetic\n",
    "  problem.\n",
    " \n",
    "- [22-lambdify.ipynb](notebooks/22-lambdify.ipynb)\n",
    "- [23-ordinary-differential-equations.ipynb](notebooks/23-ordinary-differential-equations.ipynb)\n",
    "- [25-chemical-kinetics-intro.ipynb](notebooks/25-chemical-kinetics-intro.ipynb)\n",
    "- [32-chemical-kinetics-symbolic-construction.ipynb](notebooks/32-chemical-kinetics-symbolic-construction.ipynb)\n",
    "\n",
    "### [The Harder Way: C Code Generation, Custom Printers, and CSE](notebooks/the_harder_way.ipynb) [1 hour]\n",
    "\n",
    "**Description**\n",
    "\n",
    "Convert SymPy matrices to a C program and speed up numerical execution by extending the printer with common subexpression elimination.\n",
    "\n",
    "**Motivating Example**\n",
    "\n",
    "Evaluate a chemical kinetic ordinary differential equation and its Jacobian.\n",
    "\n",
    "#### Section 1\n",
    "- [5 min] Introduction\n",
    "- [5 min] **Exercise:** Load the ODE expression, compute the Jacobian, and inspect the results.\n",
    "- [5 min] Printing C Code\n",
    "- [5 min] **Exercise:** Generate C code for the Jacobian expression.\n",
    "\n",
    "#### Section 2\n",
    "- [5 min] Extending and Customizing Printers\n",
    "- [10 min] **Exercise:** Construct custom exponential printing and symbol names.\n",
    "\n",
    "#### Section 3\n",
    "- [5 min] Common Sub-expression Elimination\n",
    "- [15 min] **Exercise:** Generate C code (w/ CSE) for the ODE and Jacobian expressions.\n",
    "- [5 min] **Bonus:** Compile and execute the C code.\n",
    "\n",
    "### [Cythonizing Your Code (manually and autowrap)](notebooks/the-easy-hard-way.ipynb) [1 hour]\n",
    "\n",
    "**Description**\n",
    "\n",
    "Generate C code to evaluate an ordinary differential equation and and its Jacobian and wrap it for use with SciPy’s integrators. Additionally, demonastrate how one might access external C libraries.\n",
    "\n",
    "**Motivating Example**\n",
    "\n",
    "Chemical kinetic ODE.\n",
    " \n",
    "#### Section 1\n",
    "\n",
    "- [5 minutes] Explanation of Cython\n",
    "- [5 minutes] Example: writing Cython and compiling in a notebook\n",
    "- [5 minutes] Demonstrate Cython writing exercise notebook\n",
    "\n",
    "#### Section 2\n",
    "\n",
    "- [5 minutes] Example: writing a Cython wrapper around existing C code\n",
    "- [10 minutes] Demonstrate codegen generation of ODE and Jacobian functions\n",
    "- [10 minutes] **Exercise**: wrap the generated C code with Cython\n",
    "\n",
    "#### Section 3\n",
    "\n",
    "- [5 minutes] Demonstrate autowrap codegen + compilation\n",
    "- [5 minutes] Explanation of custom printer and compile flags with autowrap\n",
    "- [10 minutes] **Exercise**: use autowrap or codegen to evaluate an expression with exponentials\n",
    "    \n",
    "- [40-chemical-kinetics-cython.ipynb](notebooks/40-chemical-kinetics-cython.ipynb)\n",
    "- [45-chemical-kinetics-cython-vode.ipynb](notebooks/45-chemical-kinetics-cython-vode.ipynb)\n",
    "\n",
    "----\n",
    "\n",
    "The attendees will come away with a powerful set of tools that will allow them\n",<commit_msg>Autosave.<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatic Code Generation with SymPy\n",
    "\n",
    "This tutorial will introduce code generation concepts using the [SymPy](http://sympy.org) library. SymPy is a pure Python library for symbolic mathematics. Code generation refers to the act of converting a SymPy symbolic expression into equivalent code in some language, typically for numeric evaluation. This allows one to use SymPy to symbolically model a problem and generate fast numerical code for specific platforms that executes 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 mathematics 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.\n",
    "\n",
    "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.\n",
    "\n",
    "## Instructors\n",
    "\n",
    "- <NAME>, KTH Royal Institute of Technology\n",
    "- <NAME>, University of California, Davis\n",
    "- <NAME>, University of South Carolina\n",
    "- <NAME>, University of California, Davis\n",
    "\n",
    "## Learning objectives\n",
    "\n",
    "Attendees will be able to:\n",
    "- write SymPy expressions describing mathematical functions and identify the\n",
    "  function arguments and outputs,\n",
    "- use the SymPy code printers to transform SymPy expressions representing\n",
    "  common domain specific functions into multiple output languages,\n",
    "- use the SymPy code generation routines to output compilable C code and use\n",
    "  Cython to access these functions in Python,\n",
    "- generate custom vectorized functions with the three SymPy functions:\n",
    "  lambdify, ufuncify, and autowrap,\n",
    "- create both custom code printers that make use of specialized C libraries and\n",
    "  common subexpression elimination (CSE),\n",
    "- subclass the core SymPy printers and create a printer for a custom language.\n",
    "\n",
    "## Outline\n",
    "\n",
    "### [Introduction](intro-slides/intro-slides.html) [5 minutes]\n",
    "\n",
    "- Description: Introduction of the topic, speakers, and software.\n",
    "\n",
    "### [Intro to SymPy Expressions](notebooks/intro-sympy.ipynb) [30 minutes]\n",
    "\n",
    "- Description: Writing common domain specific mathematical expressions with\n",
    "  SymPy.\n",
    "- Motivating Examples: Long expressions, Matrix operations, and Loop Fusion\n",
    "  from classical mechanics, chemical kinetics, nuclear dynamics, and materials\n",
    "  science.\n",
    "\n",
    "### [Code Printers](notebooks/code-printers.ipynb) [30 minutes]\n",
    "\n",
    "- Description: Printing expressions in multiple languages (C, Fortran, Rust,\n",
    "  Julia, Octave, Javascript, etc)\n",
    "- Motivating Example: 2D interactive plot in a Jupyter notebook by Javascript\n",
    "  injection\n",
    "\n",
    "### The Easy Way: High Level Generation (lambdify, ufuncify) [1 hour]\n",
    "\n",
    "- Description: Generate loop fused NumPy ufuncs and compare to automatically\n",
    "  generated NumPy code. Show how you can extend lambdify with custom Python\n",
    "  functions.\n",
    "- Motivating Example: Generate a Jacobian function for a chemical kinetic\n",
    "  problem.\n",
    " \n",
    "- [22-lambdify.ipynb](notebooks/22-lambdify.ipynb)\n",
    "- [23-ordinary-differential-equations.ipynb](notebooks/23-ordinary-differential-equations.ipynb)\n",
    "- [25-chemical-kinetics-intro.ipynb](notebooks/25-chemical-kinetics-intro.ipynb)\n",
    "- [32-chemical-kinetics-symbolic-construction.ipynb](notebooks/32-chemical-kinetics-symbolic-construction.ipynb)\n",
    "\n",
    "### [The Harder Way: C Code Generation, Custom Printers, and CSE](notebooks/the_harder_way.ipynb) [1 hour]\n",
    "\n",
    "**Description**\n",
    "\n",
    "Convert SymPy matrices to a C program and speed up numerical execution by extending the printer with common subexpression elimination.\n",
    "\n",
    "**Motivating Example**\n",
    "\n",
    "Evaluate a chemical kinetic ordinary differential equation and its Jacobian.\n",
    "\n",
    "#### Section 1\n",
    "- [5 min] Introduction\n",
    "- [5 min] **Exercise:** Load the ODE expression, compute the Jacobian, and inspect the results.\n",
    "- [5 min] Printing C Code\n",
    "- [5 min] **Exercise:** Generate C code for the Jacobian expression.\n",
    "\n",
    "#### Section 2\n",
    "- [5 min] Extending and Customizing Printers\n",
    "- [10 min] **Exercise:** Construct custom exponential printing and symbol names.\n",
    "\n",
    "#### Section 3\n",
    "- [5 min] Common Sub-expression Elimination\n",
    "- [15 min] **Exercise:** Generate C code (w/ CSE) for the ODE and Jacobian expressions.\n",
    "- [5 min] **Bonus:** Compile and execute the C code.\n",
    "\n",
    "### [Cythonizing Your Code (manually and autowrap)](notebooks/the-easy-hard-way.ipynb) [1 hour]\n",
    "\n",
    "**Description**\n",
    "\n",
    "Generate C code to evaluate an ordinary differential equation and and its Jacobian and wrap it for use with SciPy’s integrators. Additionally, demonastrate how one might access external C libraries.\n",
    "\n",
    "**Motivating Example**\n",
    "\n",
    "Chemical kinetic ODE.\n",
    " \n",
    "#### Section 1\n",
    "\n",
    "- [5 minutes] Explanation of Cython\n",
    "- [5 minutes] Example: writing Cython and compiling in a notebook\n",
    "- [5 minutes] Demonstrate Cython writing exercise notebook\n",
    "\n",
    "#### Section 2\n",
    "\n",
    "- [5 minutes] Example: writing a Cython wrapper around existing C code\n",
    "- [10 minutes] Demonstrate codegen generation of ODE and Jacobian functions\n",
    "- [10 minutes] **Exercise**: wrap the generated C code with Cython\n",
    "\n",
    "#### Section 3\n",
    "\n",
    "- [5 minutes] Demonstrate autowrap codegen + compilation\n",
    "- [5 minutes] Explanation of custom printer and compile flags with autowrap\n",
    "- [10 minutes] **Exercise**: use autowrap or codegen to evaluate an expression with exponentials\n",
    "\n",
    "----\n",
    "\n",
    "The attendees will come away with a powerful set of tools that will allow them\n",
{'fraction_non_alphanumeric': 0.13473883863437408, 'fraction_numerical': 0.008462211847096586, 'mean_word_length': 3.3300694883133293, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1395579', 'n_tokens_mistral': 4190, 'n_tokens_neox': 3997, 'n_words': 1492}