通过boost :: math使用Gauss-Kronrod正交积分求复杂函数

时间:2019-05-14 19:09:51

标签: c++ boost numerical-integration

我正在尝试使用Boost提供的Gauss-Kronrod正交在C ++中以数字形式将具有实参和复杂返回值的函数集成到域[0,5]上的实参和复杂返回值,例如f(x)= 1 + i。

我已使用boost解决有效的实际被积数,因此该库已正确安装。 documentation说:“ Gauss-Kronrod正交支持在实线上定义的整数并返回复数值。”

#include <iostream>
#include <cmath>
#include <complex>
#include <boost/math/quadrature/gauss_kronrod.hpp>

using namespace boost::math::quadrature;
using complex = std::complex<double>;


complex f(double t){
    return complex{1, 1};
};


int main() {
    complex error;
    complex a{0};
    complex b{5};
    unsigned int max_depth = 0;
    complex tolerance = 0;
    complex Q = gauss_kronrod<complex, 61>::integrate(f, a, b, max_depth, tolerance, &error);
    std::cout << Q << ", " << error << "\n";
    return 0;
}

我的预期结果是5 + 5i。我收到编译器错误

/usr/include/boost/math/quadrature/gauss_kronrod.hpp:1871:17: error: no match for 'operator<=' (operand types are 'std::complex<double>' and 'std::complex<double>')

表示未为std :: complex定义某些运算符。我会误读文档,并且无法进行复杂的集成吗?

编辑: 将我的代码更改为user14717提供的版本会给我一个新的编译器错误:

/usr/include/boost/math/quadrature/gauss_kronrod.hpp: In instantiation of 'static boost::math::quadrature::gauss_kronrod<Real, N, Policy>::value_type boost::math::quadrature::gauss_kronrod<Real, N, Policy>::integrate(F, Real, Real, unsigned int, Real, Real*, Real*) [with F = std::complex<double> (*)(double); Real = double; unsigned int N = 61; Policy = boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy>; boost::math::quadrature::gauss_kronrod<Real, N, Policy>::value_type = double]':
/home/olaf/testing numerical integration/boost/main.cpp:21:91:   required from here
/usr/include/boost/math/quadrature/gauss_kronrod.hpp:1877:47: error: cannot convert 'std::complex<double>' to 'double' in return
                return f(t*inv)*(1 + t_sq)*inv*inv;
                                               ^~~
/usr/include/boost/math/quadrature/gauss_kronrod.hpp:1890:32: error: cannot convert ?std::complex<double>? to 'double' in return
                return f(arg)*z*z;
                                ^
/usr/include/boost/math/quadrature/gauss_kronrod.hpp:1907:40: error: cannot convert 'std::complex<double>' to 'double' in return
                return f(b - arg) * z * z;
                                        ^

1 个答案:

答案 0 :(得分:2)

解决方法是

null

您的错误是您以复杂类型为模板。模板参数必须为实数。

我注意到这里的文档没有提供示例,因此您实际上没有办法知道这一点。我将为此提供一个补丁。 。