gpt4 book ai didi

c++ - 使用来自 boost::math 的 Gauss-Kronrod 求积积分复杂函数

转载 作者:太空狗 更新时间:2023-10-29 21:32:07 25 4
gpt4 key购买 nike

我正在尝试使用 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;
^

最佳答案

修复方法如下:

#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() {
double error;
double a{0};
double b{5};
unsigned int max_depth = 0;
double tolerance = 0;
complex Q = gauss_kronrod<double, 61>::integrate(f, a, b, max_depth, tolerance, &error);
std::cout << Q << ", " << error << "\n";
return 0;
}

您的错误是您在复杂类型上进行了模板化。模板参数必须是真实类型。

我注意到文档在这里没有提供示例,所以您真的没有办法知道这一点。我会为此提交一个补丁。 . .

关于c++ - 使用来自 boost::math 的 Gauss-Kronrod 求积积分复杂函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com