/ad_example1.cpp Secret
Created
January 3, 2021 21:14
autodiff example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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