vSMC
vSMC: Scalable Monte Carlo
uniform_real_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/uniform_real_distribution.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2015, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_RNG_UNIFORM_REAL_DISTRIBUTION_HPP
33 #define VSMC_RNG_UNIFORM_REAL_DISTRIBUTION_HPP
34 
37 
38 #define VSMC_RUNTIME_ASSERT_RNG_UNIFORM_REAL_DISTRIBUTION_PARAM_CHECK(a, b) \
39  VSMC_RUNTIME_ASSERT((a <= b), "**UniformRealDistribution** CONSTRUCTED " \
40  "WITH INVALID MINIMUM AND MAXIMUM " \
41  "PARAMTER VALUES")
42 
43 namespace vsmc
44 {
45 
52 template <typename RealType, typename Left, typename Right>
53 class UniformRealLRDistribution
54 {
55  public:
56  using result_type = RealType;
58 
59  class param_type
60  {
61  public:
62  using result_type = RealType;
63  using distribution_type =
65 
67  {
68  invariant();
69  }
70 
71  result_type a() const { return a_; }
72  result_type b() const { return b_; }
73 
74  friend bool operator==(
75  const param_type &param1, const param_type &param2)
76  {
77  if (!is_equal(param1.a_, param2.a_))
78  return false;
79  if (!is_equal(param1.b_, param2.b_))
80  return false;
81  return true;
82  }
83 
84  friend bool operator!=(
85  const param_type &param1, const param_type &param2)
86  {
87  return !(param1 == param2);
88  }
89 
90  template <typename CharT, typename Traits>
91  friend std::basic_ostream<CharT, Traits> &operator<<(
92  std::basic_ostream<CharT, Traits> &os, const param_type &param)
93  {
94  if (!os.good())
95  return os;
96 
97  os << param.a_ << ' ';
98  os << param.b_;
99 
100  return os;
101  }
102 
103  template <typename CharT, typename Traits>
104  friend std::basic_istream<CharT, Traits> &operator>>(
105  std::basic_istream<CharT, Traits> &is, param_type &param)
106  {
107  if (!is.good())
108  return is;
109 
110  result_type a = 1;
111  result_type b = 0;
112  is >> std::ws >> a;
113  is >> std::ws >> b;
114 
115  if (is.good()) {
116  if (a <= b)
117  param = param_type(a, b);
118  else
119  is.setstate(std::ios_base::failbit);
120  }
121 
122  return is;
123  }
124 
125  private:
126  result_type a_;
127  result_type b_;
128 
129  friend distribution_type;
130 
131  void invariant()
132  {
134  a_, b_);
135  }
136 
137  void reset() {}
138  }; // class param_type
139 
141  : param_(a, b)
142  {
143  }
144 
145  explicit UniformRealLRDistribution(const param_type &param) : param_(param)
146  {
147  }
148 
149  result_type a() const { return param_.a(); }
150  result_type b() const { return param_.b(); }
151 
152  result_type min VSMC_MNE() const { return a(); }
153  result_type max VSMC_MNE() const { return b(); }
154 
155  void reset() {}
156 
157  template <typename RNGType>
158  result_type operator()(RNGType &rng)
159  {
160  return operator()(rng, param_);
161  }
162 
163  template <typename RNGType>
164  result_type operator()(RNGType &rng, const param_type &param)
165  {
167 
168  return param.a() + (param.b() - param.a()) * u01(rng);
169  }
170 
171  template <typename RNGType>
172  void operator()(RNGType &rng, std::size_t n, result_type *r)
173  {
174  return operator()(rng, n, r, param_);
175  }
176 
177  template <typename RNGType>
179  RNGType &rng, std::size_t n, result_type *r, const param_type &param)
180  {
181  uniform_real_lr_distribution<RealType, Left, Right>(
182  rng, n, r, param.a(), param.b());
183  }
184 
186 
187  private:
188  param_type param_;
189 }; // class UniformRealLRDistribution
190 
193 template <typename RealType = double>
196 
199 template <typename RealType = double>
202 
205 template <typename RealType = double>
208 
211 template <typename RealType = double>
214 
216 template <typename RealType = double>
218 
219 namespace internal
220 {
221 
222 template <typename RealType, typename Left, typename Right, typename RNGType>
224  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
225 {
226  u01_lr_distribution<RealType, Left, Right>(rng, n, r);
227  fma(n, (b - a), r, a, r);
228 }
229 
230 } // namespace vsmc::internal
231 
234 template <typename RealType, typename Left, typename Right, typename RNGType>
236  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
237 {
238  const std::size_t k = 1000;
239  const std::size_t m = n / k;
240  const std::size_t l = n % k;
241  for (std::size_t i = 0; i != m; ++i) {
242  internal::uniform_real_lr_distribution_impl<RealType, Left, Right>(
243  rng, k, r + i * k, a, b);
244  }
245  internal::uniform_real_lr_distribution_impl<RealType, Left, Right>(
246  rng, l, r + m * k, a, b);
247 }
248 
251 template <typename RealType, typename RNGType>
253  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
254 {
255  uniform_real_lr_distribution<RealType, Closed, Closed>(rng, n, r, a, b);
256 }
257 
260 template <typename RealType, typename RNGType>
262  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
263 {
264  uniform_real_lr_distribution<RealType, Closed, Open>(rng, n, r, a, b);
265 }
266 
269 template <typename RealType, typename RNGType>
271  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
272 {
273  uniform_real_lr_distribution<RealType, Open, Closed>(rng, n, r, a, b);
274 }
275 
278 template <typename RealType, typename RNGType>
280  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
281 {
282  uniform_real_lr_distribution<RealType, Open, Open>(rng, n, r, a, b);
283 }
284 
287 template <typename RealType, typename RNGType>
289  RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
290 {
291  uniform_real_co_distribution(rng, n, r, a, b);
292 }
293 
294 template <typename RealType, typename RNGType, typename Left, typename Right>
295 inline void rng_rand(RNGType &rng,
297  RealType *r)
298 {
299  dist(rng, n, r);
300 }
301 
302 } // namespace vsmc
303 
304 #endif // VSMC_RNG_UNIFORM_REAL_DISTRIBUTION_HPP
friend bool operator!=(const param_type &param1, const param_type &param2)
Definition: monitor.hpp:49
Standard uniform distribution with open/closed variants.
Definition: common.hpp:512
UniformRealLRDistribution(const param_type &param)
bool is_equal(const T &a, const T &b)
Definition: common.hpp:333
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
void uniform_real_lr_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
void operator()(RNGType &rng, std::size_t n, result_type *r)
UniformRealLRDistribution(result_type a=0, result_type b=1)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, param_type &param)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
Definition: common.hpp:286
void uniform_real_oo_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generate uniform real random variates on open-open interval.
#define VSMC_RUNTIME_ASSERT_RNG_UNIFORM_REAL_DISTRIBUTION_PARAM_CHECK(a, b)
#define VSMC_MNE
Definition: defines.hpp:38
void uniform_real_cc_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generate uniform real random variates on closed-closed interval.
void uniform_real_oc_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generate uniform real random variates on open-closed interval.
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const param_type &param)
void uniform_real_lr_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generate uniform real random variates with open/closed variants.
void operator()(RNGType &rng, std::size_t n, result_type *r, const param_type &param)
UniformRealLRDistribution< RealType, Left, Right > distribution_type
friend bool operator==(const param_type &param1, const param_type &param2)
void uniform_real_co_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generate uniform real random variates on closed-open interval.
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
Definition: vmath.hpp:257
result_type operator()(RNGType &rng, const param_type &param)
void uniform_real_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generate uniform real random variates.
Uniform real distribution with open/closed variants.
Definition: common.hpp:515