update config
This commit is contained in:
parent
b51b72a7b5
commit
a98692e5a3
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
}
|
|
@ -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}")
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
@ -89,7 +89,7 @@ int main()
|
|||
|
||||
// Output:
|
||||
// C0 = 7
|
||||
// Cx(computed) = 5
|
||||
// Cx(expected) = 5
|
||||
// Cy(computed) = 9
|
||||
// Cy(expected) = 9
|
||||
// Cx(computed) = 9
|
||||
// Cx(expected) = 9
|
||||
// Cy(computed) = 5
|
||||
// Cy(expected) = 5
|
|
@ -0,0 +1,41 @@
|
|||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <autodiff/forward/dual.hpp>
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue