lucaferranti / ForwardModeAD

forward mode automatic differentiation using dual numbers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ForwardModeAD

license: MITdocs-devlifecycle

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));
}

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.

Documentation

  • latest : documentation of the latest version on main

Contributing

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

License

MIT (c) Luca Ferranti

About

forward mode automatic differentiation using dual numbers

License:MIT License


Languages

Language:Chapel 100.0%