Goal: Reevaluate a tape at a different position.
Prerequisite: Tutorial 2 - Reverse mode AD
Function:
Full code:
#include <codi.hpp>
#include <iostream>
return x * x * x;
}
int main(int nargs, char** args) {
tape.setActive();
tape.registerInput(x);
tape.registerOutput(y);
tape.setPassive();
tape.evaluate();
std::cout << "f(4.0) = " << y << std::endl;
std::cout <<
"df/dx(4.0) = " << x.
getGradient() << std::endl;
tape.clearAdjoints();
tape.evaluatePrimal();
tape.evaluate();
std::cout <<
"f(3.0) = " << tape.getPrimal(y.
getIdentifier()) << std::endl;
std::cout <<
"df/dx(3.0) = " << x.
getGradient() << std::endl;
tape.reset();
return 0;
}
RealReversePrimalGen< double > RealReversePrimal
Definition codi.hpp:178
A primal reevaluation follows the same rules as an evaluation of the tape. There are a few things to consider:
- A primal value taping approach needs to be used, e.g. codi::RealReversePrimal
- The primal value needs to be changed on the tape with setPrimal.
- New primal values of the outputs have to be received with getPrimal.
- CoDiPack does not record branching statements. These changes are not considered in a primal reevaluation.