From c123ef8d8726a1c3af5dbbddafc18785ba14142e Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Thu, 18 Feb 2016 18:13:02 -0500 Subject: add Nlopt version of sobol which allows higher dimensions --- src/low_discrepancy.hpp | 73 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 25 deletions(-) (limited to 'src/low_discrepancy.hpp') diff --git a/src/low_discrepancy.hpp b/src/low_discrepancy.hpp index 6b3c1bd..bc8e820 100644 --- a/src/low_discrepancy.hpp +++ b/src/low_discrepancy.hpp @@ -8,6 +8,9 @@ #include #include #include +#ifdef NLOPT +#include "nlopt-util.h" +#endif class p_adic { public: @@ -159,27 +162,32 @@ struct faure { struct sobol { - typedef std::vector result_type; - sobol(int dimension) : dimension(dimension), result(dimension) { - q = gsl_qrng_alloc(gsl_qrng_sobol, dimension); - } - sobol(sobol const & o) : dimension(o.dimension) { - q = gsl_qrng_clone(o.q); - result = o.result; - } + typedef std::vector result_type; + sobol(int dimension) : dimension(dimension), result(dimension) { +#ifdef NLOPT + s = nlopt_sobol_create(dimension); +#else + q = gsl_qrng_alloc(gsl_qrng_sobol, dimension); +#endif + } +#ifndef NLOPT + sobol(sobol const & o) : dimension(o.dimension) { + q = gsl_qrng_clone(o.q); + result = o.result; + } // constructeur move uniquement en C++11 sobol(sobol && o) : dimension(o.dimension), q(o.q), result(std::move(o.result)) { o.dimension = 0; o.q = nullptr; } - sobol & operator=(sobol const & o) { - if (this != &o) { - dimension = o.dimension; - gsl_qrng_memcpy(q, o.q); - result = o.result; - } - return *this; - } + sobol & operator=(sobol const & o) { + if (this != &o) { + dimension = o.dimension; + gsl_qrng_memcpy(q, o.q); + result = o.result; + } + return *this; + } // operateur move uniquement en C++11 sobol & operator=(sobol && o) { if (this != &o) { @@ -191,15 +199,30 @@ struct sobol { } return *this; } - ~sobol() { gsl_qrng_free(q); } - result_type operator()() { - gsl_qrng_get(q, &(*result.begin())); - return result; - } - protected: - int dimension; - gsl_qrng * q; - result_type result; +#endif + ~sobol() { +#ifdef NLOPT + nlopt_sobol_destroy(s); +#else + gsl_qrng_free(q); +#endif + } + result_type operator()() { +#ifdef NLOPT + nlopt_sobol_next01(s, result.data()); +#else + gsl_qrng_get(q, result.data()); +#endif + return result; + } +protected: + int dimension; +#ifdef NLOPT + nlopt_sobol s; +#else + gsl_qrng * q; +#endif + result_type result; }; #endif -- cgit v1.2.3-70-g09d2