ForwardModeAD

ForwardModeAD is a lightweight library for forward-mode automatic differentiation using dual numbers and functions overloading. It can compute the derivative, gradient and jacobian of any function, as long as it is written as a combination of overloaded functions.

As a showcase, in a few lines we can implement the Newton method for root finding.

use ForwardModeAD;

proc f(x) {
   return exp(-x) * sin(x) - log(x);
}

var tol = 1e-6, // tolerance to find the root
   cnt = 0, // to count number of iterations
   x0 = initdual(0.5), // initial guess
   valder = f(x0); // initial function value and derivative

while abs(value(valder)) > tol {
   x0 -= value(valder) / derivative(valder);
   valder = f(x0);
   cnt += 1;
   writeln("Iteration ", cnt, " x = ", value(x0), " residual = ", value(valder));
}

To get started with the package, check out the Quickstart Tutorial. If you want to learn about solving real-world problems, check out the Applications section. To read about the theory behind the package, check the Background section. A detailed reference of the functionalities of the package can be found in the API docs section.

Installation

If you are writing you application with Mason, all you have to do is run

mason add ForwardModeAD

to add the library as dependency.

To use the library you will need to import it with

use ForwardModeAD;

and you are ready to go.

Contributing

If you encounter bugs or have feature requests, feel free to open an issue. Pull requests are also welcome. More details in the contributing guidelines.

License

MIT (c) Luca Ferranti