2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-14 12:23:13 +00:00
|
|
|
#ifndef SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
|
|
|
#define SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
|
|
|
|
2014-11-28 07:19:07 +00:00
|
|
|
#include <istream>
|
|
|
|
#include <ostream>
|
2011-10-14 12:23:13 +00:00
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2012-05-09 01:15:19 +00:00
|
|
|
#include <sprout/math/log.hpp>
|
2012-07-04 15:09:44 +00:00
|
|
|
#include <sprout/math/floor.hpp>
|
2011-10-14 12:23:13 +00:00
|
|
|
#include <sprout/random/random_result.hpp>
|
|
|
|
#include <sprout/random/uniform_01.hpp>
|
2014-01-17 13:09:08 +00:00
|
|
|
#include <sprout/random/results.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2011-10-14 12:23:13 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
//
|
|
|
|
// geometric_distribution
|
|
|
|
//
|
|
|
|
template<typename IntType = int, typename RealType = double>
|
|
|
|
class geometric_distribution {
|
|
|
|
public:
|
|
|
|
typedef RealType input_type;
|
|
|
|
typedef IntType result_type;
|
|
|
|
public:
|
|
|
|
//
|
|
|
|
// param_type
|
|
|
|
//
|
|
|
|
class param_type {
|
|
|
|
public:
|
|
|
|
typedef geometric_distribution distribution_type;
|
|
|
|
private:
|
|
|
|
RealType p_;
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR param_type()
|
|
|
|
: p_(RealType(0.5))
|
|
|
|
{}
|
2014-08-01 06:24:00 +00:00
|
|
|
param_type(param_type const&) = default;
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR param_type(RealType p_arg)
|
2013-03-18 10:12:21 +00:00
|
|
|
: p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg))
|
2011-10-14 12:23:13 +00:00
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return p_;
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
|
2011-10-14 12:23:13 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
2011-10-17 04:44:19 +00:00
|
|
|
param_type& rhs
|
2011-10-14 12:23:13 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
RealType p;
|
|
|
|
if (lhs >> p) {
|
2013-03-18 10:12:21 +00:00
|
|
|
if (RealType(0) < p && p < RealType(1)) {
|
2011-10-14 12:23:13 +00:00
|
|
|
rhs.p_ = p;
|
|
|
|
} else {
|
|
|
|
lhs.setstate(std::ios_base::failbit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
|
2011-10-14 12:23:13 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
param_type const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs << rhs.p_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return lhs.p_ == rhs.p_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR RealType init_log_1mp(RealType p) {
|
2013-04-24 13:48:36 +00:00
|
|
|
return sprout::math::log(1 - p);
|
2011-10-14 12:23:13 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
public:
|
|
|
|
RealType p_;
|
|
|
|
RealType log_1mp_;
|
|
|
|
private:
|
|
|
|
template<typename Engine, typename Random>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> generate_1(Random const& rnd) const {
|
|
|
|
return sprout::random::random_result<Engine, geometric_distribution>(
|
2014-01-16 09:59:25 +00:00
|
|
|
static_cast<IntType>(sprout::math::floor(sprout::math::log(RealType(1) - sprout::random::result(rnd)) / log_1mp_)),
|
|
|
|
sprout::random::next(rnd).engine(),
|
2011-10-14 12:23:13 +00:00
|
|
|
*this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> generate(Engine const& eng) const {
|
|
|
|
return generate_1<Engine>(sprout::random::uniform_01<RealType>()(eng));
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR geometric_distribution()
|
|
|
|
: p_(RealType(0.5))
|
|
|
|
, log_1mp_(init_log_1mp(RealType(0.5)))
|
|
|
|
{}
|
2014-08-01 06:24:00 +00:00
|
|
|
geometric_distribution(geometric_distribution const&) = default;
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR geometric_distribution(RealType p_arg)
|
2013-03-18 10:12:21 +00:00
|
|
|
: p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg))
|
2011-10-14 12:23:13 +00:00
|
|
|
, log_1mp_(init_log_1mp(p_arg))
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR geometric_distribution(param_type const& parm)
|
2011-10-14 12:23:13 +00:00
|
|
|
: p_(parm.p())
|
|
|
|
, log_1mp_(init_log_1mp(parm.p()))
|
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type p() const SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return p_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
2013-08-06 15:15:09 +00:00
|
|
|
return sprout::numeric_limits<result_type>::max();
|
2011-10-14 12:23:13 +00:00
|
|
|
}
|
2013-11-09 09:13:26 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void reset() SPROUT_NOEXCEPT {}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return param_type(p_);
|
|
|
|
}
|
2013-11-02 09:28:18 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void param(param_type const& parm) {
|
2011-10-14 12:23:13 +00:00
|
|
|
p_ = parm.p();
|
|
|
|
log_1mp_ = init_log_1mp(p_);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
|
|
|
|
return static_cast<result_type>(
|
|
|
|
sprout::math::floor(sprout::math::log(RealType(1) - static_cast<RealType>(sprout::random::uniform_01<RealType>()(eng))) / log_1mp_)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> const operator()(Engine const& eng) const {
|
2011-10-14 12:23:13 +00:00
|
|
|
return generate(eng);
|
|
|
|
}
|
2013-11-05 14:46:23 +00:00
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const& parm) const {
|
|
|
|
return geometric_distribution(parm)(eng);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> const operator()(Engine const& eng, param_type const& parm) const {
|
|
|
|
return geometric_distribution(parm)(eng);
|
|
|
|
}
|
2011-10-14 12:23:13 +00:00
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
|
2011-10-14 12:23:13 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
2011-10-17 04:44:19 +00:00
|
|
|
geometric_distribution& rhs
|
2011-10-14 12:23:13 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
param_type parm;
|
2011-10-19 13:47:59 +00:00
|
|
|
if (lhs >> parm) {
|
|
|
|
rhs.param(parm);
|
|
|
|
}
|
2011-10-14 12:23:13 +00:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
|
2011-10-14 12:23:13 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
geometric_distribution const& rhs
|
|
|
|
)
|
|
|
|
{
|
2011-10-17 04:44:19 +00:00
|
|
|
return lhs << rhs.param();
|
2011-10-14 12:23:13 +00:00
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(geometric_distribution const& lhs, geometric_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return lhs.param() == rhs.param();
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(geometric_distribution const& lhs, geometric_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-14 12:23:13 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace random
|
|
|
|
|
|
|
|
using sprout::random::geometric_distribution;
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|