vSMC
vSMC: Scalable Monte Carlo
u01_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/u01_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_U01_DISTRIBUTION_HPP
33 #define VSMC_RNG_U01_DISTRIBUTION_HPP
34 
36 #include <vsmc/rng/u01.hpp>
38 
39 namespace vsmc
40 {
41 
48 template <typename RealType, typename Left, typename Right>
49 class U01LRDistribution
50 {
51  public:
52  using result_type = RealType;
54 
55  class param_type
56  {
57  public:
58  using result_type = RealType;
60 
61  friend bool operator==(const param_type &, const param_type &)
62  {
63  return true;
64  }
65 
66  friend bool operator!=(const param_type &, const param_type &)
67  {
68  return false;
69  }
70 
71  template <typename CharT, typename Traits>
72  friend std::basic_ostream<CharT, Traits> &operator<<(
73  std::basic_ostream<CharT, Traits> &os, const param_type &)
74  {
75  return os;
76  }
77 
78  template <typename CharT, typename Traits>
79  friend std::basic_istream<CharT, Traits> &operator>>(
80  std::basic_istream<CharT, Traits> &is, param_type &)
81  {
82  return is;
83  }
84  }; // class param_type
85 
87  explicit U01LRDistribution(const param_type &) {}
88 
89  result_type min VSMC_MNE() const { return 0; }
90  result_type max VSMC_MNE() const { return 1; }
91 
92  void reset() {}
93 
94  template <typename RNGType>
95  result_type operator()(RNGType &rng)
96  {
97  using uint_type =
98  typename std::conditional<internal::RNGBits<RNGType>::value >= 64,
100  using flt_type = typename std::conditional<
101  std::is_same<result_type, float>::value ||
102  std::is_same<result_type, double>::value,
103  result_type, double>::type;
104 
106 
108  }
109 
110  template <typename RNGType>
111  result_type operator()(RNGType &rng, const param_type &)
112  {
113  return operator()(rng);
114  }
115 
116  template <typename RNGType>
117  void operator()(RNGType &rng, std::size_t n, result_type *r)
118  {
119  u01_lr_distribution<RealType, Left, Right>(rng, n, r);
120  }
121 
122  template <typename RNGType>
124  RNGType &rng, std::size_t n, result_type *r, const param_type &)
125  {
126  u01_lr_distribution<RealType, Left, Right>(rng, n, r);
127  }
128 
131  {
132  return true;
133  }
134 
137  {
138  return false;
139  }
140 
141  template <typename CharT, typename Traits>
142  friend std::basic_ostream<CharT, Traits> &operator<<(
143  std::basic_ostream<CharT, Traits> &os,
145  {
146  return os;
147  }
148 
149  template <typename CharT, typename Traits>
150  friend std::basic_istream<CharT, Traits> &operator>>(
151  std::basic_istream<CharT, Traits> &is,
153  {
154  return is;
155  }
156 }; // class U01LRDistribution
157 
160 template <typename RealType = double>
162 
165 template <typename RealType = double>
167 
170 template <typename RealType = double>
172 
175 template <typename RealType = double>
177 
180 template <typename RealType = double>
182 
183 namespace internal
184 {
185 
186 template <std::size_t K, typename RealType, typename Left, typename Right,
187  typename RNGType>
188 inline void u01_lr_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
189 {
190  uint32_t s[K];
191  uniform_bits_distribution(rng, n, s);
192  for (std::size_t i = 0; i != n; ++i)
194 }
195 
196 } // namespace vsmc::internal
197 
200 template <typename RealType, typename Left, typename Right, typename RNGType>
201 inline void u01_lr_distribution(RNGType &rng, std::size_t n, RealType *r)
202 {
203  const std::size_t k = 1000;
204  const std::size_t m = n / k;
205  const std::size_t l = n % k;
206  for (std::size_t i = 0; i != m; ++i, r += k) {
207  internal::u01_lr_distribution_impl<k, RealType, Left, Right>(
208  rng, k, r);
209  }
210  internal::u01_lr_distribution_impl<k, RealType, Left, Right>(rng, l, r);
211 }
212 
215 template <typename RealType, typename RNGType>
216 inline void u01_cc_distribution(RNGType &rng, std::size_t n, RealType *r)
217 {
218  u01_lr_distribution<RealType, Closed, Closed>(rng, n, r);
219 }
220 
223 template <typename RealType, typename RNGType>
224 inline void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
225 {
226  u01_lr_distribution<RealType, Closed, Open>(rng, n, r);
227 }
228 
231 template <typename RealType, typename RNGType>
232 inline void u01_oc_distribution(RNGType &rng, std::size_t n, RealType *r)
233 {
234  u01_lr_distribution<RealType, Open, Closed>(rng, n, r);
235 }
236 
239 template <typename RealType, typename RNGType>
240 inline void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
241 {
242  u01_lr_distribution<RealType, Open, Open>(rng, n, r);
243 }
244 
247 template <typename RealType, typename RNGType>
248 inline void u01_distribution(RNGType &rng, std::size_t n, RealType *r)
249 {
250  u01_co_distribution(rng, n, r);
251 }
252 
253 template <typename RealType, typename RNGType, typename Left, typename Right>
255  std::size_t n, RealType *r)
256 {
257  dist(rng, n, r);
258 }
259 
260 } // namespace vsmc
261 
262 #endif // VSMC_RNG_U01_HPP
Definition: monitor.hpp:49
Standard uniform distribution with open/closed variants.
Definition: common.hpp:512
result_type operator()(RNGType &rng, const param_type &)
uint uint32_t
Definition: opencl.h:39
ulong uint64_t
Definition: opencl.h:40
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on open-open interval.
void u01_lr_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, param_type &)
void u01_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
friend bool operator==(const U01LRDistribution< RealType, Left, Right > &, const U01LRDistribution< RealType, Left, Right > &)
U01LRDistribution(const param_type &)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, U01LRDistribution< RealType, Left, Right > &)
void u01_lr_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates with open/closed variants.
#define VSMC_MNE
Definition: defines.hpp:38
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-open interval.
void operator()(RNGType &rng, std::size_t n, result_type *r)
result_type min() const
void operator()(RNGType &rng, std::size_t n, result_type *r, const param_type &)
friend bool operator==(const param_type &, const param_type &)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const param_type &)
friend bool operator!=(const param_type &, const param_type &)
friend bool operator!=(const U01LRDistribution< RealType, Left, Right > &, const U01LRDistribution< RealType, Left, Right > &)
result_type operator()(RNGType &rng)
void u01_cc_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-closed interval.
result_type max() const
void uniform_bits_distribution(RNGType &, std::size_t, UIntType *)
void u01_oc_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on open-closed interval.
Uniform bits distribution.
Definition: common.hpp:461
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const U01LRDistribution< RealType, Left, Right > &)