CodePlexProject Hosting for Open Source Software

Variable x = new Variable(); Variable y = new Variable(); Function f = x * y; Point p = new Point(new VariableAssignment(x, 2.0), new VariableAssignment(y, 3.0)); double x = f.Value(p); // Value is 6

Longer function expressions can be constructed from more primitive ones using operator overloads and static methods defined by the abstract Function class. The static methods are used like those in the System.Math class, except they return another function (which derives from Function) instead of a double. To compute the exponential, we may write:

Function g = Function.Exp(f); double x = g.Value(p); // Value is 403.43

Another operator being defined by Function is the evaluation operator |. When this binary operator is used with a Variable and a double, a VariableAssignment object is created and returned. This way we may define and use the point above simply by writing:

```
Point p = new Point(x | 2.0, y | 3.0);
f.Value(p);
```

Or even simpler, using another overload of the Value method:

f.Value(x | 2.0, y | 3.0);

All primitives in FuncLib are immutable, so they can’t be modified once created. An instance of Point can safely be passed to other methods without having to worry about if the object is modified.

Function g1 = g.Derivative(x);

We may continue and compute the partial derivative of this new function with respect to y by writing:

Function g2 = g1.Derivative(y);

Of course we can write this in one single statement as

Function g2 = g.Derivative(x).Derivative(y);

Or, using another overload of the Derivative method, we may use the short-hand notation:

Function g2 = g.Derivative(x, y);

Similarly, we can compute second order partial derivative with respect to x twice using:

g.Derivative(x, 2);

Where’s no intrinsic limitation of the order of differentiation or the number of variables being used. The library may even be smart and determine that some of the Function objects can be reused if the same mathematical expression occurs. The canonical example of this is the exponential, since the derivative of the exponential is just the exponential itself. Hence the computation of the 10000th order derivative creates just one single object:

Function f = Function.Exp(x); f.Derivative(x, 10000);

Now f doesn't depend on x anymore, so taking the derivative wi

Point p = new Point(x | 0.5);

The preferred method in FuncLib since it's easier to represent in a object-oriented structure. The FuncLib primitives Variable and Function are reverse-mode objects. It's also possible to compile this structure to very fast IL code.

To overcome some limitations with the reverse-mode implementation, support for forward-mode computations is also provided with the DualNumber, DualMatrix, and DualVector classes.

See: Performance considerations

Last edited Aug 24, 2011 at 7:04 PM by bakkedal, version 2