32 #ifndef VSMC_RNG_NORMAL_DISTRIBUTION_HPP 33 #define VSMC_RNG_NORMAL_DISTRIBUTION_HPP 45 template <
typename RealType>
55 template <
typename RealType>
59 Normal, normal, mean, 0, stddev, 1)
66 : param_(mean, stddev), v_(0), saved_(false)
72 : param_(param), v_(0), saved_(false)
83 return std::numeric_limits<result_type>::lowest();
104 param_ = std::move(param);
108 template <
typename RNGType>
111 return operator()(rng, param_);
114 template <
typename RNGType>
117 return generate(rng, param);
120 template <
typename RNGType>
123 operator()(rng, n, r, param_);
126 template <
typename RNGType>
131 for (std::size_t i = 0; i != n; ++i)
132 r[i] =
operator()(rng, param);
141 if (dist1.param_ != dist2.param_)
145 if (dist1.saved_ && !dist2.saved_)
147 if (!dist1.saved_ && dist2.saved_)
155 return !(dist1 == dist2);
158 template <
typename CharT,
typename Traits>
165 os << dist.param_ <<
' ';
166 os << dist.v_ <<
' ';
172 template <
typename CharT,
typename Traits>
182 is >> std::ws >> param;
184 is >> std::ws >> saved;
199 template <
typename RNGType>
209 result_type u2 = const_pi_2<result_type>() * u01(rng);
222 template <std::
size_t K,
typename RealType,
typename RNGType>
224 RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
227 const std::size_t nu = n / 2;
228 RealType *
const u1 = r;
229 RealType *
const u2 = r + nu;
232 mul(nu, static_cast<RealType>(-2), s, s);
234 mul(nu, const_pi_2<RealType>(), u2, u2);
236 mul(nu, stddev, s, s);
237 fma(nu, s, u1, mean, u1);
238 fma(nu, s, u2, mean, u2);
245 template <
typename RealType,
typename RNGType>
247 RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
249 static_assert(std::is_floating_point<RealType>::value,
250 "**normal_distribution** USED WITH RealType OTHER THAN FLOATING POINT " 253 const std::size_t k = 1024;
254 const std::size_t m = n / k;
255 const std::size_t l = n % k;
256 for (std::size_t i = 0; i != m; ++i, r += k)
257 internal::normal_distribution_impl<k>(rng, k, r, mean, stddev);
258 internal::normal_distribution_impl<k>(rng, l, r, mean, stddev);
261 RealType u = u01(rng);
262 RealType v = u01(rng);
265 std::cos(const_pi_2<RealType>() * v);
273 #endif // VSMC_RNG_NORMAL_DISTRIBUTION_HPP NormalDistribution(const param_type ¶m)
void pram(param_type &¶m)
void mul(std::size_t n, const float *a, const float *b, float *y)
const param_type & param() const
void sqrt(std::size_t n, const float *a, float *y)
#define VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_2(Name, name, p1, v1, p2, v2)
bool is_equal(const T &a, const T &b)
void normal_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
result_type stddev() const
void u01_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
result_type operator()(RNGType &rng)
friend bool operator!=(const distribution_type &dist1, const distribution_type &dist2)
void normal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating Normal random variates.
void cos(std::size_t n, const float *a, float *y)
void operator()(RNGType &rng, std::size_t n, result_type *r)
void operator()(RNGType &rng, std::size_t n, result_type *r, const param_type ¶m)
bool normal_distribution_check_param(RealType, RealType stddev)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, distribution_type &dist)
void param(const param_type ¶m)
result_type stddev() const
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
void sincos(std::size_t n, const float *a, float *y, float *z)
void sin(std::size_t n, const float *a, float *y)
result_type operator()(RNGType &rng, const param_type ¶m)
Standard uniform distribution.
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
friend bool operator==(const distribution_type &dist1, const distribution_type &dist2)
void log(std::size_t n, const float *a, float *y)
NormalDistribution< RealType > distribution_type
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const distribution_type &dist)