Goal: Demonstation of the compile time access interface of the DerivativeAccess helper.
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() {
{
t2s aFor = 2.0;
DH::setAllDerivatives<1>(aFor, 1.0);
t2s cFor = func(aFor);
std::cout << "t0s: " << DH::derivative<0, 0>(cFor) << std::endl;
std::cout << "t1_1s: " << DH::derivative<1, 0>(cFor) << std::endl;
std::cout << "t1_2s: " << DH::derivative<1, 1>(cFor) << std::endl;
std::cout << "t2s: " << DH::derivative<2, 0>(cFor) << std::endl;
}
{
t6s aFor = 2.0;
DH::setAllDerivatives<1>(aFor, 1.0);
t6s cFor = func(aFor);
std::cout << "t0s: " << cFor << std::endl;
std::cout << "t6s: " << DH::derivative<6, 0>(cFor) << std::endl;
}
{
r6s aRev = 2.0;
DH::setAllDerivativesForward<1>(aRev, 1.0);
r6s cRev = func(aRev);
DH::setAllDerivativesReverse<1>(cRev, 1.0);
std::cout << "r0s: " << cRev << std::endl;
std::cout << "r6s: " << DH::derivative<6, 0>(aRev) << std::endl;
}
{
t2v aFor = 2.0;
DH::derivative<1, 0>(aFor) = {1.0, 2.0};
DH::derivative<1, 1>(aFor) = 1.0;
t2v cFor = func(aFor);
std::cout << "t0v: " << DH::derivative<0, 0>(cFor) << std::endl;
std::cout << "t1_1v: " << DH::derivative<1, 0>(cFor) << std::endl;
std::cout << "t1_2v: " << DH::derivative<1, 1>(cFor) << std::endl;
std::cout << "t2v: " << DH::derivative<2, 0>(cFor) << std::endl;
}
return 0;
}