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-19 13:47:59 +00:00
|
|
|
#ifndef SPROUT_RANDOM_NORMAL_DISTRIBUTION_HPP
|
|
|
|
#define SPROUT_RANDOM_NORMAL_DISTRIBUTION_HPP
|
|
|
|
|
|
|
|
#include <ios>
|
|
|
|
#include <istream>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2012-02-28 09:54:39 +00:00
|
|
|
#include <sprout/math/constants.hpp>
|
2012-05-09 01:15:19 +00:00
|
|
|
#include <sprout/math/sin.hpp>
|
|
|
|
#include <sprout/math/cos.hpp>
|
|
|
|
#include <sprout/math/log.hpp>
|
|
|
|
#include <sprout/math/sqrt.hpp>
|
2011-10-19 13:47:59 +00:00
|
|
|
#include <sprout/random/uniform_01.hpp>
|
|
|
|
#include <sprout/random/random_result.hpp>
|
2014-01-16 09:59:25 +00:00
|
|
|
#include <sprout/random/result.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2011-10-19 13:47:59 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
//
|
|
|
|
// normal_distribution
|
|
|
|
//
|
|
|
|
template<typename RealType = double>
|
|
|
|
class normal_distribution {
|
|
|
|
public:
|
|
|
|
typedef RealType input_type;
|
|
|
|
typedef RealType result_type;
|
|
|
|
private:
|
2013-08-09 13:14:43 +00:00
|
|
|
struct private_construct_t {};
|
2011-10-19 13:47:59 +00:00
|
|
|
public:
|
|
|
|
//
|
|
|
|
// param_type
|
|
|
|
//
|
|
|
|
class param_type {
|
|
|
|
public:
|
|
|
|
typedef normal_distribution distribution_type;
|
|
|
|
private:
|
|
|
|
RealType mean_;
|
|
|
|
RealType sigma_;
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR param_type()
|
|
|
|
: mean_(RealType(0.0))
|
|
|
|
, sigma_(RealType(1.0))
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR param_type(RealType mean_arg, RealType sigma_arg = RealType(1.0))
|
2013-03-18 10:12:21 +00:00
|
|
|
: mean_(mean_arg)
|
|
|
|
, sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg))
|
2011-10-19 13:47:59 +00:00
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR RealType mean() const SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return mean_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR RealType sigma() const SPROUT_NOEXCEPT {
|
|
|
|
return sigma_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR RealType stddev() const SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return sigma_;
|
|
|
|
}
|
|
|
|
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-19 13:47:59 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
|
|
|
param_type& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
RealType mean;
|
|
|
|
RealType sigma;
|
|
|
|
if (lhs >> mean >> std::ws >> sigma) {
|
2013-03-18 10:12:21 +00:00
|
|
|
if (sigma >= RealType(0)) {
|
2011-10-19 13:47:59 +00:00
|
|
|
rhs.mean_ = mean;
|
|
|
|
rhs.sigma_ = sigma;
|
|
|
|
} 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-19 13:47:59 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
param_type const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs << rhs.mean_ << " " << rhs.sigma_;
|
|
|
|
}
|
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-19 13:47:59 +00:00
|
|
|
return lhs.mean_ == rhs.mean_ && lhs.sigma_ == rhs.sigma_;
|
|
|
|
}
|
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-19 13:47:59 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
RealType mean_;
|
|
|
|
RealType sigma_;
|
|
|
|
RealType r1_;
|
|
|
|
RealType r2_;
|
|
|
|
RealType cached_rho_;
|
|
|
|
bool valid_;
|
|
|
|
private:
|
|
|
|
SPROUT_CONSTEXPR normal_distribution(
|
2012-10-05 15:58:56 +00:00
|
|
|
RealType mean, RealType sigma, RealType r1, RealType r2,
|
|
|
|
RealType cached_rho, bool valid,
|
2013-08-09 13:14:43 +00:00
|
|
|
private_construct_t
|
2011-10-19 13:47:59 +00:00
|
|
|
)
|
|
|
|
: mean_(mean)
|
|
|
|
, sigma_(sigma)
|
|
|
|
, r1_(r1)
|
|
|
|
, r2_(r2)
|
|
|
|
, cached_rho_(cached_rho)
|
|
|
|
, valid_(valid)
|
|
|
|
{}
|
|
|
|
template<typename Engine>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution>
|
|
|
|
generate_2(Engine const& eng, RealType r1, RealType r2, RealType cached_rho, bool valid) const {
|
2011-10-19 13:47:59 +00:00
|
|
|
return sprout::random::random_result<Engine, normal_distribution>(
|
2012-02-28 09:54:39 +00:00
|
|
|
cached_rho
|
|
|
|
* (valid
|
2013-02-14 09:02:43 +00:00
|
|
|
? sprout::cos(sprout::math::two_pi<result_type>() * r1)
|
|
|
|
: sprout::sin(sprout::math::two_pi<result_type>() * r1)
|
2012-02-28 09:54:39 +00:00
|
|
|
)
|
|
|
|
* sigma_ + mean_,
|
2011-10-19 13:47:59 +00:00
|
|
|
eng,
|
|
|
|
normal_distribution(
|
|
|
|
mean_,
|
|
|
|
sigma_,
|
|
|
|
r1,
|
|
|
|
r2,
|
|
|
|
cached_rho,
|
|
|
|
valid,
|
2013-08-09 13:14:43 +00:00
|
|
|
private_construct_t()
|
2011-10-19 13:47:59 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine, typename Random>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution>
|
|
|
|
generate_1_1(RealType r1, Random const& rnd) const {
|
|
|
|
return generate_2(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd).engine(), r1, sprout::random::result(rnd),
|
|
|
|
sprout::sqrt(-result_type(2) * sprout::math::log(result_type(1) - sprout::random::result(rnd))), true
|
2012-10-05 15:58:56 +00:00
|
|
|
);
|
2011-10-19 13:47:59 +00:00
|
|
|
}
|
|
|
|
template<typename Engine, typename Random>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution>
|
|
|
|
generate_1(Random const& rnd) const {
|
2014-01-16 09:59:25 +00:00
|
|
|
return generate_1_1<Engine>(sprout::random::result(rnd), sprout::random::next(rnd)());
|
2011-10-19 13:47:59 +00:00
|
|
|
}
|
|
|
|
template<typename Engine>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution>
|
|
|
|
generate(Engine const& eng) const {
|
2011-10-19 13:47:59 +00:00
|
|
|
return !valid_
|
|
|
|
? generate_1<Engine>(sprout::random::uniform_01<RealType>()(eng))
|
|
|
|
: generate_2(eng, r1_, r2_, cached_rho_, false)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR normal_distribution()
|
|
|
|
: mean_(RealType(0.0))
|
|
|
|
, sigma_(RealType(1.0))
|
|
|
|
, r1_(0)
|
|
|
|
, r2_(0)
|
|
|
|
, cached_rho_(0)
|
|
|
|
, valid_(false)
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR normal_distribution(RealType mean_arg, RealType sigma_arg = RealType(1.0))
|
2013-03-18 10:12:21 +00:00
|
|
|
: mean_(mean_arg)
|
|
|
|
, sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg))
|
2011-10-19 13:47:59 +00:00
|
|
|
, r1_(0)
|
|
|
|
, r2_(0)
|
|
|
|
, cached_rho_(0)
|
|
|
|
, valid_(false)
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR normal_distribution(param_type const& parm)
|
2011-10-19 13:47:59 +00:00
|
|
|
: mean_(parm.mean())
|
|
|
|
, sigma_(parm.sigma())
|
|
|
|
, r1_(0)
|
|
|
|
, r2_(0)
|
|
|
|
, cached_rho_(0)
|
|
|
|
, valid_(false)
|
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type mean() const SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return mean_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type sigma() const SPROUT_NOEXCEPT {
|
|
|
|
return sigma_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type stddev() const SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return sigma_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
2013-08-06 15:15:09 +00:00
|
|
|
return -sprout::numeric_limits<RealType>::infinity();
|
2011-10-19 13:47:59 +00:00
|
|
|
}
|
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<RealType>::infinity();
|
2011-10-19 13:47:59 +00:00
|
|
|
}
|
2013-11-09 09:13:26 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void reset() SPROUT_NOEXCEPT {
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return param_type(mean_, sigma_);
|
|
|
|
}
|
2013-11-02 09:28:18 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void param(param_type const& parm) {
|
2011-10-19 13:47:59 +00:00
|
|
|
mean_ = parm.mean();
|
|
|
|
sigma_ = parm.sigma();
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) {
|
|
|
|
if (!valid_) {
|
|
|
|
r1_ = static_cast<RealType>(sprout::random::uniform_01<RealType>()(eng));
|
|
|
|
r2_ = static_cast<RealType>(sprout::random::uniform_01<RealType>()(eng));
|
|
|
|
cached_rho_ = sprout::math::sqrt(-result_type(2) * sprout::math::log(result_type(1) - r2_));
|
|
|
|
valid_ = true;
|
|
|
|
} else {
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
return cached_rho_
|
|
|
|
* (valid_
|
|
|
|
? sprout::math::cos(sprout::math::two_pi<result_type>() * r1_)
|
|
|
|
: sprout::math::sin(sprout::math::two_pi<result_type>() * r1_))
|
|
|
|
* sigma_ + mean_
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution> const operator()(Engine const& eng) const {
|
2011-10-19 13:47:59 +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 normal_distribution(parm)(eng);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, normal_distribution> const operator()(Engine const& eng, param_type const& parm) const {
|
|
|
|
return normal_distribution(parm)(eng);
|
|
|
|
}
|
2011-10-19 13:47:59 +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-19 13:47:59 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
|
|
|
normal_distribution& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
param_type parm;
|
|
|
|
bool valid;
|
|
|
|
RealType cached_rho;
|
|
|
|
RealType r1;
|
|
|
|
RealType r2;
|
|
|
|
if (lhs >> parm >> std::ws >> valid >> std::ws >> cached_rho >> std::ws >> r1 >> std::ws >> r2) {
|
|
|
|
rhs.param(parm);
|
|
|
|
rhs.valid_ = valid;
|
|
|
|
rhs.cached_rho_ = cached_rho;
|
|
|
|
rhs.r1_ = r1;
|
|
|
|
rhs.r2_ = r2;
|
|
|
|
}
|
|
|
|
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-19 13:47:59 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
normal_distribution const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs << rhs.param() << " " << rhs.valid_ << " " << rhs.cached_rho_ << " " << rhs.r1_ << " " << rhs.r2_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(normal_distribution const& lhs, normal_distribution const& rhs) SPROUT_NOEXCEPT {
|
2012-10-05 15:58:56 +00:00
|
|
|
return lhs.param() == rhs.param()
|
|
|
|
&& lhs.valid_ == rhs.valid_
|
|
|
|
&& lhs.cached_rho_ == rhs.cached_rho_
|
|
|
|
&& lhs.r1_ == rhs.r1_ && lhs.r2_ == rhs.r2_
|
|
|
|
;
|
2011-10-19 13:47:59 +00:00
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(normal_distribution const& lhs, normal_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-19 13:47:59 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace random
|
|
|
|
|
|
|
|
using sprout::random::normal_distribution;
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_NORMAL_DISTRIBUTION_HPP
|