32 #ifndef MCKL_RANDOM_GAMMA_DISTRIBUTION_HPP 33 #define MCKL_RANDOM_GAMMA_DISTRIBUTION_HPP 43 template <
typename RealType>
46 return alpha > 0 && beta > 0;
57 template <typename RealType>
63 if (alpha < static_cast<RealType>(0.6)) {
65 }
else if (alpha < 1) {
67 }
else if (alpha > 1) {
80 d_ =
std::pow(alpha, alpha / (1 - alpha)) * (1 - alpha);
84 d_ = alpha - const_one<RealType>() / 3;
92 RealType
d()
const {
return d_; }
93 RealType
c()
const {
return c_; }
101 if (c1.d_ != c2.d_) {
104 if (c1.c_ != c2.c_) {
107 if (c1.algorithm_ != c2.algorithm_) {
122 template <std::
size_t K,
typename RealType,
typename RNGType>
124 RealType *r, RealType alpha, RealType beta,
128 const RealType d = constant.
d();
129 const RealType c = constant.
c();
130 RealType *
const u = s.data();
131 RealType *
const e = s.data() + n;
132 RealType *
const x = s.data() + n * 2;
136 mul(n, static_cast<RealType>(-1), e, e);
137 for (std::size_t i = 0; i != n; ++i) {
141 u[i] = d + alpha * u[i];
151 for (std::size_t i = 0; i != n; ++i) {
160 template <std::
size_t K,
typename RealType,
typename RNGType>
162 RealType *r, RealType, RealType beta,
166 const RealType d = constant.
d();
167 const RealType c = constant.
c();
168 RealType *
const u = s.data();
169 RealType *
const e = s.data() + n;
170 RealType *
const x = s.data() + n * 2;
173 log(n * 2, s.data(), s.data());
174 mul(n * 2, static_cast<RealType>(-1), s.data(), s.data());
183 for (std::size_t i = 0; i != n; ++i) {
192 template <std::
size_t K,
typename RealType,
typename RNGType>
194 RealType *r, RealType, RealType beta,
198 const RealType d = constant.
d();
199 const RealType c = constant.
c();
200 RealType *
const u = s.data();
201 RealType *
const e = s.data() + n;
202 RealType *
const v = s.data() + n * 2;
203 RealType *
const w = s.data() + n * 3;
204 RealType *
const x = s.data() + n * 4;
208 rng, n, w, const_zero<RealType>(), const_one<RealType>());
209 muladd(n, c, w, const_one<RealType>(), v);
211 for (std::size_t i = 0; i != n; ++i) {
223 muladd(n, -static_cast<RealType>(0.0331), e, const_one<RealType>(), e);
224 mul(n, d * beta, v, x);
227 for (std::size_t i = 0; i != n; ++i) {
231 e[i] = w[i] * w[i] / 2 + d * (1 - v[i] +
std::log(v[i]));
241 template <std::
size_t,
typename RealType,
typename RNGType>
243 RealType *r, RealType, RealType beta,
253 template <std::
size_t K,
typename RealType,
typename RNGType>
255 RealType *r, RealType alpha, RealType beta,
260 return gamma_distribution_impl_t<K>(
261 rng, n, r, alpha, beta, constant);
263 return gamma_distribution_impl_w<K>(
264 rng, n, r, alpha, beta, constant);
266 return gamma_distribution_impl_n<K>(
267 rng, n, r, alpha, beta, constant);
269 return gamma_distribution_impl_e<K>(
270 rng, n, r, alpha, beta, constant);
277 template <
typename RealType,
typename RNGType>
279 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
284 std::size_t m = internal::gamma_distribution_impl<k>(
285 rng, k, r, alpha, beta, constant);
293 internal::gamma_distribution_impl<k>(rng, n, r, alpha, beta, constant);
298 for (std::size_t i = 0; i != n; ++i) {
304 template <
typename RealType,
typename RNGType>
313 template <
typename RealType>
336 return constant_ == other.constant_;
339 template <
typename CharT,
typename Traits>
340 void ostream(std::basic_ostream<CharT, Traits> &)
const 344 template <
typename CharT,
typename Traits>
345 void istream(std::basic_istream<CharT, Traits> &)
350 template <
typename RNGType>
353 if (param == param_) {
354 return generate(rng, param_, constant_);
360 return generate(rng, param, constant);
363 template <
typename RNGType>
370 r = generate_t(rng, param, constant);
373 r = generate_w(rng, param, constant);
376 r = generate_n(rng, param, constant);
379 r = generate_e(rng, param, constant);
383 return param.
beta() * r;
386 template <
typename RNGType>
394 if (u > constant.
d()) {
397 u = constant.
d() + param.
alpha() * u;
406 template <
typename RNGType>
418 }
while (u + e < constant.
d() + r);
423 template <
typename RNGType>
436 v = 1 + constant.
c() * w;
440 e = 1 -
static_cast<result_type>(0.0331) * (w * w) * (w * w);
442 return constant.
d() * v;
445 e = w * w / 2 + constant.
d() * (1 - v +
std::log(v));
447 return constant.
d() * v;
452 template <
typename RNGType>
466 #endif // MCKL_RANDOM_GAMMA_DISTRIBUTION_HPP #define MCKL_DEFINE_RANDOM_DISTRIBUTION_2( Name, name, T, T1, p1, v1, T2, p2, v2)
void pow(std::size_t n, const float *a, const float *b, float *y)
void mul(std::size_t n, const float *a, const float *b, float *y)
void normal_distribution(MKLEngine< BRNG, Bits > &rng, std::size_t n, float *r, float mean, float stddev)
GammaDistributionAlgorithm algorithm() const
#define MCKL_PUSH_CLANG_WARNING(warning)
GammaDistribution< RealType > distribution_type
void log(std::size_t n, const float *a, float *y)
GammaDistributionAlgorithm
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
void gamma_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
std::size_t gamma_distribution_impl_t(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const GammaDistributionConstant< RealType > &constant)
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_RAND(Name, T)
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
void abs(std::size_t n, const float *a, float *y)
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
std::size_t gamma_distribution_impl_n(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &constant)
result_type alpha() const
Standard uniform distribution on (0, 1)
void sqr(std::size_t n, const float *a, float *y)
#define MCKL_PUSH_INTEL_WARNING(wid)
GammaDistributionConstant(RealType alpha=1, RealType=1)
void sqrt(std::size_t n, const float *a, float *y)
void exp(std::size_t n, const float *a, float *y)
bool gamma_distribution_check_param(RealType alpha, RealType beta)
#define MCKL_POP_CLANG_WARNING
#define MCKL_ALIGNMENT
The default alignment for scalar type.
bool is_equal(const float &v1, const float &v2)
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_ASSERT_REAL_TYPE(Name)
std::size_t gamma_distribution_impl_w(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &constant)
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
std::size_t gamma_distribution_impl_e(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &)
friend bool operator==(const GammaDistributionConstant< RealType > &c1, const GammaDistributionConstant< RealType > &c2)
std::size_t gamma_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const GammaDistributionConstant< RealType > &constant)
#define MCKL_POP_INTEL_WARNING
void add(std::size_t n, const float *a, const float *b, float *y)