This repository contains a more or less random collection of ideas that demonstrate applications of the differentiability built into the Swift programming language. Running the examples requires trunk development snapshot of the Swift compiler. Release versions of the compiler don't contain the Differentiation
module.
Critters learn to walk - Animals
Currently (Nov 2020) extremely slow. See https://bugs.swift.org/browse/SR-14113 for more details.
Back-propagate backward through time and optimize the distance the animal achieved at the end of the simulation.
Inspired by the mass_spring.py
example used in the DiffTaichi paper.
Long stick and some balls - Billiard
Currently (Feb 2021) impossible to complete due to a bug that crashes the Swift compiler when inline closures are used. See https://bugs.swift.org/browse/SR-12992 for more details.
Oh no! The balls aren’t reaching the targets.
Source code and the idea is adapted from Differentiable Swift presentation and notebook.
A differentiable ODE integrator for sensitivity analysis - RK4
Currently (Feb 2021) impossible to complete due to a bug that crashes the Swift compiler when inline closures are used. See https://bugs.swift.org/browse/SR-12992 for more details.
A related topic is finding derivatives of functions that are defined by differential equations. We typically use a numerical integrator to find solutions to these functions. Those leave us with numeric solutions which we then have to use to approximate derivatives. What if the integrator itself was differentiable? It is after all, just a program, and automatic differentiation should be able to tell us the derivatives of functions that use them.
Source code and the idea is adapted from this blog post published by the Kitchin group at CMU.
Derivative of an integral function - Trapz
The idea is simple, we define a function in Python as usual, and in the function body calculate the integral in a program. Then we use autograd to get the derivative of the function.
Source code and the idea is adapted from this blog post published by the Kitchin group at CMU.
Wave - Shallow water PDE solver - Wave
Currently (Nov 2020) extremely slow. See https://bugs.swift.org/browse/SR-14113 for more details.
Back-propagate backward through time and optimize the initial water surface height in order to achieve a particular target pattern at the end of the simulation.
Inspired by the wave.py
example used in the DiffTaichi paper.
Bounce - Some balls attached to springs - Bounce
We demonstrate the language using a mass-spring simulator, with three springs and three mass points, as shown right.
Inspired by an example used in the DiffTaichi paper.
Introduction to differentiable Swift - Opening
Build from the Foundation up!
Rudiments of automatic differentiation - Basics
Auto-differentiation, or simply autodiff, is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. AD exploits the fact that every computer program, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.)
Inspired by the Automatic differentiation Wikipedia page.
Licensed under the MIT License.