exponential_distribution.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/base/config.h"
  22. #include "absl/meta/type_traits.h"
  23. #include "absl/random/internal/fast_uniform_bits.h"
  24. #include "absl/random/internal/generate_real.h"
  25. #include "absl/random/internal/iostream_state_saver.h"
  26. namespace absl {
  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. public:
  34. using result_type = RealType;
  35. class param_type {
  36. public:
  37. using distribution_type = exponential_distribution;
  38. explicit param_type(result_type lambda = 1) : lambda_(lambda) {
  39. assert(lambda > 0);
  40. neg_inv_lambda_ = -result_type(1) / lambda_;
  41. }
  42. result_type lambda() const { return lambda_; }
  43. friend bool operator==(const param_type& a, const param_type& b) {
  44. return a.lambda_ == b.lambda_;
  45. }
  46. friend bool operator!=(const param_type& a, const param_type& b) {
  47. return !(a == b);
  48. }
  49. private:
  50. friend class exponential_distribution;
  51. result_type lambda_;
  52. result_type neg_inv_lambda_;
  53. static_assert(
  54. std::is_floating_point<RealType>::value,
  55. "Class-template absl::exponential_distribution<> must be parameterized "
  56. "using a floating-point type.");
  57. };
  58. exponential_distribution() : exponential_distribution(1) {}
  59. explicit exponential_distribution(result_type lambda) : param_(lambda) {}
  60. explicit exponential_distribution(const param_type& p) : param_(p) {}
  61. void reset() {}
  62. // Generating functions
  63. template <typename URBG>
  64. result_type operator()(URBG& g) { // NOLINT(runtime/references)
  65. return (*this)(g, param_);
  66. }
  67. template <typename URBG>
  68. result_type operator()(URBG& g, // NOLINT(runtime/references)
  69. const param_type& p);
  70. param_type param() const { return param_; }
  71. void param(const param_type& p) { param_ = p; }
  72. result_type(min)() const { return 0; }
  73. result_type(max)() const {
  74. return std::numeric_limits<result_type>::infinity();
  75. }
  76. result_type lambda() const { return param_.lambda(); }
  77. friend bool operator==(const exponential_distribution& a,
  78. const exponential_distribution& b) {
  79. return a.param_ == b.param_;
  80. }
  81. friend bool operator!=(const exponential_distribution& a,
  82. const exponential_distribution& b) {
  83. return a.param_ != b.param_;
  84. }
  85. private:
  86. param_type param_;
  87. random_internal::FastUniformBits<uint64_t> fast_u64_;
  88. };
  89. // --------------------------------------------------------------------------
  90. // Implementation details follow
  91. // --------------------------------------------------------------------------
  92. template <typename RealType>
  93. template <typename URBG>
  94. typename exponential_distribution<RealType>::result_type
  95. exponential_distribution<RealType>::operator()(
  96. URBG& g, // NOLINT(runtime/references)
  97. const param_type& p) {
  98. using random_internal::GenerateNegativeTag;
  99. using random_internal::GenerateRealFromBits;
  100. using real_type =
  101. absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
  102. const result_type u = GenerateRealFromBits<real_type, GenerateNegativeTag,
  103. false>(fast_u64_(g)); // U(-1, 0)
  104. // log1p(-x) is mathematically equivalent to log(1 - x) but has more
  105. // accuracy for x near zero.
  106. return p.neg_inv_lambda_ * std::log1p(u);
  107. }
  108. template <typename CharT, typename Traits, typename RealType>
  109. std::basic_ostream<CharT, Traits>& operator<<(
  110. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  111. const exponential_distribution<RealType>& x) {
  112. auto saver = random_internal::make_ostream_state_saver(os);
  113. os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
  114. os << x.lambda();
  115. return os;
  116. }
  117. template <typename CharT, typename Traits, typename RealType>
  118. std::basic_istream<CharT, Traits>& operator>>(
  119. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  120. exponential_distribution<RealType>& x) { // NOLINT(runtime/references)
  121. using result_type = typename exponential_distribution<RealType>::result_type;
  122. using param_type = typename exponential_distribution<RealType>::param_type;
  123. result_type lambda;
  124. auto saver = random_internal::make_istream_state_saver(is);
  125. lambda = random_internal::read_floating_point<result_type>(is);
  126. if (!is.fail()) {
  127. x.param(param_type(lambda));
  128. }
  129. return is;
  130. }
  131. ABSL_NAMESPACE_END
  132. } // namespace absl
  133. #endif // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_