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_PARAM_TYPE_0(Name, T, t, Type) \ 49 static_assert(std::is_##t<T>::value, \ 50 "**" #Name "Distribution::param_type** USED WITH " #T \ 51 " OTHER THAN " #Type " INTEGER TYPES"); \ 54 using result_type = T; \ 55 using distribution_type = Name##Distribution<T>; \ 57 friend bool operator==(const param_type &, const param_type &) \ 62 friend bool operator!=(const param_type &, const param_type &) \ 67 template <typename CharT, typename Traits> \ 68 friend std::basic_ostream<CharT, Traits> &operator<<( \ 69 std::basic_ostream<CharT, Traits> &os, const param_type &) \ 74 template <typename CharT, typename Traits> \ 75 friend std::basic_istream<CharT, Traits> &operator>>( \ 76 std::basic_istream<CharT, Traits> &is, param_type &) \ 82 friend distribution_type; \ 83 }; // class param_type 85 #define VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_1(Name, name, p1, v1) \ 89 static_assert(std::is_floating_point<RealType>::value, \ 90 "**" #Name "Distribution::param_type** USED WITH RealType OTHER " \ 91 "THAN FLOATING POINT TYPES"); \ 94 using result_type = RealType; \ 95 using distribution_type = Name##Distribution<RealType>; \ 97 explicit param_type(result_type p1 = v1) : p1##_(p1) \ 99 VSMC_RUNTIME_ASSERT_RNG_DISTRIBUTION_PARAM( \ 100 internal::name##_distribution_check_param(p1), Name); \ 103 result_type p1() const { return p1##_; } \ 105 friend bool operator==( \ 106 const param_type ¶m1, const param_type ¶m2) \ 108 if (!internal::is_equal(param1.p1##_, param2.p1##_)) \ 113 friend bool operator!=( \ 114 const param_type ¶m1, const param_type ¶m2) \ 116 return !(param1 == param2); \ 119 template <typename CharT, typename Traits> \ 120 friend std::basic_ostream<CharT, Traits> &operator<<( \ 121 std::basic_ostream<CharT, Traits> &os, const param_type ¶m) \ 131 template <typename CharT, typename Traits> \ 132 friend std::basic_istream<CharT, Traits> &operator>>( \ 133 std::basic_istream<CharT, Traits> &is, param_type ¶m) \ 138 result_type p1 = 0; \ 139 is >> std::ws >> p1; \ 142 if (internal::name##_distribution_check_param(p1)) \ 145 is.setstate(std::ios_base::failbit); \ 154 friend distribution_type; \ 155 }; // class param_type 157 #define VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_2(Name, name, p1, v1, p2, v2) \ 161 static_assert(std::is_floating_point<RealType>::value, \ 162 "**" #Name "Distribution::param_type** USED WITH RealType OTHER " \ 163 "THAN FLOATING POINT TYPES"); \ 166 using result_type = RealType; \ 167 using distribution_type = Name##Distribution<RealType>; \ 169 explicit param_type(result_type p1 = v1, result_type p2 = v2) \ 170 : p1##_(p1), p2##_(p2) \ 172 VSMC_RUNTIME_ASSERT_RNG_DISTRIBUTION_PARAM( \ 173 internal::name##_distribution_check_param(p1, p2), Name); \ 176 result_type p1() const { return p1##_; } \ 177 result_type 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) \ 215 result_type p1 = 0; \ 216 result_type p2 = 0; \ 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); \ 236 friend distribution_type; \ 237 }; // class param_type 239 #define VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_0(Name, T) \ 241 using result_type = T; \ 242 using distribution_type = Name##Distribution<T>; \ 244 Name##Distribution() = default; \ 245 explicit Name##Distribution(const param_type &) {} 247 #define VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_1(Name, p1, v1) \ 249 using result_type = RealType; \ 250 using distribution_type = Name##Distribution<RealType>; \ 252 explicit Name##Distribution(result_type p1 = v1) : param_(p1) \ 257 explicit Name##Distribution(const param_type ¶m) : param_(param) \ 262 result_type p1() const { return param_.p1(); } 264 #define VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_2(Name, p1, v1, p2, v2) \ 266 using result_type = RealType; \ 267 using distribution_type = Name##Distribution<RealType>; \ 269 explicit Name##Distribution(result_type p1 = v1, result_type p2 = v2) \ 275 explicit Name##Distribution(const param_type ¶m) : param_(param) \ 280 result_type p1() const { return param_.p1(); } \ 281 result_type p2() const { return param_.p2(); } 283 #define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATOR(Name, name) \ 285 const param_type ¶m() const { return param_; } \ 287 void param(const param_type ¶m) \ 293 void pram(param_type &¶m) \ 295 param_ = std::move(param); \ 299 template <typename RNGType> \ 300 result_type operator()(RNGType &rng) \ 302 return operator()(rng, param_); \ 305 template <typename RNGType> \ 306 result_type operator()(RNGType &rng, const param_type ¶m) \ 308 return generate(rng, param); \ 311 template <typename RNGType> \ 312 void operator()(RNGType &rng, std::size_t n, result_type *r) \ 314 operator()(rng, n, r, param_); \ 317 template <typename RNGType> \ 319 RNGType &rng, std::size_t n, result_type *r, const param_type ¶m) \ 322 for (std::size_t i = 0; i != n; ++i) \ 323 r[i] = operator()(rng, param); \ 325 name##_distribution(rng, n, r, param); \ 329 friend bool operator==( \ 330 const distribution_type &dist1, const distribution_type &dist2) \ 332 return dist1.param_ == dist2.param_; \ 335 friend bool operator!=( \ 336 const distribution_type &dist1, const distribution_type &dist2) \ 338 return !(dist1 == dist2); \ 341 template <typename CharT, typename Traits> \ 342 friend std::basic_ostream<CharT, Traits> &operator<<( \ 343 std::basic_ostream<CharT, Traits> &os, const distribution_type &dist) \ 350 template <typename CharT, typename Traits> \ 351 friend std::basic_istream<CharT, Traits> &operator>>( \ 352 std::basic_istream<CharT, Traits> &is, distribution_type &dist) \ 354 is >> std::ws >> dist.param_; \ 364 #define VSMC_DEFINE_RNG_DISTRIBUTION_0(Name, name, T, t, Type) \ 365 VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_0(Name, T, t, Type) \ 366 VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_0(Name, T) \ 367 VSMC_DEFINE_RNG_DISTRIBUTION_OPERATOR(Name, name) 369 #define VSMC_DEFINE_RNG_DISTRIBUTION_1(Name, name, p1, v1) \ 370 VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_1(Name, name, p1, v1) \ 371 VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_1(Name, p1, v1) \ 372 VSMC_DEFINE_RNG_DISTRIBUTION_OPERATOR(Name, name) 374 #define VSMC_DEFINE_RNG_DISTRIBUTION_2(Name, name, p1, v1, p2, v2) \ 375 VSMC_DEFINE_RNG_DISTRIBUTION_PARAM_TYPE_2(Name, name, p1, v1, p2, v2) \ 376 VSMC_DEFINE_RNG_DISTRIBUTION_CONSTRUCTOR_2(Name, p1, v1, p2, v2) \ 377 VSMC_DEFINE_RNG_DISTRIBUTION_OPERATOR(Name, name) 379 #define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_0(Name, name, T) \ 380 template <typename T, typename RNGType> \ 381 inline void name##_distribution(RNGType &rng, std::size_t n, T *r, \ 382 const typename Name##Distribution<T>::param_type &) \ 384 name##_distribution(rng, n, r); \ 387 template <typename T, typename RNGType> \ 388 inline void rng_rand( \ 389 RNGType &rng, Name##Distribution<T> &dist, std::size_t n, T *r) \ 394 #define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_1(Name, name, p1) \ 395 template <typename RealType, typename RNGType> \ 396 inline void name##_distribution(RNGType &rng, std::size_t n, RealType *r, \ 397 const typename Name##Distribution<RealType>::param_type ¶m) \ 399 name##_distribution(rng, n, r, param.p1()); \ 402 template <typename RealType, typename RNGType> \ 403 inline void rng_rand(RNGType &rng, Name##Distribution<RealType> &dist, \ 404 std::size_t n, RealType *r) \ 409 #define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2) \ 410 template <typename RealType, typename RNGType> \ 411 inline void name##_distribution(RNGType &rng, std::size_t n, RealType *r, \ 412 const typename Name##Distribution<RealType>::param_type ¶m) \ 414 name##_distribution(rng, n, r, param.p1(), param.p2()); \ 417 template <typename RealType, typename RNGType> \ 418 inline void rng_rand(RNGType &rng, Name##Distribution<RealType> &dist, \ 419 std::size_t n, RealType *r) \ 432 template <typename UIntType, UIntType U,
433 int N =
std::numeric_limits<UIntType>::digits>
436 static constexpr
int M = std::numeric_limits<UIntType>::digits - N;
439 static constexpr
int value =
443 template <
typename UIntType, UIntType U>
447 static constexpr
int value = std::numeric_limits<UIntType>::digits;
454 template <
typename RNGType>
456 internal::RNGMinBitsImpl<typename RNGType::result_type,
457 RNGType::min()>::value>
464 template <
typename UIntType, UIntType U,
465 int N = std::numeric_limits<UIntType>::digits>
468 static constexpr UIntType bmax = std::numeric_limits<UIntType>::max() >>
469 (std::numeric_limits<UIntType>::digits - N);
476 template <
typename UIntType, UIntType U>
490 template <
typename RNGType>
492 internal::RNGMaxBitsImpl<typename RNGType::result_type,
493 RNGType::max()>::value>
507 template <
typename RNGType>
508 class RNGBits :
public std::integral_constant<int,
509 RNGMaxBits<RNGType>::value - RNGMinBits<RNGType>::value>
523 template <
typename RNGType>
524 void rng_rand(RNGType &rng, std::size_t n,
typename RNGType::result_type *r)
526 for (std::size_t i = 0; i != n; ++i)
533 template <
typename =
double, std::
size_t = Dynamic>
536 template <
typename =
double, std::
size_t = Dynamic, std::
size_t = Dynamic>
539 template <
typename =
double>
542 template <
typename =
double, std::
size_t = Dynamic>
545 template <
typename =
double>
548 template <
typename =
double>
551 template <
typename =
double>
554 template <
typename =
int>
557 template <
typename =
double>
560 template <
typename =
double>
563 template <
typename =
double>
566 template <
typename =
double>
569 template <
typename =
double>
572 template <
typename =
double>
575 template <
typename =
double>
578 template <
typename =
double>
581 template <
typename =
double>
584 template <
typename =
double, std::
size_t = Dynamic>
587 template <
typename =
double>
590 template <
typename =
double>
593 template <
typename =
double>
596 template <
typename =
double>
599 template <
typename =
double,
typename = Open,
typename = Closed>
602 template <
typename =
unsigned>
605 template <
typename =
double>
608 template <
typename =
double>
611 template <
typename Generator>
615 template <
typename RealType,
typename RNGType>
619 template <
typename RealType,
typename RNGType>
623 template <
typename RealType,
typename RNGType>
627 template <
typename RealType,
typename RNGType>
631 template <
typename RealType,
typename RNGType>
635 template <
typename RealType,
typename RNGType>
639 template <
typename RealType,
typename RNGType>
643 template <
typename RealType,
typename RNGType>
647 template <
typename RealType,
typename RNGType>
651 template <
typename RealType,
typename RNGType>
655 template <
typename RealType,
typename RNGType>
659 template <
typename RealType,
typename RNGType>
663 template <
typename RealType,
typename RNGType>
667 template <
typename RealType, std::
size_t Dim,
typename RNGType>
671 template <
typename RealType,
typename RNGType>
675 template <
typename RealType,
typename RNGType>
679 template <
typename RealType,
typename RNGType>
683 template <
typename RealType,
typename RNGType>
687 template <
typename RealType,
typename RNGType,
typename Left,
typename Right>
689 std::size_t, RealType *);
691 template <
typename UIntType,
typename RNGType>
695 template <
typename RealType,
typename RNGType>
699 template <
typename RealType,
typename RNGType>
703 template <
typename RealType,
typename RNGType>
705 RNGType &, std::size_t, RealType *, RealType, RealType);
707 template <
typename RealType,
typename RNGType>
709 RNGType &, std::size_t, RealType *, RealType, RealType);
711 template <
typename RealType,
typename RNGType>
713 RNGType &, std::size_t, RealType *, RealType);
715 template <
typename RealType,
typename RNGType>
717 RNGType &, std::size_t, RealType *, RealType);
719 template <
typename RealType,
typename RNGType>
721 RNGType &, std::size_t, RealType *, RealType, RealType);
723 template <
typename RealType,
typename RNGType>
725 RNGType &, std::size_t, RealType *, RealType, RealType);
727 template <
typename RealType,
typename RNGType>
729 RNGType &, std::size_t, RealType *, RealType, RealType);
731 template <
typename RealType,
typename RNGType>
733 RNGType &, std::size_t, RealType *, RealType, RealType);
735 template <
typename RealType,
typename RNGType>
737 RNGType &, std::size_t, RealType *, RealType, RealType);
739 template <
typename RealType,
typename RNGType>
741 RNGType &, std::size_t, RealType *, RealType, RealType);
743 template <
typename RealType,
typename RNGType>
745 RNGType &, std::size_t, RealType *, RealType, RealType);
747 template <
typename RealType,
typename RNGType>
749 RNGType &, std::size_t, RealType *, RealType, RealType);
751 template <
typename RealType,
typename RNGType>
753 std::size_t,
const RealType *,
const RealType *);
755 template <
typename RealType,
typename RNGType>
757 RNGType &, std::size_t, RealType *, RealType, RealType);
759 template <
typename RealType,
typename RNGType>
761 RNGType &, std::size_t, RealType *, RealType);
763 template <
typename RealType,
typename RNGType>
765 RNGType &, std::size_t, RealType *, RealType);
767 template <
typename RealType,
typename RNGType>
770 template <
typename,
typename,
typename RealType,
typename RNGType>
773 template <
typename UIntType,
typename RNGType>
776 template <
typename RealType,
typename RNGType>
778 RNGType &, std::size_t, RealType *, RealType, RealType);
780 template <
typename RealType,
typename RNGType>
782 RNGType &, std::size_t, RealType *, RealType, RealType);
786 template <MKL_INT,
int>
789 template <MKL_INT BRNG,
int Bits>
793 template <MKL_INT BRNG,
int Bits>
797 template <MKL_INT BRNG,
int Bits>
801 template <MKL_INT BRNG,
int Bits>
805 template <MKL_INT BRNG,
int Bits>
809 template <MKL_INT BRNG,
int Bits>
813 template <MKL_INT BRNG,
int Bits>
817 template <MKL_INT BRNG,
int Bits>
821 template <MKL_INT BRNG,
int Bits>
825 template <MKL_INT BRNG,
int Bits>
829 template <MKL_INT BRNG,
int Bits>
833 template <MKL_INT BRNG,
int Bits>
837 template <MKL_INT BRNG,
int Bits>
841 template <MKL_INT BRNG,
int Bits>
845 template <MKL_INT BRNG,
int Bits>
849 template <MKL_INT BRNG,
int Bits>
853 template <MKL_INT BRNG,
int Bits>
857 template <MKL_INT BRNG,
int Bits>
859 float *, std::size_t,
const float *,
const float *);
861 template <MKL_INT BRNG,
int Bits>
863 double *, std::size_t,
const double *,
const double *);
865 template <MKL_INT BRNG,
int Bits>
869 template <MKL_INT BRNG,
int Bits>
873 template <MKL_INT BRNG,
int Bits>
876 template <MKL_INT BRNG,
int Bits>
879 template <MKL_INT BRNG,
int Bits>
883 template <MKL_INT BRNG,
int Bits>
887 template <MKL_INT BRNG,
int Bits>
891 template <MKL_INT BRNG,
int Bits>
895 #endif // VSMC_HAS_MKL 899 #endif // VSMC_RNG_INTERNAL_COMMON_HPP #define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
ExtremeValue distribution.
void laplace_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating laplace random variates.
void normal_mv_distribution(RNGType &, std::size_t, RealType *, std::size_t, const RealType *, const RealType *)
Generating multivariate Normal random varaites.
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 u01_lr_distribution(RNGType &, std::size_t, RealType *)
Random walk MCMC update with test function.
Find the smallest N such that (RNGType::min() >> N) == 0
void cauchy_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generating cauchy random variates.
Find the largest N such that RNGType::max() >= (M >> (W - N)) where M = std::numeric_limits<typename ...
void lognormal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating lognormal random variates.
void rng_rand(RNGType &rng, BetaDistribution< RealType > &dist, std::size_t n, RealType *r)
Counter based RNG engine.
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.
static constexpr int value
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
typename Generator::result_type result_type
void weibull_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating weibull random variates.
Exponential distribution.
void extreme_value_distribution(RNGType &rng, std::size_t n, RealType *r, RealType a, RealType b)
Generating extreme_value random variates.
Multivariate Normal distribution.
Normal random walk proposal.
void chi_squared_distribution(RNGType &rng, std::size_t n, RealType *r, RealType df)
Generating random variates.
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 *)
Standard uniform distribution.
Multivariate Normal random walk proposal.
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 with open/closed variants.
The value of RNGMaxBits<RNGType>::value - RNGMinBits<RNGType>::value.
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.