我想知道是否可以应用boost的自动区分库:
#include <boost/math/differentiation/autodiff.hpp>返回std::complex<double>值的函数?
例如,,考虑多元复值函数:
#include <complex>
std::complex<double> complex_function(double a, double c){
// Assuming a < 0
return exp(sqrt(std::complex(a, 0.0))) + sin(c);
}如何使用Boost的a将导数wrt转换到c或autodiff?这可能吗?
发布于 2020-10-23 14:58:56
是否可以将boost的自动微分库应用于返回
std::complex<double>值的函数?
现在不行。
这样的版本看起来可能是这样的:
// 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,类似于其他数学函数(exp、sqrt等)。在autodiff库中重载,并通过ADL调用。
正如@ them 14717在注释中指出的那样,它是向量值自差的特例,因为返回值不是单个截断的Taylor多项式,而是它们的一个元组。
https://stackoverflow.com/questions/64492633
复制相似问题