Changeset 564:2b6d5d22bb23 in lemon for lemon/random.h
- Timestamp:
- 02/20/09 22:37:19 (16 years ago)
- Branch:
- default
- Parents:
- 563:dab9e610e37d (diff), 518: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
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/random.h
r517 r564 3 3 * This file is a part of LEMON, a generic C++ optimization library. 4 4 * 5 * Copyright (C) 2003-200 85 * Copyright (C) 2003-2009 6 6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 7 7 * (Egervary Research Group on Combinatorial Optimization, EGRES). … … 531 531 /// @{ 532 532 533 ///\name Initialization534 ///535 /// @{536 537 533 /// \brief Default constructor 538 534 /// … … 681 677 } 682 678 683 /// @}684 685 ///\name Uniform distributions686 ///687 /// @{688 689 679 /// \brief Returns a random real number from the range [0, 1) 690 680 /// … … 741 731 return _random_bits::IntConversion<Number, Word>::convert(core); 742 732 } 743 744 /// @}745 733 746 734 unsigned int uinteger() { … … 777 765 ///\name Non-uniform distributions 778 766 /// 779 780 767 ///@{ 781 768 782 /// \brief Returns a random bool 769 /// \brief Returns a random bool with given probability of true result. 783 770 /// 784 771 /// It returns a random bool with given probability of true result. … … 787 774 } 788 775 789 /// Standard Gaussdistribution790 791 /// Standard Gaussdistribution.776 /// Standard normal (Gauss) distribution 777 778 /// Standard normal (Gauss) distribution. 792 779 /// \note The Cartesian form of the Box-Muller 793 780 /// transformation is used to generate a random normal distribution. … … 802 789 return std::sqrt(-2*std::log(S)/S)*V1; 803 790 } 804 /// Gaussdistribution with given mean and standard deviation805 806 /// Gaussdistribution 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. 807 794 /// \sa gauss() 808 795 double gauss(double mean,double std_dev) 809 796 { 810 797 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> ¶ms) 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)); 811 838 } 812 839 … … 914 941 ///\name Two dimensional distributions 915 942 /// 916 917 943 ///@{ 918 944 … … 931 957 return dim2::Point<double>(V1,V2); 932 958 } 933 /// A kind of two dimensional Gaussdistribution959 /// A kind of two dimensional normal (Gauss) distribution 934 960 935 961 /// This function provides a turning symmetric two-dimensional distribution. -
lemon/random.h
r562 r564 345 345 }; 346 346 347 template <typename Result, int exp , bool pos = (exp >= 0)>347 template <typename Result, int exp> 348 348 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> {359 349 static const Result multiplier() { 360 350 Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); … … 366 356 367 357 template <typename Result> 368 struct ShiftMultiplier<Result, 0 , true> {358 struct ShiftMultiplier<Result, 0> { 369 359 static const Result multiplier() { 370 360 return static_cast<Result>(1.0); … … 373 363 374 364 template <typename Result> 375 struct ShiftMultiplier<Result, -20, true> {365 struct ShiftMultiplier<Result, 20> { 376 366 static const Result multiplier() { 377 367 return static_cast<Result>(1.0/1048576.0); … … 380 370 381 371 template <typename Result> 382 struct ShiftMultiplier<Result, -32, true> {372 struct ShiftMultiplier<Result, 32> { 383 373 static const Result multiplier() { 384 return static_cast<Result>(1.0/42 4967296.0);374 return static_cast<Result>(1.0/4294967296.0); 385 375 } 386 376 }; 387 377 388 378 template <typename Result> 389 struct ShiftMultiplier<Result, -53, true> {379 struct ShiftMultiplier<Result, 53> { 390 380 static const Result multiplier() { 391 381 return static_cast<Result>(1.0/9007199254740992.0); … … 394 384 395 385 template <typename Result> 396 struct ShiftMultiplier<Result, -64, true> {386 struct ShiftMultiplier<Result, 64> { 397 387 static const Result multiplier() { 398 388 return static_cast<Result>(1.0/18446744073709551616.0); … … 414 404 415 405 static Result convert(RandomCore<Word>& rnd) { 416 return Shifting<Result, - shift -rest>::406 return Shifting<Result, shift + rest>:: 417 407 shift(static_cast<Result>(rnd() >> (bits - rest))); 418 408 } … … 424 414 425 415 static Result convert(RandomCore<Word>& rnd) { 426 return Shifting<Result, - shift -bits>::416 return Shifting<Result, shift + bits>:: 427 417 shift(static_cast<Result>(rnd())) + 428 418 RealConversion<Result, Word, rest-bits, shift + bits>::
Note: See TracChangeset
for help on using the changeset viewer.