32 #ifndef VSMC_RNG_INTERNAL_COMMON_HPP 33 #define VSMC_RNG_INTERNAL_COMMON_HPP 41 #define VSMC_RUNTIME_ASSERT_RNG_DISTRIBUTION_PARAM(flag, Name) \ 42 VSMC_RUNTIME_ASSERT((flag), \ 43 "**" #Name "Distribution** CONSTRUCTED WITH INVALID PARAMETERS") 45 #define VSMC_DEFINE_RNG_DISTRIBUTION_1(Name, name, T, T1, p1, v1) \ 47 using result_type = T; \ 48 using distribution_type = Name##Distribution<T>; \ 53 using result_type = T; \ 54 using distribution_type = Name##Distribution<T>; \ 56 explicit param_type(T1 p1 = v1) : p1##_(p1) \ 58 VSMC_RUNTIME_ASSERT_RNG_DISTRIBUTION_PARAM( \ 59 internal::name##_distribution_check_param(p1), Name); \ 62 T1 p1() const { return p1##_; } \ 64 friend bool operator==( \ 65 const param_type ¶m1, const param_type ¶m2) \ 67 if (!internal::is_equal(param1.p1##_, param2.p1##_)) \ 72 friend bool operator!=( \ 73 const param_type ¶m1, const param_type ¶m2) \ 75 return !(param1 == param2); \ 78 template <typename CharT, typename Traits> \ 79 friend std::basic_ostream<CharT, Traits> &operator<<( \ 80 std::basic_ostream<CharT, Traits> &os, const param_type ¶m) \ 90 template <typename CharT, typename Traits> \ 91 friend std::basic_istream<CharT, Traits> &operator>>( \ 92 std::basic_istream<CharT, Traits> &is, param_type ¶m) \ 98 is >> std::ws >> p1; \ 101 if (internal::name##_distribution_check_param(p1)) \ 104 is.setstate(std::ios_base::failbit); \ 114 explicit Name##Distribution(T1 p1 = v1) : param_(p1) { reset(); } \ 116 explicit Name##Distribution(const param_type ¶m) : param_(param) \ 121 T1 p1() const { return param_.p1(); } \ 123 param_type param() const { return param_; } \ 125 void param(const param_type &parm) \ 131 template <typename RNGType> \ 132 result_type operator()(RNGType &rng) \ 134 return operator()(rng, param_); \ 137 template <typename RNGType> \ 138 result_type operator()(RNGType &rng, const param_type ¶m) \ 140 return generate(rng, param); \ 143 template <typename RNGType> \ 144 void operator()(RNGType &rng, std::size_t n, result_type *r) \ 146 operator()(rng, n, r, param_); \ 149 template <typename RNGType> \ 151 RNGType &rng, std::size_t n, result_type *r, const param_type ¶m) \ 153 name##_distribution(rng, n, r, param.p1()); \ 159 #define VSMC_DEFINE_RNG_DISTRIBUTION_2(Name, name, T, T1, p1, v1, T2, p2, v2) \ 161 using result_type = T; \ 162 using distribution_type = Name##Distribution<T>; \ 167 using result_type = T; \ 168 using distribution_type = Name##Distribution<T>; \ 170 explicit param_type(T1 p1 = v1, T2 p2 = v2) : p1##_(p1), p2##_(p2) \ 172 VSMC_RUNTIME_ASSERT_RNG_DISTRIBUTION_PARAM( \ 173 internal::name##_distribution_check_param(p1, p2), Name); \ 176 T1 p1() const { return p1##_; } \ 177 T2 p2() const { return p2##_; } \ 179 friend bool operator==( \ 180 const param_type ¶m1, const param_type ¶m2) \ 182 if (!internal::is_equal(param1.p1##_, param2.p1##_)) \ 184 if (!internal::is_equal(param1.p2##_, param2.p2##_)) \ 189 friend bool operator!=( \ 190 const param_type ¶m1, const param_type ¶m2) \ 192 return !(param1 == param2); \ 195 template <typename CharT, typename Traits> \ 196 friend std::basic_ostream<CharT, Traits> &operator<<( \ 197 std::basic_ostream<CharT, Traits> &os, const param_type ¶m) \ 202 os << param.p1##_ << ' '; \ 208 template <typename CharT, typename Traits> \ 209 friend std::basic_istream<CharT, Traits> &operator>>( \ 210 std::basic_istream<CharT, Traits> &is, param_type ¶m) \ 217 is >> std::ws >> p1; \ 218 is >> std::ws >> p2; \ 221 if (internal::name##_distribution_check_param(p1, p2)) { \ 225 is.setstate(std::ios_base::failbit); \ 237 explicit Name##Distribution(T1 p1 = v1, T2 p2 = v2) : param_(p1, p2) \ 242 explicit Name##Distribution(const param_type ¶m) : param_(param) \ 247 T1 p1() const { return param_.p1(); } \ 248 T2 p2() const { return param_.p2(); } \ 250 param_type param() const { return param_; } \ 252 void param(const param_type &parm) \ 258 template <typename RNGType> \ 259 result_type operator()(RNGType &rng) \ 261 return operator()(rng, param_); \ 264 template <typename RNGType> \ 265 result_type operator()(RNGType &rng, const param_type ¶m) \ 267 return generate(rng, param); \ 270 template <typename RNGType> \ 271 void operator()(RNGType &rng, std::size_t n, result_type *r) \ 273 operator()(rng, n, r, param_); \ 276 template <typename RNGType> \ 278 RNGType &rng, std::size_t n, result_type *r, const param_type ¶m) \ 280 name##_distribution(rng, n, r, param.p1(), param.p2()); \ 286 #define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS \ 288 friend bool operator==( \ 289 const distribution_type &dist1, const distribution_type &dist2) \ 291 return dist1.param_ == dist2.param_; \ 294 friend bool operator!=( \ 295 const distribution_type &dist1, const distribution_type &dist2) \ 297 return dist1.param_ != dist2.param_; \ 300 template <typename CharT, typename Traits> \ 301 friend std::basic_ostream<CharT, Traits> &operator<<( \ 302 std::basic_ostream<CharT, Traits> &os, const distribution_type &dist) \ 309 template <typename CharT, typename Traits> \ 310 friend std::basic_istream<CharT, Traits> &operator>>( \ 311 std::basic_istream<CharT, Traits> &is, distribution_type &dist) \ 313 is >> std::ws >> dist.param_; \ 328 #pragma clang diagnostic push 329 #pragma clang diagnostic ignored "-Wfloat-equal" 332 template <
typename T>
339 #pragma clang diagnostic pop 350 template <std::u
int64_t UMax,
int N>
360 template <std::u
int64_t UMax>
367 template <
typename RNGType>
369 :
public std::integral_constant<int,
370 RNGBitsN<static_cast<std::uint64_t>(RNGType::min VSMC_MNE()),
375 template <
typename RNGType>
377 :
public std::integral_constant<int,
378 RNGBitsN<static_cast<std::uint64_t>(RNGType::max VSMC_MNE()),
383 template <
typename RNGType>
384 class RNGBits :
public std::integral_constant<int,
385 RNGMaxBits<RNGType>::value - RNGMinBits<RNGType>::value>
389 template <std::
size_t>
393 class IntBitsN<sizeof(int8_t)> :
public std::integral_constant<int, 8>
398 class IntBitsN<sizeof(int16_t)> :
public std::integral_constant<int, 16>
403 class IntBitsN<sizeof(int32_t)> :
public std::integral_constant<int, 32>
408 class IntBitsN<sizeof(int64_t)> :
public std::integral_constant<int, 64>
412 template <
typename IntType>
417 template <
typename SeedSeq,
typename U,
typename V = U,
typename W = V>
419 :
public std::integral_constant<bool,
420 !std::is_convertible<SeedSeq, U>::value &&
421 !std::is_convertible<SeedSeq, V>::value &&
422 !std::is_convertible<SeedSeq, W>::value &&
423 !std::is_same<typename std::remove_cv<SeedSeq>::type,
425 !std::is_same<typename std::remove_cv<SeedSeq>::type,
427 !std::is_same<typename std::remove_cv<SeedSeq>::type, W>::value>
443 template <
typename RNGType>
444 void rng_rand(RNGType &rng, std::size_t n,
typename RNGType::result_type *r)
446 for (std::size_t i = 0; i != n; ++i)
453 template <
typename Generator>
457 template <
typename =
int>
460 template <
typename =
unsigned>
463 template <
typename =
int>
466 template <
typename =
double>
469 template <
typename =
double>
472 template <
typename =
double>
475 template <
typename =
double>
478 template <
typename =
double>
481 template <
typename =
double>
484 template <
typename =
double>
487 template <
typename =
double>
490 template <
typename =
double>
493 template <
typename =
double>
496 template <
typename =
double>
499 template <
typename =
double>
502 template <
typename =
double>
505 template <
typename =
double>
508 template <
typename =
double>
511 template <
typename =
double,
typename = Closed,
typename = Open>
514 template <
typename =
double,
typename = Closed,
typename = Open>
517 template <
typename =
double>
520 template <
typename IntType,
typename RNGType>
524 template <
typename UIntType,
typename RNGType>
528 template <
typename RealType,
typename RNGType>
532 template <
typename RealType,
typename RNGType>
536 template <
typename RealType,
typename RNGType>
540 template <
typename RealType,
typename RNGType>
544 template <
typename RealType,
typename RNGType>
548 template <
typename RealType,
typename RNGType>
552 template <
typename RealType,
typename RNGType>
556 template <
typename RealType,
typename RNGType>
560 template <
typename RealType,
typename RNGType>
564 template <
typename RealType,
typename RNGType>
568 template <
typename RealType,
typename RNGType>
572 template <
typename RealType,
typename RNGType>
576 template <
typename RealType,
typename RNGType>
580 template <
typename RealType,
typename RNGType>
584 template <
typename RealType,
typename RNGType>
588 template <
typename RealType,
typename RNGType>
592 template <
typename RealType,
typename RNGType,
typename Left,
typename Right>
594 std::size_t, RealType *);
596 template <
typename RealType,
typename RNGType,
typename Left,
typename Right>
601 template <
typename RealType,
typename RNGType>
605 template <
typename IntType,
typename RNGType>
608 template <
typename UIntType,
typename RNGType>
611 template <
typename RealType,
typename RNGType>
613 RNGType &, std::size_t, RealType *, RealType, RealType);
615 template <
typename RealType,
typename RNGType>
617 RNGType &, std::size_t, RealType *, RealType, RealType);
619 template <
typename RealType,
typename RNGType>
621 RNGType &, std::size_t, RealType *, RealType);
623 template <
typename RealType,
typename RNGType>
625 RNGType &, std::size_t, RealType *, RealType);
627 template <
typename RealType,
typename RNGType>
629 RNGType &, std::size_t, RealType *, RealType, RealType);
631 template <
typename RealType,
typename RNGType>
633 RNGType &, std::size_t, RealType *, RealType, RealType);
635 template <
typename RealType,
typename RNGType>
637 RNGType &, std::size_t, RealType *, RealType, RealType);
639 template <
typename RealType,
typename RNGType>
641 RNGType &, std::size_t, RealType *, RealType, RealType);
643 template <
typename RealType,
typename RNGType>
645 RNGType &, std::size_t, RealType *, RealType, RealType);
647 template <
typename RealType,
typename RNGType>
649 RNGType &, std::size_t, RealType *, RealType, RealType);
651 template <
typename RealType,
typename RNGType>
653 RNGType &, std::size_t, RealType *, RealType, RealType);
655 template <
typename RealType,
typename RNGType>
657 RNGType &, std::size_t, RealType *, RealType, RealType);
659 template <
typename RealType,
typename RNGType>
661 RNGType &, std::size_t, RealType *, RealType, RealType);
663 template <
typename RealType,
typename RNGType>
665 RNGType &, std::size_t, RealType *, RealType);
667 template <
typename RealType,
typename RNGType>
669 RNGType &, std::size_t, RealType *, RealType);
671 template <
typename RealType,
typename Left,
typename Right,
typename RNGType>
674 template <
typename RealType,
typename RNGType>
677 template <
typename RealType,
typename RNGType>
679 RNGType &, std::size_t, RealType *, RealType, RealType);
681 template <
typename RealType,
typename Left,
typename Right,
typename RNGType>
683 RNGType &, std::size_t, RealType *, RealType, RealType);
685 template <
typename RealType,
typename RNGType>
687 RNGType &, std::size_t, RealType *, RealType, RealType);
691 template <MKL_INT,
int>
694 template <MKL_INT BRNG,
int Bits>
698 template <MKL_INT BRNG,
int Bits>
702 template <MKL_INT BRNG,
int Bits>
706 template <MKL_INT BRNG,
int Bits>
710 template <MKL_INT BRNG,
int Bits>
713 template <MKL_INT BRNG,
int Bits>
716 template <MKL_INT BRNG,
int Bits>
720 template <MKL_INT BRNG,
int Bits>
724 template <MKL_INT BRNG,
int Bits>
728 template <MKL_INT BRNG,
int Bits>
732 template <MKL_INT BRNG,
int Bits>
736 template <MKL_INT BRNG,
int Bits>
740 template <MKL_INT BRNG,
int Bits>
744 template <MKL_INT BRNG,
int Bits>
748 template <MKL_INT BRNG,
int Bits>
752 template <MKL_INT BRNG,
int Bits>
756 template <MKL_INT BRNG,
int Bits>
760 template <MKL_INT BRNG,
int Bits>
764 template <MKL_INT BRNG,
int Bits>
768 template <MKL_INT BRNG,
int Bits>
772 template <MKL_INT BRNG,
int Bits>
776 template <MKL_INT BRNG,
int Bits>
780 template <MKL_INT BRNG,
int Bits>
784 template <MKL_INT BRNG,
int Bits>
788 template <MKL_INT BRNG,
int Bits>
792 template <MKL_INT BRNG,
int Bits>
796 #endif // VSMC_HAS_MKL 800 #endif // VSMC_RNG_INTERNAL_COMMON_HPP #define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Standard uniform distribution with open/closed variants.
ExtremeValue distribution.
void laplace_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating laplace random variates.
void bernoulli_distribution(RNGType &rng, std::size_t n, IntType *r, IntType p)
Generating bernoulli random variates.
void student_t_distribution(RNGType &, std::size_t, RealType *, RealType)
Generating student-t random variates.
void logistic_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating logistic random variates.
void cauchy_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generating cauchy random variates.
bool is_equal(const T &a, const T &b)
void lognormal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating lognormal random variates.
Counter based RNG engine.
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
void exponential_distribution(RNGType &rng, std::size_t n, RealType *r, RealType lambda)
Generating exponential random variates.
void u01_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
void normal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating normal random variates.
void rayleigh_distribution(RNGType &, std::size_t, RealType *, RealType)
Generating rayleigh random variates.
void pareto_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating pareto random variates.
typename KeyTypeTrait< T >::type KeyType
void u01_lr_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates with open/closed variants.
typename Generator::result_type result_type
void weibull_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating weibull random variates.
Exponential distribution.
void uniform_real_lr_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generate uniform real random variates with open/closed variants.
void extreme_value_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generating extreme_value random variates.
void chi_squared_distribution(RNGType &rng, std::size_t n, RealType *r, RealType df)
Generating random variates.
static constexpr std::uint64_t value
void beta_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
Generating beta random variates.
void uniform_bits_distribution(RNGType &, std::size_t, UIntType *)
Draw a single sample given weights.
void fisher_f_distribution(RNGType &rng, std::size_t n, RealType *r, RealType df1, RealType df2)
Generating Fisher-F random variates.
void uniform_real_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generate uniform real random variates.
void gamma_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
Generating gamma random variates.
internal::MKLResultType< Bits > result_type
void levy_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating levy random variates.