Changeset 3604 in Sophya for trunk/SophyaLib/BaseTools
- Timestamp:
- Apr 29, 2009, 12:14:04 PM (16 years ago)
- Location:
- trunk/SophyaLib/BaseTools
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/randinterf.cc
r3602 r3604 14 14 //------------------------------------------------------------------------------- 15 15 // ------ Definition d'interface des classes de generateurs de nombres aleatoires 16 /*! 17 \class RandomGeneratorInterface 18 \ingroup BaseTools 19 \brief Base class for random number generators 20 21 This class defines the interface for random number generator classes and 22 implements the generation of some specific distributions (Gaussian, Poisson ...) 23 through generation of random number with a flat distribution in the range [0,1[. 24 25 The sub classes inheriting from this class should implement the Next() method. 26 27 This base class manages also a global instance of a default generator. 28 29 \sa frand01 drand01 frandpm1 drandpm1 30 \sa GauRnd PoissRand 31 32 */ 33 34 35 RandomGeneratorInterface* RandomGeneratorInterface::gl_rndgen_p = NULL; 36 37 /*! 38 \brief: static method to set or change the intance of the global Random Generator object 39 40 This method should be called during initialization, before any call to global 41 functions for random number generation. The rgp object should be created using new. 42 */ 43 void RandomGeneratorInterface::SetGlobalRandGenP(RandomGeneratorInterface* rgp) 44 { 45 if (rgp == NULL) return; 46 if (gl_rndgen_p) delete gl_rndgen_p; 47 gl_rndgen_p = rgp; 48 return; 49 } 16 50 17 51 RandomGeneratorInterface::RandomGeneratorInterface() 18 : usegaussian_(0), usepoisson_(0), useexpo_(0) 19 { 52 { 53 SelectGaussianAlgo(); 54 SelectPoissonAlgo(); 55 SelectExponentialAlgo(); 20 56 } 21 57 … … 26 62 } 27 63 28 void RandomGeneratorInterface::UseGaussian(uint_2 type)29 {30 usegaussian_ = type;31 }32 33 void RandomGeneratorInterface::UseExpo(uint_2 type)34 {35 useexpo_ = type;36 }37 38 void RandomGeneratorInterface::UsePoisson(uint_2 type)39 {40 usepoisson_ = type;41 }42 64 43 65 ///////////////////////////////////////////////////////////////////////// … … 150 172 } 151 173 152 153 ///////////////////////////////////////////////////////////////////////// 154 ///////////////////////////////////////////////////////////////////////// 155 ///////////////////////////////////////////////////////////////////////// 174 ///////////////////////////////////////////////////////////////////////// 175 ///////////////////////////////////////////////////////////////////////// 176 ///////////////////////////////////////////////////////////////////////// 177 178 r_8 RandomGeneratorInterface::Gaussian() 179 { 180 switch (usegaussian_) { 181 case C_Gaussian_BoxMuller : 182 return GaussianBoxMuller(); 183 break; 184 case C_Gaussian_RandLibSNorm : 185 return GaussianSNorm(); 186 break; 187 case C_Gaussian_PolarBoxMuller : 188 return GaussianPolarBoxMuller(); 189 break; 190 case C_Gaussian_RatioUnif : 191 return GaussianRatioUnif(); 192 break; 193 case C_Gaussian_LevaRatioUnif : 194 return GaussianLevaRatioUnif(); 195 break; 196 default: 197 return GaussianBoxMuller(); 198 break; 199 } 200 } 156 201 157 202 //--- Generation de nombre aleatoires suivant une distribution gaussienne … … 374 419 ///////////////////////////////////////////////////////////////////////// 375 420 421 uint_8 RandomGeneratorInterface::Poisson(double mu, double mumax) 422 { 423 switch (usepoisson_) { 424 case C_Poisson_Simple : 425 return PoissonSimple(mu,mumax); 426 break; 427 case C_Poisson_Ahrens : 428 return PoissonAhrens(mu); 429 break; 430 default: 431 return PoissonSimple(mu,mumax); 432 break; 433 } 434 } 435 436 376 437 //--- Generation de nombre aleatoires suivant une distribution de Poisson 377 438 uint_8 RandomGeneratorInterface::PoissonSimple(double mu,double mumax) … … 634 695 ///////////////////////////////////////////////////////////////////////// 635 696 ///////////////////////////////////////////////////////////////////////// 697 698 r_8 RandomGeneratorInterface::Exponential() 699 { 700 switch (useexpo_) { 701 case C_Exponential_Simple : 702 return ExpoSimple(); 703 break; 704 case C_Exponential_Ahrens : 705 return ExpoAhrens(); 706 break; 707 default: 708 return ExpoSimple(); 709 break; 710 } 711 } 636 712 637 713 r_8 RandomGeneratorInterface::ExpoSimple(void) -
trunk/SophyaLib/BaseTools/randinterf.h
r3602 r3604 15 15 namespace SOPHYA { 16 16 17 //! Definition of the interface for Random number generator classes 17 //! enum definition for the type of algorithm used for Gaussian distribution generation 18 enum GaussianGenAlgo { 19 C_Gaussian_BoxMuller = 0, 20 C_Gaussian_RandLibSNorm = 1, 21 C_Gaussian_PolarBoxMuller = 2, 22 C_Gaussian_RatioUnif = 3, 23 C_Gaussian_LevaRatioUnif = 4 24 }; 25 26 //! enum definition for the type of algorithm used for Poisson distribution generation 27 enum PoissonGenAlgo { 28 C_Poisson_Simple = 0, 29 C_Poisson_Ahrens = 1 30 }; 31 32 //! enum definition for the type of algorithm used for exponential distribution generation 33 enum ExponentialGenAlgo { 34 C_Exponential_Simple = 0, 35 C_Exponential_Ahrens = 1 36 }; 37 38 // Definition de l'interface pour les classes generateur aleatoire 18 39 class RandomGeneratorInterface : public AnyDataObj { 19 40 public: … … 21 42 virtual ~RandomGeneratorInterface(); 22 43 23 void UseGaussian(uint_2 type=0); 24 void UsePoisson(uint_2 type=0); 25 void UseExpo(uint_2 type=0); 44 //! Select Gaussian generation algorithm 45 inline void SelectGaussianAlgo(GaussianGenAlgo typ=C_Gaussian_BoxMuller) 46 { usegaussian_ = typ; } 47 //! Select Poisson generation algorithm 48 inline void SelectPoissonAlgo(PoissonGenAlgo typ=C_Poisson_Simple) 49 { usepoisson_ = typ; } 50 //! Select Exponential generation algorithm 51 inline void SelectExponentialAlgo(ExponentialGenAlgo typ=C_Exponential_Simple) 52 { useexpo_ = typ; } 26 53 27 void GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp=0); 54 //! Return the Gaussian generation algorithm type 55 inline GaussianGenAlgo GetGaussianAlgo() 56 { return usegaussian_; } 57 //! Return the Poisson generation algorithm type 58 inline PoissonGenAlgo GetPoissonAlgo() 59 { return usepoisson_; } 60 //! Return the Exponential generation algorithm type 61 //! Select Exponential generation algorithm 62 inline ExponentialGenAlgo GetExponentialAlgo() 63 { return useexpo_; } 64 65 virtual void GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp=0); 28 66 29 67 // --- Le tirage sur une distribution plate … … 35 73 // --- Le tirage sur une distribution gaussienne 36 74 /*! \brief Return a random number following a gaussian distribution (sigma=1, mean=0)*/ 37 inline r_8 Gaussian() 38 { 39 if(usegaussian_==0) return GaussianBoxMuller(); 40 if(usegaussian_==1) return GaussianSNorm(); 41 if(usegaussian_==2) return GaussianPolarBoxMuller(); 42 if(usegaussian_==3) return GaussianRatioUnif(); 43 if(usegaussian_==4) return GaussianLevaRatioUnif(); 44 return GaussianBoxMuller(); 45 } 75 virtual r_8 Gaussian(); 76 46 77 virtual r_8 GaussianBoxMuller(); 47 78 virtual r_8 GaussianSNorm(); … … 59 90 // --- Le tirage sur une distribution de poisson 60 91 /*! \brief Return a random number following a poisson distribution with mean mu */ 61 inline uint_8 Poisson(double mu, double mumax) 62 { 63 if(usepoisson_==0) return PoissonSimple(mu,mumax); 64 if(usepoisson_==1) return PoissonAhrens(mu); 65 return PoissonSimple(mu,mumax); 66 } 92 virtual uint_8 Poisson(double mu, double mumax=-1); 67 93 virtual uint_8 PoissonSimple(double mu, double mumax=-1); 68 94 virtual uint_8 PoissonAhrens(double mu); 69 95 70 // --- Le tirage sur une distribution exponentielle 71 inline r_8 Expo() 72 { 73 if(useexpo_==0) return ExpoSimple(); 74 if(useexpo_==1) return ExpoAhrens(); 75 return ExpoSimple(); 76 } 96 //! Return a random number with exponential distribution 97 virtual r_8 Exponential(); 98 inline r_8 Expo() { return Exponential(); } 99 77 100 virtual r_8 ExpoSimple(); 78 101 virtual r_8 ExpoAhrens(); … … 87 110 {double r=-log(1.-Next()); return sig*sqrt(2.*r);} 88 111 112 //! Return the pointer to the default global RandomGenerator object 113 static inline RandomGeneratorInterface* GetGlobalRandGenP() 114 { return gl_rndgen_p; } 115 116 // Permet de definir l'instance global du generateur aleatoire 117 static void SetGlobalRandGenP(RandomGeneratorInterface* rgp); 118 89 119 protected: 90 120 //! Return a random number in [0,1] 91 121 virtual r_8 Next(); 92 uint_2 usegaussian_, usepoisson_, useexpo_; 93 122 123 // Selection de type d'algo pour les aleatoires autres que plates 124 GaussianGenAlgo usegaussian_; 125 PoissonGenAlgo usepoisson_; 126 ExponentialGenAlgo useexpo_; 127 128 // Instance global d'un generateur par defaut 129 static RandomGeneratorInterface* gl_rndgen_p; 94 130 }; 95 131 -
trunk/SophyaLib/BaseTools/randr48.cc
r3602 r3604 157 157 } 158 158 159 159 160 void ThSDR48RandGen::GenSeq(void) 160 161 { … … 163 164 ths_rand->unlock(); 164 165 idx_ = 0; 166 } 167 168 r_8 ThSDR48RandGen::Next() 169 { 170 if (rseq_.Size() == 0) return drand48(); 171 else { 172 if(idx_==rseq_.Size()) GenSeq(); 173 return(rseq_(idx_++)); 174 } 165 175 } 166 176 -
trunk/SophyaLib/BaseTools/randr48.h
r3602 r3604 16 16 namespace SOPHYA { 17 17 18 //! Pseudorandom number generator class using dSFMT code18 //! Implementation of the RandomGeneratorInterface class using drand48() functions 19 19 class DR48RandGen : public RandomGeneratorInterface { 20 20 … … 39 39 //-------------------------------------------------------------------------------- 40 40 41 //! Version thread-safe de RandomGeneratorInterface avec drand48() functions 41 42 class ThSDR48RandGen : public DR48RandGen { 42 43 … … 62 63 // ---- protected methods 63 64 void GenSeq(); 64 inline r_8 Next() 65 { 66 if (rseq_.Size() == 0) return drand48(); 67 else { 68 if(idx_==rseq_.Size()) GenSeq(); 69 return(rseq_(idx_++)); 70 } 71 } 65 virtual r_8 Next(); 66 72 67 // Non thread-safe version of Init() and GetSeed() 73 68 void SetSeed_P(uint_2 seed[3]); -
trunk/SophyaLib/BaseTools/sophyainit.cc
r3602 r3604 23 23 24 24 #include "sversion.h" 25 26 #include "randr48.h" 25 27 26 28 #include <iostream> … … 34 36 // - Nettoyage machdefs_mkmf.h 35 37 // - Ajout classe ThSafeOp ---> NDataBlock<T> Sw/SegDataBlock<T> ThreadSafe 36 // Module version number - 2.15 , Oct08 37 #define MOD_VERS 2.15 38 // Module version number - 2.15 , Oct08 39 // - Extension / ajout de classes generateur aleatoires (Mersenne-Twister ...) 40 // Module version number - 2.20 , Avr09 41 #define MOD_VERS 2.20 38 42 39 43 // Pour garder la liste des modules et leurs numeros de version … … 64 68 65 69 ModListP = new map<string, double>; 66 70 71 // --- Les generateurs aleatoires ... 72 DR48RandGen* grgp = new DR48RandGen; 73 RandomGeneratorInterface::SetGlobalRandGenP(grgp); 67 74 68 75 // Initialisation des mecanismes PPF I/O -
trunk/SophyaLib/BaseTools/sversion.h
r3572 r3604 3 3 4 4 #define SOPHYA_VERSION 2.1 5 #define SOPHYA_REVISION 3 06 #define SOPHYA_TAG "V_ Fev2009"5 #define SOPHYA_REVISION 35 6 #define SOPHYA_TAG "V_Avr2009" 7 7 8 8 #endif
Note:
See TracChangeset
for help on using the changeset viewer.