//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Base/Math/IntegratorMCMiser.h
//! @brief     Defines class IntegratorMCMiser.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifdef SWIG
#error no need to expose this header to Swig
#endif // SWIG
#ifndef BORNAGAIN_BASE_MATH_INTEGRATORMCMISER_H
#define BORNAGAIN_BASE_MATH_INTEGRATORMCMISER_H

#include <functional>
#include <gsl/gsl_monte_miser.h>
#include <memory>

//! Monte Carlo MISER integrator. Wraps the C function from GNU Scientific Library.
//!
//! The MISER algorithm of Press and Farrar is based on recursive stratified sampling.
//! This technique aims to reduce the overall integration error by concentrating
//! integration points in the regions of highest variance. See
//! https://www.gnu.org/software/gsl/doc/html/montecarlo.html.

class IntegratorMCMiser {
public:
    IntegratorMCMiser(size_t dim);
    IntegratorMCMiser(IntegratorMCMiser&&) = default;
    ~IntegratorMCMiser();

    using integrand_t = std::function<double(double* fractions, void* params)>;

    //! Performs the actual integration over the ranges [min_array, max_array]
    double integrate(double* min_array, double* max_array, const integrand_t& f, void* params,
                     size_t n_points) const;

private:
    const size_t m_dim; //!< dimension of the integration
    gsl_monte_miser_state* m_gsl_workspace;
    gsl_rng* m_random_gen;
};

#endif // BORNAGAIN_BASE_MATH_INTEGRATORMCMISER_H
