Are there any open-source auto-differentiation libraries for MATLAB?

I am aware of commercial packages such as Tomlab/MAD and plenty of C++ libraries, but I can't find many more for MATLAB other than Automatic Differentiation for Matlab package on Matlab File Exchange which isn't a major package.


As a bare minimum of functionality, I need to be able 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'
  • 2
    $\begingroup$ When I last used AdMat some years ago it was quite capable, but it doesn't satisfy your requirement of being open source. $\endgroup$ – 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:

% Load ad based module
mrstModule add ad-fi

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

z = sum(x.*x);
disp 'Jacobian:'

% MRST AD also supports multiple vector valued functions
a = ones(5, 1);
b = ones(5, 1);
[a, b] = initVariablesADI(a, b);
c = a.*b + 2.*b;

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.

  • $\begingroup$ Thanks, sounds great. I will take a look. Are there features for sparsity patterns, etc.? $\endgroup$ – jlperla Jul 21 '14 at 17:45
  • $\begingroup$ 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. $\endgroup$ – moyner Jul 21 '14 at 17:48
  • $\begingroup$ 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. $\endgroup$ – jlperla Jul 22 '14 at 22:38

This article in SIAM Review describes how to implement operator-overloading automatic differentiation in MATLAB, and gives a good introduction to the technology.

  • $\begingroup$ Thank you, let me take a look. For completeness, it looks like the files are located in: academics.davidson.edu/math/neidinger/IntroAD&OOP.zip $\endgroup$ – jlperla Jul 19 '14 at 23:45
  • 1
    $\begingroup$ This is a great explanation of AD and matlab, but doesn't support vector based variables as far as I can tell. I would have a (runtime determined) vector of variables, so individually defining each wouldn't work. See my adjusted test case in the description. $\endgroup$ – jlperla Jul 20 '14 at 0:15

I realize this is an old question, but when looking for this myself today I found ADiGator, which is open source, and seems to handle vectors. I haven't tested it yet myself, but it seems to be actively developed.

  • $\begingroup$ Thanks @Petter T, I really appreciate it. After trying a bunch, I ended up deciding that operator overloading based ones were just too slow in matlab. Class access is just too slow compared to generic programming and no overhead abstractions in C++. Of the source code control ones, my solution was ADImat, but I am not sure how supported it is. Looking forward to trying out ADiGator. $\endgroup$ – jlperla Aug 21 '15 at 16:49

If you are really interested in open source and like to promote/support it, then you should probably take a look at the open source matlab clone octave. There is an extension to it, called ad that supports automatic forward differentiation. Unfortunately, it seems to be unmaintained currently.


The wikipedia page for automatic differentiation has links to many software packages or libraries, including several for MATLAB. http://en.wikipedia.org/wiki/Automatic_differentiation#Software

  • 1
    $\begingroup$ Thanks. Yeah, I have looked at them and it is pretty out of date. most seem to be proprietary or just something someone threw together for an older version of matlab without core functionality. I was hoping there was a major implementation, but I am starting to doubt it exists. $\endgroup$ – jlperla Jul 21 '14 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.