CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
integration_cuba.cpp

This example shows how to use the wrapper for CUBA multidimensiona integration functions

// ===============================================================================================
// Example code: how to integrate a multidimensional function using wrappers to the CUBA libraries
// ===============================================================================================
#include "Func.h"
int main () {
try {
// dimension of the integrand function
const int ndim = 2;
// function to be integrated
auto integrand = [] (const std::vector<double> x)
{
double sum = 0.;
for (size_t i=0; i<x.size(); i++)
sum += x[i];
return sum;
};
// limits of the integral
std::vector<std::vector<double>> integration_limits(2);
integration_limits[0] = {0., 2.};
integration_limits[1] = {0., 2.};
// wrapper to CUBA libraries
cbl::wrapper::cuba::CUBAwrapper CW(integrand, ndim);
// write the outputs
std::cout << "The integral computed with Vegas is: " << CW.IntegrateVegas(integration_limits) << std::endl;
std::cout << "The integral computed with Suave is: " << CW.IntegrateSuave(integration_limits) << std::endl;
std::cout << "The integral computed with Divonne is: " << CW.IntegrateDivonne(integration_limits) << std::endl;
std::cout << "The integral computed with Cuhre is: " << CW.IntegrateCuhre(integration_limits) << std::endl;
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
Useful generic functions.
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
The class CUBAwrapper.
Definition: CUBAwrapper.h:187
double IntegrateSuave(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Suave routine
double IntegrateDivonne(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Divonne routine
double IntegrateCuhre(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Cuhre routine
double IntegrateVegas(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Vegas routine