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