Skip to content

Instantly share code, notes, and snippets.

@chvandorp
Created January 3, 2021 21:14
autodiff example 1
#include "dualmath.hpp"
#include <cmath>
#include <iostream>
template<class T>
T fun(T x) {
return sin(x) / cos(x); // tan(x)
}
int main() {
for ( double x = 0; x < 10; x += 0.01 ) {
// promote x to dual
Dual xe(x, 1.0); // set y = 1
// the derivative of tan(x) is 1/cos^2(x)
std::cout << x << " " << fun(xe).y << " " << 1/(cos(x) * cos(x)) << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment