Changeset 358


Ignore:
Timestamp:
Aug 3, 2005, 6:09:44 PM (17 years ago)
Author:
Peter
Message:

cleaned up in random_singleton

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/gslapi/utility.cc

    r301 r358  
    3434    vector vec(invec);
    3535    utility::random_singleton*
    36       rnd=utility::random_singleton::get_instance();
     36      rnd=utility::random_singleton::instance();
    3737    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);
    3939      invec[i]=vec(index);
    4040      vec(index)=vec(vec.size()-i-1);
  • trunk/lib/utility/random_singleton.cc

    r308 r358  
    3232
    3333
    34 void random_singleton::set_seed( const int& seed )
     34void random_singleton::seed( const int& seed )
    3535{
    3636  if( seed < 0 )
     
    4545
    4646
    47 random_singleton* random_singleton::get_instance( int seed )
     47random_singleton* random_singleton::instance( int seed )
    4848{
    4949  if( !singleobj_ )
    5050    {
    5151     singleobj_ = new random_singleton();
    52      singleobj_->set_seed( seed );
     52     singleobj_->seed( seed );
    5353    }
    5454  return singleobj_;
  • trunk/lib/utility/random_singleton.h

    r312 r358  
    1515namespace utility { 
    1616
    17   /**
    18      Class defining interface for different random
    19      stuff.
    20   */
     17  ///
     18  /// Class defining interface for different random stuff.
     19  ///
    2120  class random_singleton
    2221  {
     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
    23139  private:
    24     /**
    25       pointer to an instance of the class.
    26     */
     140    ///
     141    /// pointer to an instance of the class.
     142    ///
    27143    static random_singleton *singleobj_;
    28144
    29     /**
    30       GSL stuff
    31     */
     145    ///
     146    /// GSL stuff
     147    ///
    32148    gsl_rng* r_;
    33149    gsl_ran_discrete_t* gen_;  // general purpose distribution
    34150
    35     /**
    36        Private constructor. Only way to
    37        create object is via get_instance().
    38     */
     151    ///
     152    /// Private constructor. Only way to create object is via
     153    /// instance().
     154    ///
    39155    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  };
    156157 
    157158  struct my_uniform_rng
     
    159160  public:
    160161    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);
    162163    }
    163164  };
    164165
    165 double random_singleton::get_exponential( const double& mu ) const
     166double random_singleton::exponential( const double& mu ) const
    166167{
    167168  return gsl_ran_exponential( r_, mu );
     
    169170
    170171
    171 double random_singleton::get_gaussian( const double& sigma ) const
     172double random_singleton::gaussian( const double& sigma ) const
    172173{
    173174  return gsl_ran_gaussian( r_, sigma );
     
    175176
    176177
    177 inline std::string random_singleton::get_generator_type() const
     178inline std::string random_singleton::generator_type() const
    178179{
    179180  return static_cast<std::string>( gsl_rng_name( r_ ) );
     
    181182
    182183
    183 size_t random_singleton::get_rnd_discrete()
     184size_t random_singleton::rnd_discrete()
    184185{
    185186  return gsl_ran_discrete( r_, gen_ );
     
    187188
    188189
    189 int random_singleton::get_seed() const
     190int random_singleton::seed() const
    190191{
    191192  return gsl_rng_default_seed;
     
    193194
    194195
    195 double random_singleton::get_uniform_double() const
     196double random_singleton::uniform_double() const
    196197{
    197198  return gsl_rng_uniform( r_ );
     
    199200
    200201
    201 u_long random_singleton::get_uniform_int(const u_long n) const
     202u_long random_singleton::uniform_int(const u_long n) const
    202203{
    203204    return ( n ? gsl_rng_uniform_int(r_,n) : gsl_rng_get( r_ ));
     
    205206
    206207
    207 u_long random_singleton::get_uniform_min() const
     208u_long random_singleton::uniform_min() const
    208209{
    209210  return gsl_rng_min( r_ );
     
    211212
    212213
    213 u_long random_singleton::get_uniform_max() const
     214u_long random_singleton::uniform_max() const
    214215{
    215216  return gsl_rng_max( r_ );
     
    217218
    218219
    219 void random_singleton::set_general_distribution_prob( const size_t& k,
    220                   const double* p )
     220void random_singleton::general_distribution_prob( const size_t& k,
     221                                                  const double* p )
    221222{
    222223  gen_ = gsl_ran_discrete_preproc( k, p );
  • trunk/test/rnd_test.cc

    r301 r358  
    1010{
    1111  utility::random_singleton* my_rnd;
    12   my_rnd = utility::random_singleton::get_instance( -1 );
     12  my_rnd = utility::random_singleton::instance( -1 );
    1313 
    1414  double* prob = new double[ 5 ];
     
    1818  prob[ 3 ] = 0.2;
    1919  prob[ 4 ] = 0.2;
    20   my_rnd->set_general_distribution_prob( 5, prob );
     20  my_rnd->general_distribution_prob( 5, prob );
    2121  delete prob;
    2222
     
    2929
    3030  for( size_t i = 0; i < 100; ++i )
    31     antal[ my_rnd->get_rnd_discrete() ]++;
     31    antal[ my_rnd->rnd_discrete() ]++;
    3232
    3333  delete my_rnd;
  • trunk/test/svd_test.cc

    r301 r358  
    2323  // accepted error, Jari: should be picked up from GSL
    2424  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);
    2626
    2727  // initialise a random test-matrix
     
    2929  for (size_t i=0; i<m; ++i)
    3030    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();
    3232
    3333  utility::SVD svd(A);
Note: See TracChangeset for help on using the changeset viewer.