Goal: Evaluate a tape with forward AD mode.
Prerequisite: Tutorial 2 - Reverse mode AD
Function:
Real func(const Real& x) {
return x * x * x;
}
Full code:
#include <codi.hpp>
#include <iostream>
Real func(const Real& x) {
return x * x * x;
}
int main(int nargs, char** args) {
Real x = 4.0;
tape.setActive();
tape.registerInput(x);
Real y = func(x);
tape.registerOutput(y);
tape.setPassive();
tape.evaluateForward();
std::cout << "f(4.0) = " << y << std::endl;
std::cout <<
"df/dx(4.0) = " << y.
getGradient() << std::endl;
tape.reset();
return 0;
}
Forward mode tape evaluation works the same as a reverse mode tape evaluation. Only the inputs need to be seeded and the derivatives are obtained from the outputs.