c++ - 复变量函数的自动微分

标签 c++ boost

我想知道是否可以应用bo​​ost的自动微分库:

#include <boost/math/differentiation/autodiff.hpp>

返回std::complex<double>的函数值(value)观?


例如,考虑多元复值函数:

 #include <complex>

 std::complex<double> complex_function(double a, double c){
     // Assuming a < 0
     return exp(sqrt(std::complex(a, 0.0))) + sin(c);
 }

我怎样才能将导数带到ac使用 Boost 的 autodiff ?这可能吗?

最佳答案

is [it] possible to apply boost's automatic differentiation library to functions which return std::complex<double> values?

目前没有。

一个版本可能看起来像这样:

// THIS DOES NOT COMPILE - FOR DISCUSSION ONLY
#include <boost/math/differentiation/autodiff.hpp>

#include <iostream>
#include <complex>

namespace ad = boost::math::differentiation;

template <typename T0, typename T1>
auto complex_function(T0 a, T1 c){
  // Assuming a < 0
  return exp(sqrt(complex(a, 0.0))) + sin(c);  // DOES NOT COMPILE
}

int main() {
  auto const a = ad::make_fvar<double, 2>(-3);
  auto const c = 0.0;
  auto const answer = complex_function(a, c);
  return 0;
}

这需要 complex将针对 autodiff::fvar 进行定义模板类型,类似于其他数学函数( expsqrt 等)如何在 autodiff 库中重载,并通过 ADL 调用。

正如@user14717 在评论中指出的那样,这是 vector 值自动微分的一个特例,因为返回值不是单个截断的泰勒多项式,而是它们的元组。

关于c++ - 复变量函数的自动微分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64492633/

相关文章:

c++ - OpenGL:QApplication的执行流程

c++ - 将 const char * 转换为 std::string

c++ - 从十六进制转换为 LPCVOID 切断了地址的一半

c++ - 使用 boost::thread 发生线程锁。我的条件变量有什么问题?

c++ - 无法编译最简单的 Phoenix lambda

c++ - 填充 boost vector 或矩阵

c++ - 使用 boost::thread 时出现 Visual Studio 2010 链接器错误

c++ - 类型检查中的纯名称等效

c++ - 为什么这在分成 2 行时不起作用?

c++ - 变体与继承