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));
}
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.
- latest : documentation of the latest version on main
If you encounter bugs or have feature requests, feel free to open an issue. Pull requests are also welcome. More details in the contribution guidelines
MIT (c) Luca Ferranti