Changeset 358
- Timestamp:
- Aug 3, 2005, 6:09:44 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/gslapi/utility.cc
r301 r358 34 34 vector vec(invec); 35 35 utility::random_singleton* 36 rnd=utility::random_singleton:: get_instance();36 rnd=utility::random_singleton::instance(); 37 37 for (size_t i=0; i<vec.size(); i++){ 38 size_t index = rnd-> get_uniform_int(vec.size()-i);38 size_t index = rnd->uniform_int(vec.size()-i); 39 39 invec[i]=vec(index); 40 40 vec(index)=vec(vec.size()-i-1); -
trunk/lib/utility/random_singleton.cc
r308 r358 32 32 33 33 34 void random_singleton::se t_seed( const int& seed )34 void random_singleton::seed( const int& seed ) 35 35 { 36 36 if( seed < 0 ) … … 45 45 46 46 47 random_singleton* random_singleton:: get_instance( int seed )47 random_singleton* random_singleton::instance( int seed ) 48 48 { 49 49 if( !singleobj_ ) 50 50 { 51 51 singleobj_ = new random_singleton(); 52 singleobj_->se t_seed( seed );52 singleobj_->seed( seed ); 53 53 } 54 54 return singleobj_; -
trunk/lib/utility/random_singleton.h
r312 r358 15 15 namespace utility { 16 16 17 /** 18 Class defining interface for different random 19 stuff. 20 */ 17 /// 18 /// Class defining interface for different random stuff. 19 /// 21 20 class random_singleton 22 21 { 22 public: 23 /// 24 /// Method used to instantiate an object from this class. The 25 /// seed is zero by default. If user wish to use time the user 26 /// should specify a negative seed. Note that if an object 27 /// already exists the method will return that object and hence 28 /// no new object is created. 29 /// 30 static random_singleton* instance( int seed = 0 ); 31 32 33 /// 34 /// A random unsigned long integer number is returned from a 35 /// uniform distribution. 36 /// 37 /// @return If \a n is zero, or left out, a number between [ \a 38 /// min, \a max ] is returned, otherwise a number between 0 and 39 /// n-1 is returned. 40 /// 41 /// @see uniform_int_min() and uniform_int_max(). 42 /// 43 inline u_long uniform_int(const u_long n=0) const; 44 45 46 /// A random number (double precision) is returned from a uniform 47 /// distribution between [ 0, 1 [. 48 /// 49 inline double uniform_double() const; 50 51 52 /// 53 /// A random number (double precision) is returned from a 54 /// gaussioan distribution between where \a sigma can be specified 55 /// by caller. \a sigma (\f$\sigma\f$) is the standard deviation 56 /// in a gaussian distribution: 57 /// 58 /// \f$ f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{x^2}{2\sigma^2}}\f$ 59 /// 60 inline double gaussian( const double& sigma=1 ) const; 61 62 63 /// 64 /// A random number (double precision) is returned from an 65 /// exponential distribution. \a mu (\f$\mu\f$), the mean, can be 66 /// specified by caller: \f$f(x) = 67 /// \frac{1}{\mu}e^{-\frac{x}{\mu}}\f$. 68 /// 69 inline double exponential( const double& mu=1 ) const; 70 71 72 /// 73 /// @brief random number from Poisson distribution 74 /// 75 /// Probability that returned number i \f$ k \f$ is \f$ P(k) = 76 /// \frac{m^k}{k!}e^{-m} \f$ 77 /// 78 inline u_int poisson(const double m) const 79 { return gsl_ran_poisson(r_, m); } 80 81 /// 82 /// @See uniform_int()! 83 /// 84 inline u_long uniform_min() const; 85 86 87 /// 88 /// @See uniform_int()! 89 /// 90 inline u_long uniform_max() const; 91 92 93 /// 94 /// Return seed to user for documentation purposes. 95 /// 96 inline int seed() const; 97 98 /// 99 /// Set a new seed 100 /// 101 void seed( const int& seed ); 102 103 104 /// 105 /// Set probabilities \a p for the general purpose distribution. 106 /// 107 /// @see rnd_discrete 108 /// 109 inline void general_distribution_prob( const size_t& k, 110 const double* p ); 111 112 113 /// 114 /// Get a random number (index) from the general purpose 115 /// distribution 116 /// 117 /// @see general_distribution_prob 118 /// 119 inline size_t rnd_discrete(); 120 121 122 /// 123 /// GSL has different types of random genenerators. This function 124 /// returns the one used. 125 /// 126 std::string generator_type(void) const; 127 128 /// 129 /// Operator for STL 130 /// 131 ///inline double operator()(void) { return uniform_double();} 132 133 134 /// 135 /// Destructor returning memory using GSL free functions 136 /// 137 ~random_singleton(); 138 23 139 private: 24 / **25 26 */140 /// 141 /// pointer to an instance of the class. 142 /// 27 143 static random_singleton *singleobj_; 28 144 29 / **30 31 */145 /// 146 /// GSL stuff 147 /// 32 148 gsl_rng* r_; 33 149 gsl_ran_discrete_t* gen_; // general purpose distribution 34 150 35 / **36 Private constructor. Only way to37 create object is via get_instance().38 */151 /// 152 /// Private constructor. Only way to create object is via 153 /// instance(). 154 /// 39 155 random_singleton(); 40 41 public: 42 /** 43 Method used to instantiate an object from this class. 44 The seed is zero by default. If user wish to use time 45 the user should specify a negative seed. Note that if an 46 object already exists the method will return that object 47 and hence no new object is created. 48 */ 49 static random_singleton* get_instance( int seed = 0 ); 50 51 52 /** 53 A random unsgined long integer number is returned from a 54 uniform distribution. 55 56 @return If \a n is zero, or left out, a number between [ \a 57 min, \a max ] is returned, otherwise a number between 0 and n-1 58 is returned. 59 60 The values of \a min and \a max are obtained via 61 get_uniform_int_min() and get_uniform_int_max(). 62 */ 63 inline u_long get_uniform_int(const u_long n=0) const; 64 65 66 /** 67 A random number (double precision) is returned 68 from a uniform distribution between [ 0, 1 [. 69 */ 70 inline double get_uniform_double() const; 71 72 73 /** 74 A random number (double precision) is returned 75 from a gaussioan distribution between where \a sigma 76 can be specified by caller. \a sigma (\f$\sigma\f$) is 77 the standard deviation in a gaussian distribution: \n\n 78 \f$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{x^2}{2\sigma^2}}\f$ \n\n 79 Default is \a sigma = 1. 80 */ 81 inline double get_gaussian( const double& sigma ) const; 82 83 84 /** 85 A random number (double precision) is returned 86 from an exponential distribution. \a mu (\f$\mu\f$), the mean, 87 can be specified by caller: \f$f(x) = \frac{1}{\mu}e^{-\frac{x}{\mu}}\f$. 88 Default is \a mu = 1. 89 */ 90 inline double get_exponential( const double& mu ) const; 91 92 93 /// 94 /// @brief random number from Poisson distribution 95 /// 96 /// Probability that returned number i \f$ k \f$ is \f$ P(k) = 97 /// \frac{m^k}{k!}e^{-m} \f$ 98 inline u_int get_poisson(const double m) const 99 { return gsl_ran_poisson(r_, m); } 100 101 /** 102 @See get_uniform_int()! 103 */ 104 inline u_long get_uniform_min() const; 105 106 107 /** 108 @See get_uniform_int()! 109 */ 110 inline u_long get_uniform_max() const; 111 112 113 /** 114 Return seed to user for documentation purposes. 115 */ 116 inline int get_seed() const; 117 118 /** 119 Set a new seed 120 */ 121 void set_seed( const int& seed ); 122 123 124 /** 125 Set probabilities \a p for the general purpose 126 distribution. 127 */ 128 inline void set_general_distribution_prob( const size_t& k, 129 const double* p ); 130 131 132 /** 133 Get a random number (index) from the general purpose 134 distribution 135 */ 136 inline size_t get_rnd_discrete(); 137 138 139 /** 140 GSL has different types of random genenerators. This function 141 returns the one used. 142 */ 143 std::string get_generator_type(void) const; 144 145 /// 146 /// Operator for STL 147 /// 148 ///inline double operator()(void) { return get_uniform_double();} 149 150 151 /** 152 Destructor returning memory using GSL free functions 153 */ 154 ~random_singleton(); 155 }; // random_singleton 156 }; 156 157 157 158 struct my_uniform_rng … … 159 160 public: 160 161 inline u_long operator()(u_long i) { 161 return random_singleton:: get_instance()->get_uniform_int(i);162 return random_singleton::instance()->uniform_int(i); 162 163 } 163 164 }; 164 165 165 double random_singleton:: get_exponential( const double& mu ) const166 double random_singleton::exponential( const double& mu ) const 166 167 { 167 168 return gsl_ran_exponential( r_, mu ); … … 169 170 170 171 171 double random_singleton::g et_gaussian( const double& sigma ) const172 double random_singleton::gaussian( const double& sigma ) const 172 173 { 173 174 return gsl_ran_gaussian( r_, sigma ); … … 175 176 176 177 177 inline std::string random_singleton::ge t_generator_type() const178 inline std::string random_singleton::generator_type() const 178 179 { 179 180 return static_cast<std::string>( gsl_rng_name( r_ ) ); … … 181 182 182 183 183 size_t random_singleton:: get_rnd_discrete()184 size_t random_singleton::rnd_discrete() 184 185 { 185 186 return gsl_ran_discrete( r_, gen_ ); … … 187 188 188 189 189 int random_singleton:: get_seed() const190 int random_singleton::seed() const 190 191 { 191 192 return gsl_rng_default_seed; … … 193 194 194 195 195 double random_singleton:: get_uniform_double() const196 double random_singleton::uniform_double() const 196 197 { 197 198 return gsl_rng_uniform( r_ ); … … 199 200 200 201 201 u_long random_singleton:: get_uniform_int(const u_long n) const202 u_long random_singleton::uniform_int(const u_long n) const 202 203 { 203 204 return ( n ? gsl_rng_uniform_int(r_,n) : gsl_rng_get( r_ )); … … 205 206 206 207 207 u_long random_singleton:: get_uniform_min() const208 u_long random_singleton::uniform_min() const 208 209 { 209 210 return gsl_rng_min( r_ ); … … 211 212 212 213 213 u_long random_singleton:: get_uniform_max() const214 u_long random_singleton::uniform_max() const 214 215 { 215 216 return gsl_rng_max( r_ ); … … 217 218 218 219 219 void random_singleton:: set_general_distribution_prob( const size_t& k,220 220 void random_singleton::general_distribution_prob( const size_t& k, 221 const double* p ) 221 222 { 222 223 gen_ = gsl_ran_discrete_preproc( k, p ); -
trunk/test/rnd_test.cc
r301 r358 10 10 { 11 11 utility::random_singleton* my_rnd; 12 my_rnd = utility::random_singleton:: get_instance( -1 );12 my_rnd = utility::random_singleton::instance( -1 ); 13 13 14 14 double* prob = new double[ 5 ]; … … 18 18 prob[ 3 ] = 0.2; 19 19 prob[ 4 ] = 0.2; 20 my_rnd-> set_general_distribution_prob( 5, prob );20 my_rnd->general_distribution_prob( 5, prob ); 21 21 delete prob; 22 22 … … 29 29 30 30 for( size_t i = 0; i < 100; ++i ) 31 antal[ my_rnd-> get_rnd_discrete() ]++;31 antal[ my_rnd->rnd_discrete() ]++; 32 32 33 33 delete my_rnd; -
trunk/test/svd_test.cc
r301 r358 23 23 // accepted error, Jari: should be picked up from GSL 24 24 double MAXTOL=1e-13; 25 utility::random_singleton* rng=utility::random_singleton:: get_instance(-1);25 utility::random_singleton* rng=utility::random_singleton::instance(-1); 26 26 27 27 // initialise a random test-matrix … … 29 29 for (size_t i=0; i<m; ++i) 30 30 for(size_t j=0; j<n; ++j) 31 A(i,j)=1000*rng-> get_uniform_double();31 A(i,j)=1000*rng->uniform_double(); 32 32 33 33 utility::SVD svd(A);
Note: See TracChangeset
for help on using the changeset viewer.