Monthly Archives: December 2013

Automatic Differentiation using Move Constructors

Automatic differentiation is a great tool for various scientific and financial computing needs. The best approach, I think, is to use operator overloading so that the resulting automatic differentiation class can be a drop-in replacement for common variable types such as double. This approach does make it harder to create the class though, as one has to be careful to avoid temporaries that can drastically slow down calculations. The traditional way to fight temporaries is to use expression templates, but there are other ways around it. I remember at one place I used to work, the approach was to not allow binary operators and only use addition-assignment (and others) operators. Obviously, that approach makes the code a pain to read, but the resulting code was very fast.

Now that I have access to a c++11 development environment, there is a new alternative: to use move constructors and move assignment operators. So, below is my attempt to embrace it in this context. The class ardv is designed to be a drop-in replacement for the double type. A typical usage pattern might be:

And below is the actual class. Note that I make absolutely no guarantee as to its accuracy.

In a future post, I’ll write about the efficiency of this approach compared to a version using expression templates. In brief though, it is pretty competitive under most circumstances.