Changeset 718 for trunk/yat/random


Ignore:
Timestamp:
Dec 26, 2006, 10:56:26 AM (15 years ago)
Author:
Jari Häkkinen
Message:

Addresses #170.

Location:
trunk/yat/random
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/random/random.cc

    r706 r718  
    3434  RNG* RNG::instance_ = NULL;
    3535
    36 
    3736  RNG::RNG(void)
    3837  {
     
    5049
    5150 
     51  u_long RNG::max(void) const
     52  {
     53    return gsl_rng_max(rng_);
     54  }
     55
     56
     57  u_long RNG::min(void) const
     58  {
     59    return gsl_rng_min(rng_);
     60  }
     61
     62
     63  std::string RNG::name(void) const
     64  {
     65    return gsl_rng_name(rng_);
     66  }
     67
     68
     69  const gsl_rng* RNG::rng(void) const
     70  {
     71    return rng_;
     72  }
     73
     74
     75  void RNG::seed(u_long s) const
     76  {
     77    gsl_rng_set(rng_,s);
     78  }
     79
     80
    5281  u_long RNG::seed_from_devurandom(void)
    5382  {
     
    72101  }
    73102
     103  // --------------------- RNG_state ----------------------------------
    74104
    75105  RNG_state::RNG_state(const RNG* rng)
     
    85115  }
    86116
     117  const gsl_rng* RNG_state::rng(void) const
     118  {
     119    return rng_;
     120  }
     121
    87122  // --------------------- Discrete distribtuions ---------------------
    88123
     
    95130  Discrete::~Discrete(void)
    96131  {
     132  }
     133
     134
     135  void Discrete::seed(u_long s) const
     136  {
     137    rng_->seed(s);
    97138  }
    98139
     
    116157
    117158
     159  u_long DiscreteGeneral::operator()(void) const
     160  {
     161    return gsl_ran_discrete(rng_->rng(), gen_);
     162  }
     163
     164
     165  DiscreteUniform::DiscreteUniform(void)
     166    : range_(rng_->max())
     167  {
     168  }
     169
     170  DiscreteUniform::DiscreteUniform(const u_long n)
     171    : range_(n)
     172  {
     173    if (range_>rng_->max())
     174      range_=rng_->max();
     175  }
     176
     177
     178  u_long DiscreteUniform::operator()(void) const
     179  {
     180    return gsl_rng_uniform_int(rng_->rng(), range_);
     181  }
     182
     183
     184  u_long DiscreteUniform::operator()(const u_long n) const
     185  {
     186    return gsl_rng_uniform_int(rng_->rng(), n);
     187  }
     188
     189
    118190  Poisson::Poisson(const double m)
    119191    : m_(m)
     
    121193  }
    122194
     195  u_long Poisson::operator()(void) const
     196  {
     197    return gsl_ran_poisson(rng_->rng(), m_);
     198  }
     199
     200
     201  u_long Poisson::operator()(const double m) const
     202  {
     203    return gsl_ran_poisson(rng_->rng(), m);
     204  }
     205
    123206  // --------------------- Continuous distribtuions ---------------------
    124207
     
    134217
    135218
     219  void Continuous::seed(u_long s) const
     220  {
     221    rng_->seed(s);
     222  }
     223
     224
    136225  ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist)
    137226    : discrete_(DiscreteGeneral(hist)), hist_(hist)
     
    140229
    141230
     231  double ContinuousGeneral::operator()(void) const
     232  {
     233    return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing();
     234  }
     235
     236  double ContinuousUniform::operator()(void) const
     237  {
     238    return gsl_rng_uniform(rng_->rng());
     239  }
     240
     241
    142242  Exponential::Exponential(const double m)
    143243    : m_(m)
     
    146246
    147247
     248  double Exponential::operator()(void) const
     249  {
     250    return gsl_ran_exponential(rng_->rng(), m_);
     251  }
     252
     253
     254  double Exponential::operator()(const double m) const
     255  {
     256    return gsl_ran_exponential(rng_->rng(), m);
     257  }
     258
     259
    148260  Gaussian::Gaussian(const double s, const double m)
    149261    : m_(m), s_(s)
     
    151263  }
    152264
     265
     266  double Gaussian::operator()(void) const
     267  {
     268    return gsl_ran_gaussian(rng_->rng(), s_)+m_;
     269  }
     270
     271
     272  double Gaussian::operator()(const double s) const
     273  {
     274    return gsl_ran_gaussian(rng_->rng(), s);
     275  }
     276
     277
     278  double Gaussian::operator()(const double s, const double m) const
     279  {
     280    return gsl_ran_gaussian(rng_->rng(), s)+m;
     281  }
     282
    153283}}} // of namespace random, yat, and theplu
  • trunk/yat/random/random.h

    r706 r718  
    9898    /// generator can return.
    9999    ///
    100     inline u_long max(void) const { return gsl_rng_max(rng_); }
     100    u_long max(void) const;
    101101
    102102    ///
     
    104104    /// generator can return.
    105105    ///
    106     inline u_long min(void) const { return gsl_rng_min(rng_); }
     106    u_long min(void) const;
    107107
    108108    ///
    109109    /// @brief Returns the name of the random number generator
    110110    ///
    111     inline std::string name(void) const { return gsl_rng_name(rng_); }
     111    std::string name(void) const;
    112112
    113113    ///
    114114    /// @return const pointer to underlying GSL random generator.
    115115    ///
    116     inline const gsl_rng* rng(void) const { return rng_; }
     116    const gsl_rng* rng(void) const;
    117117
    118118    ///
     
    125125    /// @see seed_from_devurandom
    126126    ///
    127     inline void seed(u_long s) const { gsl_rng_set(rng_,s); }
     127    void seed(u_long s) const;
    128128
    129129    ///
     
    168168    /// @return const pointer to underlying GSL random generator.
    169169    ///
    170     inline const gsl_rng* rng(void) const { return rng_; }
    171 
     170    const gsl_rng* rng(void) const;
    172171
    173172  private:
     
    209208    /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed
    210209    ///
    211     inline void seed(u_long s) const { rng_->seed(s); }
     210    void seed(u_long s) const;
    212211
    213212    ///
     
    256255    /// @return A random number.
    257256    ///
    258     inline u_long
    259     operator()(void) const { return gsl_ran_discrete(rng_->rng(), gen_); }
     257    u_long operator()(void) const;
    260258
    261259  private:
     
    281279    /// @brief Default constructor.
    282280    ///
    283     DiscreteUniform(void) : range_(rng_->max()) {}
     281    DiscreteUniform(void);
    284282
    285283    ///
     
    296294    /// functionality will not return).
    297295    ///
    298     DiscreteUniform(const u_long n) : range_(n)
    299     { if ( range_>rng_->max() ) range_=rng_->max(); }
     296    DiscreteUniform(const u_long n);
    300297
    301298    ///
     
    304301    /// likely. n is set in constructor.
    305302    ///
    306     inline u_long
    307     operator()(void) const { return gsl_rng_uniform_int(rng_->rng(), range_); }
     303    u_long operator()(void) const;
    308304
    309305    ///
     
    312308    /// likely.
    313309    ///
    314     inline u_long operator()(const u_long n) const
    315     { return gsl_rng_uniform_int(rng_->rng(), n); }
     310    u_long operator()(const u_long n) const;
    316311
    317312  private:
     
    347342    /// @return A Poisson distributed number.
    348343    ///
    349     inline u_long
    350     operator()(void) const { return gsl_ran_poisson(rng_->rng(), m_); }
    351 
    352     ///
    353     /// @return A Poisson distributed number with expectation value \a
    354     /// m
     344    u_long operator()(void) const;
     345
     346    ///
     347    /// @return A Poisson distributed number with expectation value
     348    /// \a m
    355349    ///
    356350    /// @note this operator ignores parameters set in Constructor
    357351    ///
    358     inline u_long
    359     operator()(const double m) const { return gsl_ran_poisson(rng_->rng(), m); }
     352    u_long operator()(const double m) const;
    360353
    361354  private:
     
    393386    /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed
    394387    ///
    395     inline void seed(u_long s) const { rng_->seed(s); }
     388    void seed(u_long s) const;
    396389
    397390    ///
     
    414407  };
    415408
     409  // ContinuousUniform is declared before ContinuousGeneral to avoid
     410  // forward declaration
    416411  ///
    417412  /// @brief Uniform distribution
     
    427422  {
    428423  public:
    429     inline double operator()(void) const { return gsl_rng_uniform(rng_->rng());}
     424    double operator()(void) const;
    430425  };
    431426
     
    451446    /// @return A random number.
    452447    ///
    453     inline double operator()(void) const
    454     { return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing(); }
     448    double operator()(void) const;
    455449
    456450  private:
     
    476470    /// @param m is the expectation value of the distribution.
    477471    ///
    478     inline Exponential(const double m=1);
     472    Exponential(const double m=1);
    479473
    480474    ///
    481475    /// @return A random number from exponential distribution.
    482476    ///
    483     inline double
    484     operator()(void) const { return gsl_ran_exponential(rng_->rng(), m_); }
     477    double operator()(void) const;
    485478
    486479    ///
     
    490483    /// @note This operator ignores parameters given in constructor.
    491484    ///
    492     inline double operator()(const double m) const
    493     { return gsl_ran_exponential(rng_->rng(), m); }
     485    double operator()(const double m) const;
    494486
    495487  private:
     
    524516    /// @return A random Gaussian number
    525517    ///
    526     inline double
    527     operator()(void) const { return gsl_ran_gaussian(rng_->rng(), s_)+m_; }
     518    double operator()(void) const;
    528519
    529520    ///
     
    533524    /// @note this operator ignores parameters given in Constructor
    534525    ///
    535     inline double
    536     operator()(const double s) const { return gsl_ran_gaussian(rng_->rng(), s); }
     526    double operator()(const double s) const;
    537527
    538528    ///
     
    542532    /// @note this operator ignores parameters given in Constructor
    543533    ///
    544     inline double operator()(const double s, const double m) const
    545     { return gsl_ran_gaussian(rng_->rng(), s)+m; }
     534    double operator()(const double s, const double m) const;
    546535
    547536  private:
Note: See TracChangeset for help on using the changeset viewer.