Goal: Demonstation how to set derivatives of higher order types directly.
Prerequisite: Tutorial 6 - Higher order derivatives
Function: Simple real valued function for higher order derivatives
template<typename T>
T func(const T& x) {
T t = x * x * x * x * x * x * x;
return t * 3.0;
}
Full code:
template<typename T>
T func(const T& x) {
T t = x * x * x * x * x * x * x;
return t * 3.0;
}
int main() {
{
aFor.
value().gradient() = 1.0;
std::cout <<
"t0s: " << cFor.
value().value() << std::endl;
std::cout <<
"t1_1s: " << cFor.
value().gradient() << std::endl;
std::cout <<
"t1_2s: " << cFor.
gradient().value() << std::endl;
std::cout <<
"t2s: " << cFor.
gradient().gradient() << std::endl;
}
{
aFor.
value().value().value().value().value().gradient() = 1.0;
aFor.
value().value().value().value().gradient().value() = 1.0;
aFor.
value().value().value().gradient().value().value() = 1.0;
aFor.
value().value().gradient().value().value().value() = 1.0;
aFor.
value().gradient().value().value().value().value() = 1.0;
aFor.
gradient().value().value().value().value().value() = 1.0;
std::cout << "t0s: " << cFor << std::endl;
std::cout <<
"t6s: " << cFor.
gradient().gradient().gradient().gradient().gradient().gradient() << std::endl;
}
{
aRev.
value().value().value().value().value().gradient() = 1.0;
aRev.
value().value().value().value().gradient().value() = 1.0;
aRev.
value().value().value().gradient().value().value() = 1.0;
aRev.
value().value().gradient().value().value().value() = 1.0;
aRev.
value().gradient().value().value().value().value() = 1.0;
tape.setActive();
tape.registerInput(aRev);
tape.registerOutput(cRev);
cRev.
gradient().value().value().value().value().value() = 1.0;
tape.setPassive();
tape.evaluate();
std::cout << "r0s: " << cRev << std::endl;
std::cout <<
"r6s: " << aRev.
gradient().gradient().gradient().gradient().gradient().gradient() << std::endl;
tape.reset();
}
return 0;
}