# Open source auto-differentiation for MATLAB?

Are there any open-source auto-differentiation libraries for MATLAB? I am aware of commerical packages such as http://tomopt.com/tomlab/products/mad/ and plenty of C++ libraries, but I can't find much more MATLAB other than http://www.mathworks.com/matlabcentral/fileexchange/15235-automatic-differentiation-for-matlab which isn't a major package.

As a bare minimum of functionality, I need to be able it to handle vectors as variables. In that MathWorks package I reference, for example, here is a test case:

val = [1 2 3]; %Some value
x = myAD(val); %Creates a vector with that value.

x .* [4 5 6]; %This should give the derivatives [4; 5; 6]
x_inner = sum(x.* x);% To get the inner product.  The derivative here is [2 4 6] here
%Ideally, the above would simply be x_inner = x * x'

• When I last used AdMat some years ago it was quite capable, but it doesn't satisfy your requirement of being open source. – Brian Borchers Jul 20 '14 at 3:52

The SINTEF Matlab Reservoir Simulation Toolbox includes a GPL-licensed AD library. The usage is mostly geared towards numerical applications in subsurface flow, but the library itself is usable for more general purposes.

Here is a basic runthrough of your example as you would run it from the base directory of MRST:

startup;

val = [1; 2; 3];
y = x.*[4; 5; 6];
disp 'Jacobian:'
full(y.jac{1})

z = sum(x.*x);
disp 'Jacobian:'
full(z.jac{1})
Jacobian:

% MRST AD also supports multiple vector valued functions
a = ones(5, 1);
b = ones(5, 1);
c = a.*b + 2.*b;
full(c.jac{1})
full(c.jac{2})


There exists a work-in-progress user guide at the website, where chapter 7 has some usage of the AD library. This library is primarily geared towards problems that require all Jacobian values and is vectorized using forward mode only. It does include some nice things though, like 2d table interpolation and so on. It has been used for moderate size non-linear problems (order of 500,000 unknowns).

The non-linear solvers and components are heavily focused on reservoir applications at the moment. This part of the library is presently being rewritten and may in the future have some use outside of this specific domain. However, if you just want the AD part, the ADI class and the initVariablesADI function should be sufficient.

Disclaimer: I am one of the developers attached to this research group.

• Thanks, sounds great. I will take a look. Are there features for sparsity patterns, etc.? – jlperla Jul 21 '14 at 17:45
• Presently all Jacobians are stored as cell arrays of sparse matrices (for groupings of variables). We did investigate having subclasses/treatment for some specific sparsity patterns, but as our linear systems are mostly from unstructured grids the overhead was too large to gain any benefit. – moyner Jul 21 '14 at 17:48
• Thanks. This seems like a good library. It doesn't appear to have support for matrix-vector multiplications, which would be nice based on my current structure, but I may be able to do it with loops. – jlperla Jul 22 '14 at 22:38