AutomaticDifferentiation/tests/compile_time_derivative/main.cpp

39 lines
837 B
C++
Executable file

#include <iostream>
#include "utils.hpp"
#include "compile_time_derivative.hpp"
using std::cout;
using std::endl;
// x is the unknown
expression<terminal<unknown>::type> const x;
// A transform works as a functor
Derivative const derivative;
template<typename T>
T fct(const T & x) {
return (3)*x*x + (2)*x - (3) + (1)/x;
}
template<typename T>
T dfct(const T & x) {
return (6)*x + (2) - (1)/(x*x);
}
// The file must be compiled with -O0 because with -O2 and up
int main()
{
auto func = (3*x*x + 2*x - 3 + 1./x);
// auto func = fct(x);
auto dfunc = derivative(func);
cout.precision(16);
for(double i = 0.1 ; i < 2. ; i+=.1)
std::cout << func(i) << "\t" << fct(i) << '\n';
for(double i = 0.1 ; i < 2. ; i+=.1)
std::cout << func(i) << "\t" << fct(i) << "\t" << dfunc(i) << "\t" << dfct(i) << '\n';
return 0;
}