add test for autodiff about forwarding func

This commit is contained in:
2024-04-24 15:22:10 +08:00
commit b51b72a7b5
20 changed files with 732 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// NOTE: 使用自定义标量(复数)的单变量函数的导数
// C++ includes
#include <iostream>
#include <complex>
using namespace std;
// autodiff include
#include <autodiff/forward/dual.hpp>
using namespace autodiff;
// Specialize isArithmetic for complex to make it compatible with dual
namespace autodiff::detail {
template<typename T>
struct ArithmeticTraits<complex<T>>: ArithmeticTraits<T> {};
} // autodiff::detail
using cxdual = Dual<complex<double>, complex<double>>;
cxdual f(cxdual x){
return 1+x+x*x+1/x+log(x);
}
int main(){
cxdual x = 2.0; // the input variable x
cxdual u = f(x); // the output variable u
cxdual dudx = derivative(f, wrt(x), at(x)); // evaluate the derivative du/dx
cout << "u = " << u << endl;
cout << "du/dx = " << dudx << endl;
return 0;
}