Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/utility/distributions.hpp
Go to the documentation of this file.
00001 
00004 #ifndef SKYLARK_DISTRIBUTIONS_HPP
00005 #define SKYLARK_DISTRIBUTIONS_HPP
00006 
00007 #include <boost/random.hpp>
00008 
00009 namespace skylark {
00010 namespace utility {
00011 
00012 
00016 template< typename ValueType >
00017 struct standard_levy_distribution_t {
00018 
00019     // TODO not really sure this is the standard, or implemented correctly
00020 
00021     typedef ValueType result_type;
00022 
00023     standard_levy_distribution_t() {
00024 
00025     }
00026 
00027     template< typename URNG >
00028     ValueType operator()(URNG &prng) const {
00029         boost::random::gamma_distribution<ValueType> dist(0.5, 2);
00030         result_type y = static_cast<ValueType>(dist(prng));
00031         return (1.0 / y);
00032     }
00033     void reset() {}
00034 
00035 };
00036 
00040 template< typename ValueType >
00041 struct rademacher_distribution_t {
00042 
00043     typedef ValueType result_type;
00044 
00045     template< typename URNG >
00046     ValueType operator()(URNG &prng) const {
00047         double probabilities[] = { 0.5, 0.0, 0.5 };
00048         boost::random::discrete_distribution<> dist(probabilities);
00049         return static_cast<ValueType>(dist(prng)) - 1.0;
00050     }
00051     void reset() {}
00052 };
00053 
00057 template <typename ValueType> struct uniform_distribution_t {
00058     typedef ValueType result_type;
00059 };
00060 
00064 template <> struct uniform_distribution_t <double> {
00065 
00066     typedef double result_type;
00067 
00068     boost::random::uniform_real_distribution<double> distribution;
00069 
00070     uniform_distribution_t() {}
00071 
00072     uniform_distribution_t(double low, double high) :
00073       distribution(low, high) {}
00074 
00075     template< typename URNG >
00076     double operator()(URNG &urng) const {
00077         return distribution(urng);
00078     }
00079     void reset() {}
00080 };
00081 
00085 template <> struct uniform_distribution_t <int> {
00086 
00087     typedef int result_type;
00088 
00089     boost::random::uniform_int_distribution<int> distribution;
00090 
00091     uniform_distribution_t() {}
00092 
00093     uniform_distribution_t(int low, int high) :
00094       distribution(low, high) {}
00095 
00096     template< typename URNG >
00097     int operator()(URNG &urng) const {
00098         return distribution(urng);
00099     }
00100     void reset() {}
00101 };
00102 
00106 template <> struct uniform_distribution_t <size_t> {
00107 
00108     typedef int result_type;
00109 
00110     boost::random::uniform_int_distribution<size_t> distribution;
00111 
00112     uniform_distribution_t() {}
00113 
00114     uniform_distribution_t(size_t low, size_t high) :
00115       distribution(low, high) {}
00116 
00117     template< typename URNG >
00118     int operator()(URNG &urng) const {
00119         return distribution(urng);
00120     }
00121     void reset() {}
00122 };
00123 
00124 
00128 template <> struct uniform_distribution_t <bool> {
00129 
00130     typedef bool result_type;
00131 
00132     boost::random::uniform_int_distribution<int> distribution;
00133 
00134     uniform_distribution_t() {}
00135 
00136     template< typename URNG >
00137     bool operator()(URNG &urng) const {
00138         return (1==distribution(urng));
00139     }
00140     void reset() {}
00141 };
00142 
00143 } // namespace utility
00144 } // namespace skylark
00145 
00146 #endif // SKYLARK_DISTRIBUTIONS_HPP