This example shows multi-dimensional numerical integration.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#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.
Maps function, bind and cref to either the boost or std implementation.
std::size_t Size
size of a container
Global definitions and compiler switches.
basic support for the singleton pattern
integral of a one-dimensional function using the trapezoid formula