This example shows multi-dimensional numerical integration.
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(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 <ql/patterns/singleton.hpp>
#include <ql/functional.hpp>
#include <iostream>
#include <iomanip>
struct integrand {
Real operator()(
const std::vector<Real>& arg)
const {
sum *= std::exp(-i * i) * std::cos(i);
return sum;
}
};
int main() {
try {
std::cout << std::endl;
Real exactSol = std::pow(std::exp(-.25) *
std::sqrt(M_PI),
static_cast<Real>(dimension));
ext::function<
Real(
const std::vector<Real>& arg)> f = integrand();
#ifndef QL_PATCH_SOLARIS
Real valueQuad = intg(f);
#endif
std::vector<ext::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.);
Real valueGrid = testIntg(f, a_limits, b_limits);
cout << fixed << setprecision(4);
cout << endl << "-------------- " << endl
<< "Exact: " << exactSol << endl
#ifndef QL_PATCH_SOLARIS
<< "Quad: " << valueQuad << endl
#endif
<< "Grid: " << valueGrid << endl
<< endl;
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "unknown error" << std::endl;
return 1;
}
}
Integrates a vector or scalar function of vector domain.
Integrates a vector or scalar function of vector domain.
Integral of a one-dimensional function.
std::size_t Size
size of a container