This example shows multi-dimensional numerical integration.
#include <ql/qldefines.hpp>
#ifdef BOOST_MSVC
# include <ql/auto_link.hpp>
#endif
#include <ql/experimental/math/multidimintegrator.hpp>
#include <ql/experimental/math/multidimquadrature.hpp>
#include <ql/math/integrals/trapezoidintegral.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>
#if defined(QL_ENABLE_SESSIONS)
}
#endif
struct integrand {
Real operator()(
const std::vector<Real>& arg)
const {
for(
Size i=0; i<arg.size(); i++)
sum *= std::exp(-arg[i]*arg[i]) * std::cos(arg[i]);
return sum;
}
};
int main() {
boost::timer timer;
std::cout << std::endl;
Real exactSol = std::pow(std::exp(-.25) *
std::sqrt(M_PI), static_cast<Real>(dimension));
boost::function<Real(const std::vector<Real>& arg)> f = integrand();
#ifndef QL_PATCH_SOLARIS
timer.restart();
Real valueQuad = intg(f);
Real secondsQuad = timer.elapsed();
#endif
std::vector<boost::shared_ptr<Integrator> > integrals;
for(
Size i=0; i<dimension; i++)
integrals.push_back(
std::vector<Real> a_limits(integrals.size(), -4.);
std::vector<Real> b_limits(integrals.size(), 4.);
timer.restart();
Real valueGrid = testIntg(f, a_limits, b_limits);
Real secondsGrid = timer.elapsed();
cout << fixed << setprecision(4);
cout << endl << "-------------- " << endl
<< "Exact: " << exactSol << endl
#ifndef QL_PATCH_SOLARIS
<< "Quad: " << valueQuad << endl
#endif
<< "Grid: " << valueGrid << endl
<< endl;
cout
#ifndef QL_PATCH_SOLARIS
<< "Seconds for Quad: " << secondsQuad << endl
#endif
<< "Seconds for Grid: " << secondsGrid << endl;
return 0;
}