1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/gamma_distribution.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,133 @@
1.4 +/* boost random/gamma_distribution.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2002
1.7 + * Distributed under the Boost Software License, Version 1.0. (See
1.8 + * accompanying file LICENSE_1_0.txt or copy at
1.9 + * http://www.boost.org/LICENSE_1_0.txt)
1.10 + *
1.11 + * See http://www.boost.org for most recent version including documentation.
1.12 + *
1.13 + * $Id: gamma_distribution.hpp,v 1.9 2004/07/27 03:43:32 dgregor Exp $
1.14 + *
1.15 + */
1.16 +
1.17 +#ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
1.18 +#define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
1.19 +
1.20 +#include <cmath>
1.21 +#include <cassert>
1.22 +#include <boost/limits.hpp>
1.23 +#include <boost/static_assert.hpp>
1.24 +#include <boost/random/exponential_distribution.hpp>
1.25 +
1.26 +namespace boost {
1.27 +
1.28 +// Knuth
1.29 +template<class RealType = double>
1.30 +class gamma_distribution
1.31 +{
1.32 +public:
1.33 + typedef RealType input_type;
1.34 + typedef RealType result_type;
1.35 +
1.36 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
1.37 + BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
1.38 +#endif
1.39 +
1.40 + explicit gamma_distribution(const result_type& alpha = result_type(1))
1.41 + : _exp(result_type(1)), _alpha(alpha)
1.42 + {
1.43 + assert(alpha > result_type(0));
1.44 + init();
1.45 + }
1.46 +
1.47 + // compiler-generated copy ctor and assignment operator are fine
1.48 +
1.49 + RealType alpha() const { return _alpha; }
1.50 +
1.51 + void reset() { _exp.reset(); }
1.52 +
1.53 + template<class Engine>
1.54 + result_type operator()(Engine& eng)
1.55 + {
1.56 +#ifndef BOOST_NO_STDC_NAMESPACE
1.57 + // allow for Koenig lookup
1.58 + using std::tan; using std::sqrt; using std::exp; using std::log;
1.59 + using std::pow;
1.60 +#endif
1.61 + if(_alpha == result_type(1)) {
1.62 + return _exp(eng);
1.63 + } else if(_alpha > result_type(1)) {
1.64 + // Can we have a boost::mathconst please?
1.65 + const result_type pi = result_type(3.14159265358979323846);
1.66 + for(;;) {
1.67 + result_type y = tan(pi * eng());
1.68 + result_type x = sqrt(result_type(2)*_alpha-result_type(1))*y
1.69 + + _alpha-result_type(1);
1.70 + if(x <= result_type(0))
1.71 + continue;
1.72 + if(eng() >
1.73 + (result_type(1)+y*y) * exp((_alpha-result_type(1))
1.74 + *log(x/(_alpha-result_type(1)))
1.75 + - sqrt(result_type(2)*_alpha
1.76 + -result_type(1))*y))
1.77 + continue;
1.78 + return x;
1.79 + }
1.80 + } else /* alpha < 1.0 */ {
1.81 + for(;;) {
1.82 + result_type u = eng();
1.83 + result_type y = _exp(eng);
1.84 + result_type x, q;
1.85 + if(u < _p) {
1.86 + x = exp(-y/_alpha);
1.87 + q = _p*exp(-x);
1.88 + } else {
1.89 + x = result_type(1)+y;
1.90 + q = _p + (result_type(1)-_p) * pow(x, _alpha-result_type(1));
1.91 + }
1.92 + if(u >= q)
1.93 + continue;
1.94 + return x;
1.95 + }
1.96 + }
1.97 + }
1.98 +
1.99 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1.100 + template<class CharT, class Traits>
1.101 + friend std::basic_ostream<CharT,Traits>&
1.102 + operator<<(std::basic_ostream<CharT,Traits>& os, const gamma_distribution& gd)
1.103 + {
1.104 + os << gd._alpha;
1.105 + return os;
1.106 + }
1.107 +
1.108 + template<class CharT, class Traits>
1.109 + friend std::basic_istream<CharT,Traits>&
1.110 + operator>>(std::basic_istream<CharT,Traits>& is, gamma_distribution& gd)
1.111 + {
1.112 + is >> std::ws >> gd._alpha;
1.113 + gd.init();
1.114 + return is;
1.115 + }
1.116 +#endif
1.117 +
1.118 +private:
1.119 + void init()
1.120 + {
1.121 +#ifndef BOOST_NO_STDC_NAMESPACE
1.122 + // allow for Koenig lookup
1.123 + using std::exp;
1.124 +#endif
1.125 + _p = exp(result_type(1)) / (_alpha + exp(result_type(1)));
1.126 + }
1.127 +
1.128 + exponential_distribution<RealType> _exp;
1.129 + result_type _alpha;
1.130 + // some data precomputed from the parameters
1.131 + result_type _p;
1.132 +};
1.133 +
1.134 +} // namespace boost
1.135 +
1.136 +#endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP