CodePlexProject Hosting for Open Source Software

Let us start by downloading the library. Please go to Releases and download the latest release of the library. Unzip the files and put them in your favorite folder. Then open Visual Studio 2010, start a new console project, and add the reference to AutoDiff.dll in the folder where you unzipped the downloaded files. Open the program.cs file and add using AutoDiff; to the list of using statements. Your code should look like this:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using AutoDiff; namespace MyFirstAutodiffProgram { class Program { static void Main(string[] args) { } } }

The main purpose of this library is to allow its user to compute gradients. When we create a program that solved a problem using gradients we want to be able to quickly prototype and play with our functions without the need to manually compute the gradient with pen and paper. So let's see how simple it is. In the following example we will compute the gradient of the function:

f(x,y) = (x+y) * log(exp(x) + exp(y))So let us first define our function in code. Add the following lines of code to your Main method:

Variable x = new Variable(); Variable y = new Variable(); Term func = (x + y) * TermBuilder.Log(TermBuilder.Exp(x) + TermBuilder.Exp(y));

Now we can easily compute the value and the gradient at any given point. Let us compute the value and the gradient of this function at

double value = func.Evaluate(new Variable[] { x, y }, new double[] { 2, -1 }); double[] grad = func.Differentiate(new Variable[] { x, y }, new double[] { 2, -1 });

This tutorial demonstrates the most basic usage of the AutoDiff library - defining functions and computing their values and gradients. Here are the key points:

- Objects of type Term are used to define functions analytically
- Evaluate/Differentiate methods can be used to compute the value/gradient of a function given assignment of values to variables
- The order of variables given to Evaluate/Differentiate matters

Last edited Apr 6, 2011 at 1:32 PM by alexshtf, version 2