32 #ifndef MCKL_RANDOM_DIRICHLET_DISTRIBUTION_HPP 33 #define MCKL_RANDOM_DIRICHLET_DISTRIBUTION_HPP 42 template <
typename RealType>
44 std::size_t dim, RealType *alpha)
46 for (std::size_t i = 0; i != dim; ++i) {
54 template <
typename RealType>
56 std::size_t n, std::size_t dim, RealType *r)
58 for (std::size_t i = 0; i != n; ++i, r += dim) {
59 RealType s = std::accumulate(r, r + dim, const_zero<RealType>());
60 mul<RealType>(dim, 1 / s, r, r);
66 template <
typename RealType,
typename RNGType>
68 std::size_t dim,
const RealType alpha)
78 template <
typename RealType,
typename RNGType>
80 std::size_t dim,
const RealType *alpha)
87 for (std::size_t i = 0; i != dim; ++i) {
90 for (std::size_t j = 0; j != n; ++j, t += dim) {
97 template <
typename RealType>
114 : alpha_(dim, 1), is_scalar_(true)
117 this->dim(), this->alpha()),
118 "**DirichletDistribution** constructed with invalid " 123 : alpha_(dim, alpha), is_scalar_(true)
126 this->dim(), this->alpha()),
127 "**DirichletDistribution** constructed with invalid " 132 : alpha_(alpha, alpha + dim), is_scalar_(false)
135 this->dim(), this->alpha()),
136 "**DirichletDistribution** constructed with invalid " 140 std::size_t
dim()
const {
return alpha_.size(); }
145 const param_type ¶m1,
const param_type ¶m2)
147 if (param1.alpha_ != param2.alpha_) {
150 if (param1.is_scalar_ != param2.is_scalar_) {
157 const param_type ¶m1,
const param_type ¶m2)
159 return !(param1 == param2);
162 template <
typename CharT,
typename Traits>
164 std::basic_ostream<CharT, Traits> &os,
const param_type ¶m)
170 os << param.alpha_ <<
' ';
171 os << param.is_scalar_;
176 template <
typename CharT,
typename Traits>
178 std::basic_istream<CharT, Traits> &is, param_type ¶m)
185 is >> std::ws >> tmp.alpha_;
186 is >> std::ws >> tmp.is_scalar_;
189 param = std::move(tmp);
191 is.setstate(std::ios_base::failbit);
228 : param_(
std::move(param))
233 template <
typename OutputIter>
234 OutputIter
min(OutputIter first)
const 236 return std::fill_n(first, dim(), 0);
239 template <
typename OutputIter>
240 OutputIter
max(OutputIter first)
const 242 return std::fill_n(first, dim(), 1);
247 std::size_t
dim()
const {
return param_.dim(); }
261 param_ = std::move(param);
265 template <
typename RNGType>
268 operator()(rng, r, param_);
271 template <
typename RNGType>
274 generate(rng, r, param);
277 template <
typename RNGType>
280 operator()(rng, n, r, param_);
283 template <
typename RNGType>
287 if (param.is_scalar_) {
297 if (dist1.param_ != dist2.param_) {
306 return !(dist1 == dist2);
309 template <
typename CharT,
typename Traits>
322 template <
typename CharT,
typename Traits>
331 is >> std::ws >> param;
333 dist.param_ = std::move(param);
342 template <
typename RNGType>
345 const std::size_t d = param.
dim();
346 if (param.is_scalar_) {
350 for (std::size_t i = 0; i != d; ++i) {
355 result_type s = std::accumulate(r, r + d, const_zero<result_type>());
356 mul<result_type>(d, 1 / s, r, r);
360 template <
typename RealType,
typename RNGType>
364 distribution(rng, r);
367 template <
typename RealType,
typename RNGType>
369 std::size_t n, RealType *r)
371 distribution(rng, n, r);
376 #endif // MCKL_RANDOM_DIRICHLET_DISTRIBUTION_HPP param_type(std::size_t dim, const result_type *alpha)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const distribution_type &dist)
OutputIter max(OutputIter first) const
friend bool operator==(const distribution_type &dist1, const distribution_type &dist2)
DirichletDistribution(param_type &¶m)
#define MCKL_PUSH_CLANG_WARNING(warning)
param_type(std::size_t dim=1)
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
void operator()(RNGType &rng, result_type *r)
void gamma_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
void param(const param_type ¶m)
void dirichlet_distribution(RNGType &rng, std::size_t n, RealType *r, std::size_t dim, const RealType alpha)
friend bool operator==(const param_type ¶m1, const param_type ¶m2)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, distribution_type &dist)
friend bool operator!=(const param_type ¶m1, const param_type ¶m2)
void operator()(RNGType &rng, result_type *r, const param_type ¶m)
const result_type * alpha() const
DirichletDistribution(const param_type ¶m)
void dirichlet_distribution_avg(std::size_t n, std::size_t dim, RealType *r)
OutputIter min(OutputIter first) const
DirichletDistribution(std::size_t dim, result_type alpha)
Construct a distribution with scalar shape.
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
void operator()(RNGType &rng, std::size_t n, result_type *r, const param_type ¶m)
#define MCKL_POP_CLANG_WARNING
void param(param_type &¶m)
DirichletDistribution(std::size_t dim, const result_type *alpha)
Construct a distribution with vector shapes.
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_ASSERT_REAL_TYPE(Name)
friend bool operator!=(const distribution_type &dist1, const distribution_type &dist2)
void operator()(RNGType &rng, std::size_t n, result_type *r)
param_type(std::size_t dim, result_type alpha)
const param_type & param() const
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, param_type ¶m)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const param_type ¶m)
bool dirichlet_distribution_check_param(std::size_t dim, RealType *alpha)
void runtime_assert(bool cond, const char *msg, bool soft=false)
const result_type * alpha() const
DirichletDistribution(std::size_t dim=1)
Construct a distribution with scalar shape.