You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exponential_distribution.h 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
  15. #define ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
  16. #include <cassert>
  17. #include <cmath>
  18. #include <istream>
  19. #include <limits>
  20. #include <type_traits>
  21. #include "absl/meta/type_traits.h"
  22. #include "absl/random/internal/fast_uniform_bits.h"
  23. #include "absl/random/internal/generate_real.h"
  24. #include "absl/random/internal/iostream_state_saver.h"
  25. namespace absl
  26. {
  27. ABSL_NAMESPACE_BEGIN
  28. // absl::exponential_distribution:
  29. // Generates a number conforming to an exponential distribution and is
  30. // equivalent to the standard [rand.dist.pois.exp] distribution.
  31. template<typename RealType = double>
  32. class exponential_distribution
  33. {
  34. public:
  35. using result_type = RealType;
  36. class param_type
  37. {
  38. public:
  39. using distribution_type = exponential_distribution;
  40. explicit param_type(result_type lambda = 1) :
  41. lambda_(lambda)
  42. {
  43. assert(lambda > 0);
  44. neg_inv_lambda_ = -result_type(1) / lambda_;
  45. }
  46. result_type lambda() const
  47. {
  48. return lambda_;
  49. }
  50. friend bool operator==(const param_type& a, const param_type& b)
  51. {
  52. return a.lambda_ == b.lambda_;
  53. }
  54. friend bool operator!=(const param_type& a, const param_type& b)
  55. {
  56. return !(a == b);
  57. }
  58. private:
  59. friend class exponential_distribution;
  60. result_type lambda_;
  61. result_type neg_inv_lambda_;
  62. static_assert(
  63. std::is_floating_point<RealType>::value,
  64. "Class-template absl::exponential_distribution<> must be parameterized "
  65. "using a floating-point type."
  66. );
  67. };
  68. exponential_distribution() :
  69. exponential_distribution(1)
  70. {
  71. }
  72. explicit exponential_distribution(result_type lambda) :
  73. param_(lambda)
  74. {
  75. }
  76. explicit exponential_distribution(const param_type& p) :
  77. param_(p)
  78. {
  79. }
  80. void reset()
  81. {
  82. }
  83. // Generating functions
  84. template<typename URBG>
  85. result_type operator()(URBG& g)
  86. { // NOLINT(runtime/references)
  87. return (*this)(g, param_);
  88. }
  89. template<typename URBG>
  90. result_type operator()(URBG& g, // NOLINT(runtime/references)
  91. const param_type& p);
  92. param_type param() const
  93. {
  94. return param_;
  95. }
  96. void param(const param_type& p)
  97. {
  98. param_ = p;
  99. }
  100. result_type(min)() const
  101. {
  102. return 0;
  103. }
  104. result_type(max)() const
  105. {
  106. return std::numeric_limits<result_type>::infinity();
  107. }
  108. result_type lambda() const
  109. {
  110. return param_.lambda();
  111. }
  112. friend bool operator==(const exponential_distribution& a, const exponential_distribution& b)
  113. {
  114. return a.param_ == b.param_;
  115. }
  116. friend bool operator!=(const exponential_distribution& a, const exponential_distribution& b)
  117. {
  118. return a.param_ != b.param_;
  119. }
  120. private:
  121. param_type param_;
  122. random_internal::FastUniformBits<uint64_t> fast_u64_;
  123. };
  124. // --------------------------------------------------------------------------
  125. // Implementation details follow
  126. // --------------------------------------------------------------------------
  127. template<typename RealType>
  128. template<typename URBG>
  129. typename exponential_distribution<RealType>::result_type
  130. exponential_distribution<RealType>::operator()(
  131. URBG& g, // NOLINT(runtime/references)
  132. const param_type& p
  133. )
  134. {
  135. using random_internal::GenerateNegativeTag;
  136. using random_internal::GenerateRealFromBits;
  137. using real_type =
  138. absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
  139. const result_type u = GenerateRealFromBits<real_type, GenerateNegativeTag,
  140. false>(fast_u64_(g)); // U(-1, 0)
  141. // log1p(-x) is mathematically equivalent to log(1 - x) but has more
  142. // accuracy for x near zero.
  143. return p.neg_inv_lambda_ * std::log1p(u);
  144. }
  145. template<typename CharT, typename Traits, typename RealType>
  146. std::basic_ostream<CharT, Traits>& operator<<(
  147. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  148. const exponential_distribution<RealType>& x
  149. )
  150. {
  151. auto saver = random_internal::make_ostream_state_saver(os);
  152. os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
  153. os << x.lambda();
  154. return os;
  155. }
  156. template<typename CharT, typename Traits, typename RealType>
  157. std::basic_istream<CharT, Traits>& operator>>(
  158. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  159. exponential_distribution<RealType>& x
  160. )
  161. { // NOLINT(runtime/references)
  162. using result_type = typename exponential_distribution<RealType>::result_type;
  163. using param_type = typename exponential_distribution<RealType>::param_type;
  164. result_type lambda;
  165. auto saver = random_internal::make_istream_state_saver(is);
  166. lambda = random_internal::read_floating_point<result_type>(is);
  167. if (!is.fail())
  168. {
  169. x.param(param_type(lambda));
  170. }
  171. return is;
  172. }
  173. ABSL_NAMESPACE_END
  174. } // namespace absl
  175. #endif // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_