GSLRndmEngines.cxx

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLRndmEngines.cxx 37441 2010-12-09 16:27:54Z moneta $
00002 // Authors: L. Moneta, A. Zsenei   08/2005 
00003 
00004  /**********************************************************************
00005   *                                                                    *
00006   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
00007   *                                                                    *
00008   * This library is free software; you can redistribute it and/or      *
00009   * modify it under the terms of the GNU General Public License        *
00010   * as published by the Free Software Foundation; either version 2     *
00011   * of the License, or (at your option) any later version.             *
00012   *                                                                    *
00013   * This library is distributed in the hope that it will be useful,    *
00014   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00015   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00016   * General Public License for more details.                           *
00017   *                                                                    *
00018   * You should have received a copy of the GNU General Public License  *
00019   * along with this library (see file COPYING); if not, write          *
00020   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022   *                                                                    *
00023   **********************************************************************/
00024 
00025 // Header file for class GSLRandom
00026 // 
00027 // Created by: moneta  at Sun Nov 21 16:26:03 2004
00028 // 
00029 // Last update: Sun Nov 21 16:26:03 2004
00030 // 
00031 
00032 
00033 
00034 // need to be included later
00035 #include <time.h>
00036 #include <stdlib.h>
00037 #include <cassert>
00038 
00039 #include "gsl/gsl_rng.h"
00040 #include "gsl/gsl_randist.h"
00041 
00042 
00043 #include "Math/GSLRndmEngines.h"
00044 #include "GSLRngWrapper.h"
00045 
00046 extern double gsl_ran_gaussian_acr(  const gsl_rng * r, const double sigma);
00047 
00048 //#include <iostream>
00049 
00050 namespace ROOT {
00051 namespace Math {
00052 
00053 
00054 
00055 
00056 
00057   // default constructor (need to call set type later)
00058    GSLRandomEngine::GSLRandomEngine() : 
00059       fRng(0 ),
00060       fCurTime(0)  
00061   { } 
00062 
00063    // constructor from external rng
00064    // internal generator will be managed or not depending on 
00065    // how the GSLRngWrapper is created
00066    GSLRandomEngine::GSLRandomEngine( GSLRngWrapper * rng) : 
00067       fRng(new GSLRngWrapper(*rng) ),
00068       fCurTime(0)
00069    {}
00070 
00071 //    // constructor from external rng
00072 //    GSLRandomEngine( GSLRngWrapper & rng) : 
00073 //       fRng(new GSLRngWrapper(rng) ),
00074 //       fCurTime(0)
00075 //    {}
00076 
00077    GSLRandomEngine::~GSLRandomEngine() {
00078       // destructor : call terminate if not yet called
00079       if (fRng) Terminate();
00080    }
00081 
00082 
00083    void GSLRandomEngine::Initialize() { 
00084       // initialize the generator by allocating the GSL object
00085       // if type was not passed create with default generator
00086       if (!fRng) fRng = new GSLRngWrapper(); 
00087       fRng->Allocate(); 
00088    }
00089 
00090    void GSLRandomEngine::Terminate() { 
00091       // terminate the generator by freeing the GSL object
00092       if (!fRng) return;
00093       fRng->Free();
00094       delete fRng; 
00095       fRng = 0; 
00096    }
00097 
00098 
00099    double GSLRandomEngine::operator() () const { 
00100       // generate random between 0 and 1. 
00101       // 0 is excluded 
00102       return gsl_rng_uniform_pos(fRng->Rng() ); 
00103    }
00104 
00105 
00106    unsigned int GSLRandomEngine::RndmInt(unsigned int max) const { 
00107       // generate a random integer number between 0  and MAX
00108       return gsl_rng_uniform_int( fRng->Rng(), max );
00109    }
00110 
00111 //    int GSLRandomEngine::GetMin() { 
00112 //       // return minimum integer value used in RndmInt
00113 //       return gsl_rng_min( fRng->Rng() );
00114 //    }
00115 
00116 //    int GSLRandomEngine::GetMax() { 
00117 //       // return maximum integr value used in RndmInt
00118 //       return gsl_rng_max( fRng->Rng() );
00119 //    }
00120 
00121    void GSLRandomEngine::RandomArray(double * begin, double * end )  const { 
00122       // generate array of randoms betweeen 0 and 1. 0 is excluded 
00123       // specialization for double * (to be faster) 
00124       for ( double * itr = begin; itr != end; ++itr ) { 
00125          *itr = gsl_rng_uniform_pos(fRng->Rng() ); 
00126       }
00127    }
00128 
00129    void GSLRandomEngine::SetSeed(unsigned int seed) const  { 
00130       // set the seed, if = 0then the seed is set randomly using an std::rand()
00131       // seeded with the current time. Be carefuk in case the current time is 
00132       // the same in consecutive calls 
00133       if (seed == 0) { 
00134          // use like in root (use time)
00135          time_t curtime;  
00136          time(&curtime); 
00137          unsigned int ct = static_cast<unsigned int>(curtime);
00138          if (ct != fCurTime) { 
00139             fCurTime = ct; 
00140             // set the seed for rand
00141             srand(ct); 
00142          }
00143          seed = rand();
00144       } 
00145 
00146       assert(fRng);
00147       gsl_rng_set(fRng->Rng(), seed ); 
00148    }
00149 
00150    std::string GSLRandomEngine::Name() const { 
00151       //----------------------------------------------------
00152       assert ( fRng != 0); 
00153       assert ( fRng->Rng() != 0 ); 
00154       return std::string( gsl_rng_name( fRng->Rng() ) ); 
00155    }
00156 
00157    unsigned int GSLRandomEngine::Size() const { 
00158       //----------------------------------------------------
00159       assert (fRng != 0);
00160       return gsl_rng_size( fRng->Rng() ); 
00161    }
00162 
00163 
00164    // Random distributions
00165   
00166    double GSLRandomEngine::GaussianZig(double sigma)  const
00167    {
00168       // Gaussian distribution. Use fast ziggurat algorithm implemented since GSL 1.8 
00169       return gsl_ran_gaussian_ziggurat(  fRng->Rng(), sigma);
00170    }
00171 
00172    double GSLRandomEngine::Gaussian(double sigma)  const
00173    {
00174       // Gaussian distribution. Use default Box-Muller method
00175       return gsl_ran_gaussian(  fRng->Rng(), sigma);
00176    }
00177 
00178    double GSLRandomEngine::GaussianRatio(double sigma)  const
00179    {
00180       // Gaussian distribution. Use ratio method
00181       return gsl_ran_gaussian_ratio_method(  fRng->Rng(), sigma);
00182    }
00183 
00184 
00185    double GSLRandomEngine::GaussianTail(double a , double sigma) const 
00186    {
00187       // Gaussian Tail distribution: eeturn values larger than a distributed 
00188       // according to the gaussian 
00189       return gsl_ran_gaussian_tail(  fRng->Rng(), a, sigma);
00190    }
00191 
00192    void GSLRandomEngine::Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const
00193    { 
00194       // Gaussian Bivariate distribution, with correlation coefficient rho
00195       gsl_ran_bivariate_gaussian(  fRng->Rng(), sigmaX, sigmaY, rho, &x, &y);
00196    }
00197   
00198    double GSLRandomEngine::Exponential(double mu)  const
00199    {
00200       // Exponential distribution
00201       return gsl_ran_exponential(  fRng->Rng(), mu);
00202    }
00203 
00204    double GSLRandomEngine::Cauchy(double a) const
00205    {
00206       // Cauchy distribution
00207       return gsl_ran_cauchy(  fRng->Rng(), a);
00208    }
00209 
00210    double GSLRandomEngine::Landau() const
00211    {
00212       // Landau distribution
00213       return gsl_ran_landau(  fRng->Rng());
00214    }
00215 
00216    double GSLRandomEngine::Gamma(double a, double b) const 
00217    {
00218       // Gamma distribution
00219       return gsl_ran_gamma(  fRng->Rng(), a, b);
00220    }
00221 
00222    double GSLRandomEngine::LogNormal(double zeta, double sigma) const
00223    {
00224       // Log normal distribution
00225       return gsl_ran_lognormal(  fRng->Rng(), zeta, sigma);
00226    }
00227 
00228    double GSLRandomEngine::ChiSquare(double nu) const 
00229    {
00230       // Chi square distribution
00231       return gsl_ran_chisq(  fRng->Rng(), nu);
00232    }
00233 
00234 
00235    double GSLRandomEngine::FDist(double nu1, double nu2)  const
00236    {
00237       // F distribution
00238       return gsl_ran_fdist(  fRng->Rng(), nu1, nu2);
00239    }
00240 
00241    double GSLRandomEngine::tDist(double nu)  const
00242    {
00243       // t distribution
00244       return gsl_ran_tdist(  fRng->Rng(), nu);
00245    }
00246   
00247    void GSLRandomEngine::Dir2D(double &x, double &y) const 
00248    { 
00249       // generate random numbers in a 2D circle of radious 1 
00250       gsl_ran_dir_2d(  fRng->Rng(), &x, &y);
00251    }
00252 
00253    void GSLRandomEngine::Dir3D(double &x, double &y, double &z) const
00254    { 
00255       // generate random numbers in a 3D sphere of radious 1 
00256       gsl_ran_dir_3d(  fRng->Rng(), &x, &y, &z);
00257    }
00258   
00259    unsigned int GSLRandomEngine::Poisson(double mu) const
00260    { 
00261       // Poisson distribution
00262       return gsl_ran_poisson(  fRng->Rng(), mu);
00263    }
00264 
00265    unsigned int GSLRandomEngine::Binomial(double p, unsigned int n) const
00266    { 
00267       // Binomial distribution
00268       return gsl_ran_binomial(  fRng->Rng(), p, n);
00269    }
00270 
00271    unsigned int GSLRandomEngine::NegativeBinomial(double p, double n) const
00272    { 
00273       // Negative Binomial distribution
00274       return gsl_ran_negative_binomial(  fRng->Rng(), p, n);
00275    }
00276 
00277 
00278    std::vector<unsigned int>  GSLRandomEngine::Multinomial( unsigned int ntot, const std::vector<double> & p ) const
00279    { 
00280       // Multinomial distribution  return vector of integers which sum is ntot
00281       std::vector<unsigned int> ival( p.size()); 
00282       gsl_ran_multinomial(  fRng->Rng(), p.size(), ntot, &p.front(), &ival[0]);
00283       return ival; 
00284    }
00285 
00286 
00287 
00288    //----------------------------------------------------
00289    // generators 
00290    //----------------------------------------------------
00291 
00292    //----------------------------------------------------
00293    GSLRngMT::GSLRngMT() : GSLRandomEngine()
00294    {
00295       SetType(new GSLRngWrapper(gsl_rng_mt19937));
00296    }
00297 
00298 
00299    // old ranlux - equivalent to TRandom1
00300    GSLRngRanLux::GSLRngRanLux() : GSLRandomEngine() 
00301    {
00302       SetType(new GSLRngWrapper(gsl_rng_ranlux) );
00303    }
00304 
00305    // second generation of Ranlux (single precision version - luxury 1)
00306    GSLRngRanLuxS1::GSLRngRanLuxS1() : GSLRandomEngine() 
00307    {
00308       SetType(new GSLRngWrapper(gsl_rng_ranlxs1) );
00309    }
00310 
00311    // second generation of Ranlux (single precision version - luxury 2)
00312    GSLRngRanLuxS2::GSLRngRanLuxS2() : GSLRandomEngine() 
00313    {
00314       SetType(new GSLRngWrapper(gsl_rng_ranlxs2) );
00315    }
00316 
00317    // double precision  version - luxury 1 
00318    GSLRngRanLuxD1::GSLRngRanLuxD1() : GSLRandomEngine() 
00319    {
00320       SetType(new GSLRngWrapper(gsl_rng_ranlxd1) );
00321    }
00322    
00323    // double precision  version - luxury 2 
00324    GSLRngRanLuxD2::GSLRngRanLuxD2() : GSLRandomEngine() 
00325    {
00326       SetType(new GSLRngWrapper(gsl_rng_ranlxd2) );
00327    }
00328 
00329    //----------------------------------------------------
00330    GSLRngTaus::GSLRngTaus() : GSLRandomEngine() 
00331    {
00332       SetType(new GSLRngWrapper(gsl_rng_taus2) );
00333    }
00334 
00335    //----------------------------------------------------
00336    GSLRngGFSR4::GSLRngGFSR4() : GSLRandomEngine() 
00337    {
00338       SetType(new GSLRngWrapper(gsl_rng_gfsr4) );
00339    }
00340 
00341    //----------------------------------------------------
00342    GSLRngCMRG::GSLRngCMRG() : GSLRandomEngine() 
00343    {
00344       SetType(new GSLRngWrapper(gsl_rng_cmrg) );
00345    }
00346 
00347    //----------------------------------------------------
00348    GSLRngMRG::GSLRngMRG() : GSLRandomEngine() 
00349    {
00350       SetType(new GSLRngWrapper(gsl_rng_mrg) );
00351    }
00352 
00353 
00354    //----------------------------------------------------
00355    GSLRngRand::GSLRngRand() : GSLRandomEngine() 
00356    {
00357       SetType(new GSLRngWrapper(gsl_rng_rand) );
00358    }
00359 
00360    //----------------------------------------------------
00361    GSLRngRanMar::GSLRngRanMar() : GSLRandomEngine() 
00362    {
00363       SetType(new GSLRngWrapper(gsl_rng_ranmar) );
00364    }
00365 
00366    //----------------------------------------------------
00367    GSLRngMinStd::GSLRngMinStd() : GSLRandomEngine() 
00368    {
00369       SetType(new GSLRngWrapper(gsl_rng_minstd) );
00370    }
00371 
00372 
00373 
00374 
00375 
00376 } // namespace Math
00377 } // namespace ROOT
00378 
00379 
00380 

Generated on Tue Jul 5 14:34:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1