使用来自 boost::math 的 Gauss-Kronrod 正交积分复杂函数
Integration of a complex function using Gauss-Kronrod quadrature from boost::math
我正在尝试使用 Gauss-Kronrod 正交在 C++ 中以数字方式在域 [0, 5] 上将函数与实参数和复数 return 值(例如 f(x) = 1 + i)积分正如 Boost 提供的那样。
我已经使用 boost 来求解有效的真实被积函数,因此该库已正确安装。
documentation 表示 "The Gauss-Kronrod quadrature support integrands defined on the real line and returning complex values."
#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;
}
您的错误是您在复杂类型上进行了模板化。模板参数必须是真实类型。
我注意到文档没有在这里提供示例,所以您真的没有办法知道这一点。我会为此提交一个补丁。 . .
我正在尝试使用 Gauss-Kronrod 正交在 C++ 中以数字方式在域 [0, 5] 上将函数与实参数和复数 return 值(例如 f(x) = 1 + i)积分正如 Boost 提供的那样。
我已经使用 boost 来求解有效的真实被积函数,因此该库已正确安装。 documentation 表示 "The Gauss-Kronrod quadrature support integrands defined on the real line and returning complex values."
#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;
}
您的错误是您在复杂类型上进行了模板化。模板参数必须是真实类型。
我注意到文档没有在这里提供示例,所以您真的没有办法知道这一点。我会为此提交一个补丁。 . .