COIN-OR::LEMON - Graph Library

Changeset 517:2b6d5d22bb23 in lemon-1.2 for lemon/random.h


Ignore:
Timestamp:
02/20/09 22:37:19 (11 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Parents:
516:dab9e610e37d (diff), 488:b1ef32ab39f3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Phase:
public
Message:

Merge

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lemon/random.h

    r487 r517  
    33 * This file is a part of LEMON, a generic C++ optimization library.
    44 *
    5  * Copyright (C) 2003-2008
     5 * Copyright (C) 2003-2009
    66 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    77 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     
    531531    /// @{
    532532
    533     ///\name Initialization
    534     ///
    535     /// @{
    536 
    537533    /// \brief Default constructor
    538534    ///
     
    681677    }
    682678
    683     /// @}
    684 
    685     ///\name Uniform distributions
    686     ///
    687     /// @{
    688 
    689679    /// \brief Returns a random real number from the range [0, 1)
    690680    ///
     
    741731      return _random_bits::IntConversion<Number, Word>::convert(core);
    742732    }
    743 
    744     /// @}
    745733
    746734    unsigned int uinteger() {
     
    777765    ///\name Non-uniform distributions
    778766    ///
    779 
    780767    ///@{
    781768
    782     /// \brief Returns a random bool
     769    /// \brief Returns a random bool with given probability of true result.
    783770    ///
    784771    /// It returns a random bool with given probability of true result.
     
    787774    }
    788775
    789     /// Standard Gauss distribution
    790 
    791     /// Standard Gauss distribution.
     776    /// Standard normal (Gauss) distribution
     777
     778    /// Standard normal (Gauss) distribution.
    792779    /// \note The Cartesian form of the Box-Muller
    793780    /// transformation is used to generate a random normal distribution.
     
    802789      return std::sqrt(-2*std::log(S)/S)*V1;
    803790    }
    804     /// Gauss distribution with given mean and standard deviation
    805 
    806     /// Gauss distribution with given mean and standard deviation.
     791    /// Normal (Gauss) distribution with given mean and standard deviation
     792
     793    /// Normal (Gauss) distribution with given mean and standard deviation.
    807794    /// \sa gauss()
    808795    double gauss(double mean,double std_dev)
    809796    {
    810797      return gauss()*std_dev+mean;
     798    }
     799
     800    /// Lognormal distribution
     801
     802    /// Lognormal distribution. The parameters are the mean and the standard
     803    /// deviation of <tt>exp(X)</tt>.
     804    ///
     805    double lognormal(double n_mean,double n_std_dev)
     806    {
     807      return std::exp(gauss(n_mean,n_std_dev));
     808    }
     809    /// Lognormal distribution
     810
     811    /// Lognormal distribution. The parameter is an <tt>std::pair</tt> of
     812    /// the mean and the standard deviation of <tt>exp(X)</tt>.
     813    ///
     814    double lognormal(const std::pair<double,double> &params)
     815    {
     816      return std::exp(gauss(params.first,params.second));
     817    }
     818    /// Compute the lognormal parameters from mean and standard deviation
     819
     820    /// This function computes the lognormal parameters from mean and
     821    /// standard deviation. The return value can direcly be passed to
     822    /// lognormal().
     823    std::pair<double,double> lognormalParamsFromMD(double mean,
     824                                                   double std_dev)
     825    {
     826      double fr=std_dev/mean;
     827      fr*=fr;
     828      double lg=std::log(1+fr);
     829      return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
     830    }
     831    /// Lognormal distribution with given mean and standard deviation
     832
     833    /// Lognormal distribution with given mean and standard deviation.
     834    ///
     835    double lognormalMD(double mean,double std_dev)
     836    {
     837      return lognormal(lognormalParamsFromMD(mean,std_dev));
    811838    }
    812839
     
    914941    ///\name Two dimensional distributions
    915942    ///
    916 
    917943    ///@{
    918944
     
    931957      return dim2::Point<double>(V1,V2);
    932958    }
    933     /// A kind of two dimensional Gauss distribution
     959    /// A kind of two dimensional normal (Gauss) distribution
    934960
    935961    /// This function provides a turning symmetric two-dimensional distribution.
  • lemon/random.h

    r515 r517  
    345345    };
    346346
    347     template <typename Result, int exp, bool pos = (exp >= 0)>
     347    template <typename Result, int exp>
    348348    struct ShiftMultiplier {
    349       static const Result multiplier() {
    350         Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
    351         res *= res;
    352         if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
    353         return res;
    354       }
    355     };
    356 
    357     template <typename Result, int exp>
    358     struct ShiftMultiplier<Result, exp, false> {
    359349      static const Result multiplier() {
    360350        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
     
    366356
    367357    template <typename Result>
    368     struct ShiftMultiplier<Result, 0, true> {
     358    struct ShiftMultiplier<Result, 0> {
    369359      static const Result multiplier() {
    370360        return static_cast<Result>(1.0);
     
    373363
    374364    template <typename Result>
    375     struct ShiftMultiplier<Result, -20, true> {
     365    struct ShiftMultiplier<Result, 20> {
    376366      static const Result multiplier() {
    377367        return static_cast<Result>(1.0/1048576.0);
     
    380370
    381371    template <typename Result>
    382     struct ShiftMultiplier<Result, -32, true> {
     372    struct ShiftMultiplier<Result, 32> {
    383373      static const Result multiplier() {
    384         return static_cast<Result>(1.0/424967296.0);
     374        return static_cast<Result>(1.0/4294967296.0);
    385375      }
    386376    };
    387377
    388378    template <typename Result>
    389     struct ShiftMultiplier<Result, -53, true> {
     379    struct ShiftMultiplier<Result, 53> {
    390380      static const Result multiplier() {
    391381        return static_cast<Result>(1.0/9007199254740992.0);
     
    394384
    395385    template <typename Result>
    396     struct ShiftMultiplier<Result, -64, true> {
     386    struct ShiftMultiplier<Result, 64> {
    397387      static const Result multiplier() {
    398388        return static_cast<Result>(1.0/18446744073709551616.0);
     
    414404
    415405      static Result convert(RandomCore<Word>& rnd) {
    416         return Shifting<Result, - shift - rest>::
     406        return Shifting<Result, shift + rest>::
    417407          shift(static_cast<Result>(rnd() >> (bits - rest)));
    418408      }
     
    424414
    425415      static Result convert(RandomCore<Word>& rnd) {
    426         return Shifting<Result, - shift - bits>::
     416        return Shifting<Result, shift + bits>::
    427417          shift(static_cast<Result>(rnd())) +
    428418          RealConversion<Result, Word, rest-bits, shift + bits>::
Note: See TracChangeset for help on using the changeset viewer.