00001
00002
00003 #ifndef JMATH_RANDOM_HPP
00004 #define JMATH_RANDOM_HPP
00005
00006 #include <vector>
00007
00008 #include "boost/random.hpp"
00009 #include <boost/numeric/ublas/blas.hpp>
00010
00011 #include "jmath/jblas.hpp"
00012
00013
00014 namespace jafar {
00015 namespace jmath {
00016
00020 unsigned get_srand();
00021
00022
00028 class UniformDistribution {
00029 public:
00030
00031 UniformDistribution(unsigned int seed_ = 1);
00032 UniformDistribution(double min_, double max_, unsigned int seed_ = 1);
00033 ~UniformDistribution();
00034
00035 double get();
00036
00037 private:
00038
00039 boost::mt19937 rng;
00040 boost::uniform_real<double> uniform;
00041
00042 boost::variate_generator<boost::mt19937, boost::uniform_real<double> > vg;
00043 };
00044
00050 class MultiDimUniformDistribution {
00051
00052 public:
00053
00054 MultiDimUniformDistribution(std::size_t dim_, unsigned int seed_ = 1);
00055 MultiDimUniformDistribution(std::size_t dim_, double min_, double max_, unsigned int seed_ = 1);
00056 MultiDimUniformDistribution(const jblas::vec& min_, const jblas::vec& max_, unsigned int seed_ = 1);
00057 ~MultiDimUniformDistribution();
00058
00059 jblas::vec& get();
00060
00061 private:
00062
00063 jblas::vec u;
00064
00065 std::vector<UniformDistribution*> uniformsVec;
00066
00067 };
00068
00069
00075 class NormalDistribution {
00076 public:
00077
00078 NormalDistribution(unsigned int seed_ = 1);
00079 NormalDistribution(double mean_, double sigma_, unsigned int seed_ = 1);
00080
00081 ~NormalDistribution();
00082
00083 double get();
00084
00085 private:
00086
00087 boost::mt19937 rng;
00088 boost::normal_distribution<double> normal;
00089
00090 boost::variate_generator<boost::mt19937, boost::normal_distribution<double> > vg;
00091
00092 };
00093
00099 class MultiDimNormalDistribution {
00100
00101 public:
00102
00103 MultiDimNormalDistribution(std::size_t dim_, unsigned int seed_ = 1);
00104 MultiDimNormalDistribution(std::size_t dim_, double mean_, double cov_, unsigned int seed_ = 1);
00105 MultiDimNormalDistribution(const jblas::vec& mean_, const jblas::vec& cov_, unsigned int seed_ = 1);
00106 ~MultiDimNormalDistribution();
00107
00108 jblas::vec get();
00109
00110 private:
00111
00112 std::vector<NormalDistribution*> normalsVec;
00113
00114 };
00115
00116
00117
00118
00119
00120
00121 template<typename bubTemplateMatrix>
00122 void randMatrix(bubTemplateMatrix& M) {
00123 for (size_t i = 0; i < M.size1(); ++i)
00124 for (size_t j = 0; j < M.size2(); ++j) {
00125 M(i, j) = ((rand() + 0.0) / RAND_MAX * 2) - 1.;
00126 }
00127 }
00128
00129 template<typename bubTemplateMatrix>
00130 void randMatrix(bubTemplateMatrix& M, const size_t row, const size_t col) {
00131 M.resize(row, col);
00132 randMatrix(M);
00133 }
00134
00135 template<typename bubTemplateVector>
00136 void randVector(bubTemplateVector& V) {
00137 for (size_t i = 0; i < V.size(); ++i) {
00138 V(i) = ((rand() + 0.0) / RAND_MAX * 2) - 1.;
00139 }
00140 }
00141
00142 template<typename bubTemplateVector>
00143 void randVector(bubTemplateVector& V, const size_t size) {
00144 V.resize(size);
00145 randVector(V);
00146 }
00147
00148
00149 }
00150 }
00151
00152
00153 #endif // JMATH_RANDOM_HPP