Automatic Differentiation for JavaScript
A translation of the ad library from Jeff Siskind's Qobischeme.
Some links that explain what AD is:
This package provides
- transformed functions and means to transform others
- sweetjs macros that can be used to effect replacement of primitive functions with above transformed/overloaded ones.
Usage
Install as npm install ad.js
Use sweet to load the macros and compile code
var sweet = ;var adMacros = sweetvar adLoadString = "var ad = require('ad.js')({});\n";var _compiled = sweet;var compiled = adLoadString + _compiledcode
Also, the ad
require statement must be passed options.
To get the default behaviour:
var ad = {}
To run only reverse mode
(faster)
var ad = mode: 'r'
To run only reverse mode
with only first-derivatives (faster even)
var ad = mode: 'r' noHigher: true
Test
Uses nodeunit. To run tests, do npm test
Example
The setup takes a file that looks like so:
var {return x * x * x;}
and transforms it into code that looks like so:
var {return ad;}
after which one can do things like:
var dCube_dx = ad;console// will print 300 => d/dx(x^3) = 3x^2 @x=10 = 300
and even higher-order derivatives as:
var d2Cube_dx2 = ad;console// will print 60 => d2/dx2(x^3) = 6x @x=10 = 60
The bulk of the instructions above are just the means by which sweet gets its hands on the code you want to transform and the macros to transform it with.