diff options
author | Edward Smith-Rowland <3dw4rd@verizon.net> | 2012-09-25 03:26:36 +0000 |
---|---|---|
committer | Edward Smith-Rowland <emsr@gcc.gnu.org> | 2012-09-25 03:26:36 +0000 |
commit | 21a8ccc02f643458b82780ac1b884f8454b89871 (patch) | |
tree | 8d6734d639f87cf43341496655f951ebde7ad300 /libstdc++-v3/include/ext/random | |
parent | 1ab47844f5646b949bf6a2f2a1e591b1a7db41af (diff) | |
download | gcc-21a8ccc02f643458b82780ac1b884f8454b89871.zip gcc-21a8ccc02f643458b82780ac1b884f8454b89871.tar.gz gcc-21a8ccc02f643458b82780ac1b884f8454b89871.tar.bz2 |
Implement the K-distribution as an extension.
From-SVN: r191688
Diffstat (limited to 'libstdc++-v3/include/ext/random')
-rw-r--r-- | libstdc++-v3/include/ext/random | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index 97bd014..c7321a9 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -1596,6 +1596,250 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const pareto_distribution<_RealType>& __d2) { return !(__d1 == __d2); } + + /** + * @brief A K continuous distribution for random numbers. + * + * The formula for the K probability density function is + * @f[ + * p(x|\lambda, \mu, \nu) = \frac{2}{x} + * \left(\frac{\lambda\nu x}{\mu}\right)^{\frac{\lambda + \nu}{2}} + * \frac{1}{\Gamma(\lambda)\Gamma(\nu)} + * K_{\nu - \lambda}\left(2\sqrt{\frac{\lambda\nu x}{\mu}}\right) + * @f] + * where @f$I_0(z)@f$ is the modified Bessel function of the second kind + * of order @f$\nu - \lambda@f$ and @f$\lambda > 0@f$, @f$\mu > 0@f$ + * and @f$\nu > 0@f$. + * + * <table border=1 cellpadding=10 cellspacing=0> + * <caption align=top>Distribution Statistics</caption> + * <tr><td>Mean</td><td>@f$\mu@f$</td></tr> + * <tr><td>Variance</td><td>@f$\mu^2\frac{\lambda + \nu + 1}{\lambda\nu}@f$</td></tr> + * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr> + * </table> + */ + template<typename _RealType = double> + class + k_distribution + { + static_assert(std::is_floating_point<_RealType>::value, + "template argument not a floating point type"); + + public: + /** The type of the range of the distribution. */ + typedef _RealType result_type; + /** Parameter type. */ + struct param_type + { + typedef k_distribution<result_type> distribution_type; + + param_type(result_type __lambda_val = result_type(1), + result_type __mu_val = result_type(1), + result_type __nu_val = result_type(1)) + : _M_lambda(__lambda_val), _M_mu(__mu_val), _M_nu(__nu_val) + { + _GLIBCXX_DEBUG_ASSERT(_M_lambda > result_type(0)); + _GLIBCXX_DEBUG_ASSERT(_M_mu > result_type(0)); + _GLIBCXX_DEBUG_ASSERT(_M_nu > result_type(0)); + } + + result_type + lambda() const + { return _M_lambda; } + + result_type + mu() const + { return _M_mu; } + + result_type + nu() const + { return _M_nu; } + + friend bool + operator==(const param_type& __p1, const param_type& __p2) + { return __p1._M_lambda == __p2._M_lambda + && __p1._M_mu == __p2._M_mu + && __p1._M_nu == __p2._M_nu; } + + private: + void _M_initialize(); + + result_type _M_lambda; + result_type _M_mu; + result_type _M_nu; + }; + + /** + * @brief Constructors. + */ + explicit + k_distribution(result_type __lambda_val = result_type(1), + result_type __mu_val = result_type(1), + result_type __nu_val = result_type(1)) + : _M_param(__lambda_val, __mu_val, __nu_val), + _M_gd1(__lambda_val, result_type(1) / __lambda_val), + _M_gd2(__nu_val, __mu_val / __nu_val) + { } + + explicit + k_distribution(const param_type& __p) + : _M_param(__p), + _M_gd1(__p.lambda(), result_type(1) / __p.lambda()), + _M_gd2(__p.nu(), __p.mu() / __p.nu()) + { } + + /** + * @brief Resets the distribution state. + */ + void + reset() + { + _M_gd1.reset(); + _M_gd2.reset(); + } + + /** + * @brief Return the parameters of the distribution. + */ + result_type + lambda() const + { return _M_param.lambda(); } + + result_type + mu() const + { return _M_param.mu(); } + + result_type + nu() const + { return _M_param.nu(); } + + /** + * @brief Returns the parameter set of the distribution. + */ + param_type + param() const + { return _M_param; } + + /** + * @brief Sets the parameter set of the distribution. + * @param __param The new parameter set of the distribution. + */ + void + param(const param_type& __param) + { _M_param = __param; } + + /** + * @brief Returns the greatest lower bound value of the distribution. + */ + result_type + min() const + { return result_type(0); } + + /** + * @brief Returns the least upper bound value of the distribution. + */ + result_type + max() const + { return std::numeric_limits<result_type>::max(); } + + /** + * @brief Generating functions. + */ + template<typename _UniformRandomNumberGenerator> + result_type + operator()(_UniformRandomNumberGenerator&); + + template<typename _UniformRandomNumberGenerator> + result_type + operator()(_UniformRandomNumberGenerator&, const param_type&); + + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng) + { this->__generate(__f, __t, __urng, this->param()); } + + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { this->__generate_impl(__f, __t, __urng, __p); } + + template<typename _UniformRandomNumberGenerator> + void + __generate(result_type* __f, result_type* __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p) + { this->__generate_impl(__f, __t, __urng, __p); } + + /** + * @brief Return true if two K distributions have + * the same parameters and the sequences that would + * be generated are equal. + */ + friend bool + operator==(const k_distribution& __d1, + const k_distribution& __d2) + { return (__d1.param() == __d2.param() + && __d1._M_gd1 == __d2._M_gd1 + && __d1._M_gd2 == __d2._M_gd2); } + + /** + * @brief Inserts a %k_distribution random number distribution + * @p __x into the output stream @p __os. + * + * @param __os An output stream. + * @param __x A %k_distribution random number distribution. + * + * @returns The output stream with the state of @p __x inserted or in + * an error state. + */ + template<typename _RealType1, typename _CharT, typename _Traits> + friend std::basic_ostream<_CharT, _Traits>& + operator<<(std::basic_ostream<_CharT, _Traits>&, + const k_distribution<_RealType1>&); + + /** + * @brief Extracts a %k_distribution random number distribution + * @p __x from the input stream @p __is. + * + * @param __is An input stream. + * @param __x A %k_distribution random number + * generator engine. + * + * @returns The input stream with @p __x extracted or in an error state. + */ + template<typename _RealType1, typename _CharT, typename _Traits> + friend std::basic_istream<_CharT, _Traits>& + operator>>(std::basic_istream<_CharT, _Traits>&, + k_distribution<_RealType1>&); + + private: + template<typename _ForwardIterator, + typename _UniformRandomNumberGenerator> + void + __generate_impl(_ForwardIterator __f, _ForwardIterator __t, + _UniformRandomNumberGenerator& __urng, + const param_type& __p); + + param_type _M_param; + + std::gamma_distribution<result_type> _M_gd1; + std::gamma_distribution<result_type> _M_gd2; + }; + + /** + * @brief Return true if two K distributions are not equal. + */ + template<typename _RealType> + inline bool + operator!=(const k_distribution<_RealType>& __d1, + const k_distribution<_RealType>& __d2) + { return !(__d1 == __d2); } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace __gnu_cxx |