Boost logo

Boost Users :

From: Dan Bloomquist (boost_at_[hidden])
Date: 2020-05-11 01:07:38


Ritajit Kundu via Boost-users wrote:
> I am trying to compute a 2 Dimensional numerical integration with the
> help of Boost. I've been reading the documentation for a couple of
> days and my CPP language knowledge is just up to legacy CPP level so
> I'm a noob.
>
> I saw this page
> <https://www.boost.org/doc/libs/1_73_0/libs/math/doc/html/math_toolkit/gauss_kronrod.html>
> in documentation which calculates the quadrature using*Gauss-Kronrod
> *rule. I was trying to build upon it and calculate a 2-d integral
> using Gauss-Kronrod rule. My understanding is *a 2D integral is
> basically 2 1D integrals with the Cartesian product.* So I was
> thinking If I can nest two integration function then I'm done. I've
> started from the aforementioned example and extended it a bit so the
> code for a 2d integration looks like,
>
Hi Ritajit,
I'm not up on combining the integrals as you would like. A math pro may
come along and point it out. What you can't do is make up new
functionality that the library does not support. What you will probably
want to do is run with two independent gauss_kronrod calculations and
combine them for your Cartesian product; code you would write on top of
that.

For instance, the signature,(what the integrate(..) call want to see),
of the lambda is a function that has one parameter of a double and
returns a double. Just as in the example:

double (lambda){ return a double;} integrate(..) can not be made to work
in two dimensions by changing the way you call it. It will only work on
a 1 D integral.

>       double Q = gauss_kronrod<double,
> 15>::integrate(gauss_kronrod<double, 61>::integrate(f1, 0,
> std::numeric_limits<double>::infinity(), 5), 0,
> std::numeric_limits<double>::infinity(), 5, 1e-9, &error);

Look into C++ function signatures. Integrate looks like.
 Â Â  static auto integrate(F f, Real a, Real b, unsigned max_depth = 15,
Real tol = tools::root_epsilon<Real>(), Real* error = nullptr, Real* pL1
= nullptr)....
I've talked about the signature of F, above you are passing a POD,
double, the return value of integrate. This is what 'error: no matching
function...' means
Like I said, I don't know the 'math' answer here but at the least, this
does compile.
Best, Dan

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

int main(int argc, char* argv[])
{
 Â Â Â  using namespace boost::math::quadrature;
 Â Â Â  auto f1 = [](double t) { return std::exp(-(t * t + 5 * 5 + t * 5) /
2); };
 Â Â Â  auto f2 = [](double s) { return std::exp(-(3 * 3 + s * s + 3 * s) /
2); };
 Â Â Â  double error;

 Â Â Â  //for each across your matrix???
 Â Â Â  double X = gauss_kronrod<double, 15>::integrate(f1, 0,
std::numeric_limits<double>::infinity(), 5, 1e-14, &error);
 Â Â Â  double Y = gauss_kronrod<double, 15>::integrate(f1, 0,
std::numeric_limits<double>::infinity(), 5, 1e-14, &error);
 Â Â Â  std::cout << X << " " << Y << " " << error << std::endl;

 Â Â Â  double error2;
 Â Â Â  double Q = gauss_kronrod<double, 15>::integrate(f1,
 Â Â Â Â Â Â Â  gauss_kronrod<double, 15>::integrate(f2, 0,
std::numeric_limits<double>::infinity(), 5, 1e-14, &error2),
 Â Â Â Â Â Â Â  std::numeric_limits<double>::infinity(), 5, 1e-14, &error);

 Â Â Â  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net