From a98692e5a3869d6b477b91de98df155e86a6173a Mon Sep 17 00:00:00 2001 From: Zhuo Yang Date: Thu, 25 Apr 2024 21:22:32 +0800 Subject: [PATCH] update config --- .vscode/settings.json | 3 ++ CMakeLists.txt | 6 +-- ...rialbe_function_rely_on_analytical_der.cpp | 20 ++++----- test/forward/test.cpp | 41 +++++++++++++++++++ 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 test/forward/test.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..70e34ec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 34c7e7c..b826809 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,11 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_BUILD_TYPE Debug) -set(conda_env_path "/share/software/anaconda3/envs/unisolver") +set(autodiff_path "/home/of/code/test-workspace/autodiff_bin") if(DEFINED CMAKE_PREFIX_PATH) - list(APPEND CMAKE_PREFIX_PATH ${conda_path}) + list(APPEND CMAKE_PREFIX_PATH ${autodiff_path}) else() - set(CMAKE_PREFIX_PATH ${conda_env_path}) + set(CMAKE_PREFIX_PATH ${autodiff_path}) endif() message(STATUS "cmake_prefix_path: ${CMAKE_PREFIX_PATH}") diff --git a/test/forward/5multi_varialbe_function_rely_on_analytical_der.cpp b/test/forward/5multi_varialbe_function_rely_on_analytical_der.cpp index 2b16d81..0c75d12 100644 --- a/test/forward/5multi_varialbe_function_rely_on_analytical_der.cpp +++ b/test/forward/5multi_varialbe_function_rely_on_analytical_der.cpp @@ -9,24 +9,24 @@ using namespace autodiff; // Define functions A, Ax, Ay using double; analytical derivatives are available. double A(double x, double y) { return x*y; } -double Ax(double x, double y) { return x; } -double Ay(double x, double y) { return y; } +double Ax(double x, double y) { return y; } // \partial A / \partial x = y +double Ay(double x, double y) { return x; } // \partial A / \partial y = x // Define functions B, Bx, By using double; analytical derivatives are available. double B(double x, double y) { return x + y; } double Bx(double x, double y) { return 1.0; } double By(double x, double y) { return 1.0; } -// Wrap A into Adual function so that it can be used within autodiff-enabled codes. // A是一个double相关的函数,无法与autodiff混合使用;这里将A包装成Adual +// Wrap A into Adual function so that it can be used within autodiff-enabled codes. dual Adual(dual const& x, dual const& y) { dual res = A(x.val, y.val); - if(x.grad != 0.0) + if(x.grad != 0.0) // x导数存在,需要对x链式求导 res.grad += x.grad * Ax(x.val, y.val); - if(y.grad != 0.0) + if(y.grad != 0.0) // y导数存在,需要对y链式求导 res.grad += y.grad * Ay(x.val, y.val); return res; @@ -75,7 +75,7 @@ int main() // Compute expected analytical derivatives of C with respect to x and y auto x0 = x.val; auto y0 = y.val; - auto expectedCx = 2.0*A(x0, y0)*Ax(x0, y0) + Bx(x0, y0); + auto expectedCx = 2.0*A(x0, y0)*Ax(x0, y0) + Bx(x0, y0); auto expectedCy = 2.0*A(x0, y0)*Ay(x0, y0) + By(x0, y0); std::cout << "C0 = " << C0 << "\n"; @@ -89,7 +89,7 @@ int main() // Output: // C0 = 7 -// Cx(computed) = 5 -// Cx(expected) = 5 -// Cy(computed) = 9 -// Cy(expected) = 9 \ No newline at end of file +// Cx(computed) = 9 +// Cx(expected) = 9 +// Cy(computed) = 5 +// Cy(expected) = 5 \ No newline at end of file diff --git a/test/forward/test.cpp b/test/forward/test.cpp new file mode 100644 index 0000000..986b491 --- /dev/null +++ b/test/forward/test.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace autodiff; + +dual sqrt1(dual x){ + dual res = 0.0; + res.val = std::sqrt(x.val); + if (x.grad !=0.0){ + std::cout << "x.grad: " << x.grad << std::endl; + res.grad += 0.5/res.val*x.grad; + } + return res; +} +dual test_exp(dual x){ + dual res = 0.0; + std::cout << "x.val: " << x.val << std::endl; + res.val = std::exp(x.val); + std::cout << "res.val: " << res.val << std::endl; + std::cout << "res.grad: " << res.grad << std::endl; + if(x.grad != 0.0) res.grad += x.grad*res.val; + std::cout << "res.val: " << res.val << std::endl; + std::cout << "res.grad: " << res.grad << std::endl; + return res; +} + +dual test_exp0(dual x){ + return 1.0+exp(x); +} + +int main() +{ + dual x = 1.2; + dual u = sqrt1(x); + double dfdx = derivative(sqrt1, wrt(x), at(x)); + double dfdx2 = derivative(test_exp, wrt(x), at(x)); + double dfdx3 = derivative(test_exp0, wrt(x), at(x)); + std::cout << "dfdx: " << dfdx << std::endl; + std::cout << "dfdx2: " << dfdx2 << std::endl; + std::cout << "dfdx3: " << dfdx3 << std::endl; +} \ No newline at end of file