Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
uniform_random.hpp
00001 #ifndef JMATH_UNIFORM_GENERATOR_HPP
00002 #define JMATH_UNIFORM_GENERATOR_HPP
00003 
00004 #include <boost/random/mersenne_twister.hpp>
00005 #include <boost/random/uniform_int.hpp>
00006 #include <boost/random/uniform_real.hpp>
00007 #include <boost/random/variate_generator.hpp>
00008 #include <boost/numeric/ublas/vector.hpp>
00009 #include <boost/numeric/ublas/matrix.hpp>
00010 #include <limits>
00011 #include <ctime>
00012 
00014 namespace ublas = boost::numeric::ublas;
00015 
00016 namespace jafar {
00017   namespace jmath {
00019     template <typename T> struct uniform_distribution;
00021     template<> struct uniform_distribution<int> {
00022       typedef boost::uniform_int<int> type;
00023     };
00025     template<> struct uniform_distribution<unsigned int> {
00026       typedef boost::uniform_int<unsigned int> type;
00027     };
00029     template<> struct uniform_distribution<float> {
00030       typedef boost::uniform_real<float> type;
00031     };
00033     template<> struct uniform_distribution<double> {
00034       typedef boost::uniform_real<double> type;
00035     };
00036 
00037     template<typename T>
00038     class UNIFORM_GENERATOR {
00039       T min;
00040       T max;
00041       uint32_t seed_value;
00042       typedef boost::mt19937 generator_type;
00043       typedef typename uniform_distribution<T>::type distribution_type;
00044       distribution_type distribution;
00045       generator_type rng;
00046       boost::variate_generator<generator_type&, distribution_type> generator;
00047 
00048     public:
00054       UNIFORM_GENERATOR(T _min = 0, 
00055                         T _max = 1, 
00056                         uint32_t _seed = -1) : 
00057         min(_min), max(_max), seed_value(_seed),
00058         distribution(_min, _max), generator(rng, distribution) {
00059         if(seed_value != -1)
00060           rng.seed(_seed);
00061       }
00065       inline T run() {
00066         return generator();
00067       }
00068 
00073       void seed(uint32_t _seed) {
00074         rng.seed(_seed);
00075       }
00079       inline void fill_vector(ublas::vector<T>& v) {
00080         for(typename ublas::vector<T>::iterator it = v.begin(); it != v.end(); it++)
00081           *it = run();
00082       }     
00083       inline void fill_vector(std::vector<T>& v) {
00084         for(typename std::vector<T>::iterator it = v.begin(); it != v.end(); it++)
00085           *it = run();
00086       }
00090       inline void fill_matrix(ublas::matrix<T>& m) {
00091         for(typename ublas::matrix<T>::iterator1 rit = m.begin1(); rit != m.end1(); rit++)
00092           for(typename ublas::matrix<T>::iterator2 it = rit.begin(); it != rit.end(); it++)
00093             *it = run();
00094       }
00095     };
00096     typedef UNIFORM_GENERATOR<double> uniform_generator;
00097   }
00098 
00099   namespace random {
00105     template<typename T>
00106     static void uniform_fill(T min, T max, ublas::vector<T>& v) {
00107       jafar::jmath::UNIFORM_GENERATOR<T> gen(min, max, static_cast<unsigned int>(std::time(0)));
00108       gen.fill_vector(v);
00109     }
00115     template<typename T>
00116     static void uniform_fill(T min, T max, std::vector<T>& v) {
00117       jafar::jmath::UNIFORM_GENERATOR<T> gen(min, max, static_cast<unsigned int>(std::time(0)));
00118       gen.fill_vector(v);
00119     }
00120 
00126     template<typename T>
00127     static void uniform_fill(T min, T max, ublas::matrix<T>& m) {
00128       jafar::jmath::UNIFORM_GENERATOR<T> gen(min, max, static_cast<unsigned int>(std::time(0)));
00129       gen.fill_matrix(m);
00130     }
00131   }
00132 
00133 }
00134 
00135 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS