• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #ifndef ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
16 #define ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
17 
18 #include <cassert>
19 #include <cmath>
20 #include <istream>
21 #include <limits>
22 #include <type_traits>
23 
24 #include "absl/meta/type_traits.h"
25 #include "absl/random/internal/fast_uniform_bits.h"
26 #include "absl/random/internal/generate_real.h"
27 #include "absl/random/internal/iostream_state_saver.h"
28 
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31 
32 // absl::exponential_distribution:
33 // Generates a number conforming to an exponential distribution and is
34 // equivalent to the standard [rand.dist.pois.exp] distribution.
35 template <typename RealType = double>
36 class exponential_distribution {
37  public:
38   using result_type = RealType;
39 
40   class param_type {
41    public:
42     using distribution_type = exponential_distribution;
43 
lambda_(lambda)44     explicit param_type(result_type lambda = 1) : lambda_(lambda) {
45       assert(lambda > 0);
46       neg_inv_lambda_ = -result_type(1) / lambda_;
47     }
48 
lambda()49     result_type lambda() const { return lambda_; }
50 
51     friend bool operator==(const param_type& a, const param_type& b) {
52       return a.lambda_ == b.lambda_;
53     }
54 
55     friend bool operator!=(const param_type& a, const param_type& b) {
56       return !(a == b);
57     }
58 
59    private:
60     friend class exponential_distribution;
61 
62     result_type lambda_;
63     result_type neg_inv_lambda_;
64 
65     static_assert(
66         std::is_floating_point<RealType>::value,
67         "Class-template absl::exponential_distribution<> must be parameterized "
68         "using a floating-point type.");
69   };
70 
exponential_distribution()71   exponential_distribution() : exponential_distribution(1) {}
72 
exponential_distribution(result_type lambda)73   explicit exponential_distribution(result_type lambda) : param_(lambda) {}
74 
exponential_distribution(const param_type & p)75   explicit exponential_distribution(const param_type& p) : param_(p) {}
76 
reset()77   void reset() {}
78 
79   // Generating functions
80   template <typename URBG>
operator()81   result_type operator()(URBG& g) {  // NOLINT(runtime/references)
82     return (*this)(g, param_);
83   }
84 
85   template <typename URBG>
86   result_type operator()(URBG& g,  // NOLINT(runtime/references)
87                          const param_type& p);
88 
param()89   param_type param() const { return param_; }
param(const param_type & p)90   void param(const param_type& p) { param_ = p; }
91 
result_type(min)92   result_type(min)() const { return 0; }
result_type(max)93   result_type(max)() const {
94     return std::numeric_limits<result_type>::infinity();
95   }
96 
lambda()97   result_type lambda() const { return param_.lambda(); }
98 
99   friend bool operator==(const exponential_distribution& a,
100                          const exponential_distribution& b) {
101     return a.param_ == b.param_;
102   }
103   friend bool operator!=(const exponential_distribution& a,
104                          const exponential_distribution& b) {
105     return a.param_ != b.param_;
106   }
107 
108  private:
109   param_type param_;
110   random_internal::FastUniformBits<uint64_t> fast_u64_;
111 };
112 
113 // --------------------------------------------------------------------------
114 // Implementation details follow
115 // --------------------------------------------------------------------------
116 
117 template <typename RealType>
118 template <typename URBG>
119 typename exponential_distribution<RealType>::result_type
operator()120 exponential_distribution<RealType>::operator()(
121     URBG& g,  // NOLINT(runtime/references)
122     const param_type& p) {
123   using random_internal::GenerateNegativeTag;
124   using random_internal::GenerateRealFromBits;
125   using real_type =
126       absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
127 
128   const result_type u = GenerateRealFromBits<real_type, GenerateNegativeTag,
129                                              false>(fast_u64_(g));  // U(-1, 0)
130 
131   // log1p(-x) is mathematically equivalent to log(1 - x) but has more
132   // accuracy for x near zero.
133   return p.neg_inv_lambda_ * std::log1p(u);
134 }
135 
136 template <typename CharT, typename Traits, typename RealType>
137 std::basic_ostream<CharT, Traits>& operator<<(
138     std::basic_ostream<CharT, Traits>& os,  // NOLINT(runtime/references)
139     const exponential_distribution<RealType>& x) {
140   auto saver = random_internal::make_ostream_state_saver(os);
141   os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
142   os << x.lambda();
143   return os;
144 }
145 
146 template <typename CharT, typename Traits, typename RealType>
147 std::basic_istream<CharT, Traits>& operator>>(
148     std::basic_istream<CharT, Traits>& is,    // NOLINT(runtime/references)
149     exponential_distribution<RealType>& x) {  // NOLINT(runtime/references)
150   using result_type = typename exponential_distribution<RealType>::result_type;
151   using param_type = typename exponential_distribution<RealType>::param_type;
152   result_type lambda;
153 
154   auto saver = random_internal::make_istream_state_saver(is);
155   lambda = random_internal::read_floating_point<result_type>(is);
156   if (!is.fail()) {
157     x.param(param_type(lambda));
158   }
159   return is;
160 }
161 
162 ABSL_NAMESPACE_END
163 }  // namespace absl
164 
165 #endif  // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
166