epoc32/include/stdapis/boost/random/triangle_distribution.hpp
branchSymbian2
changeset 2 2fe1408b6811
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/boost/random/triangle_distribution.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* boost random/triangle_distribution.hpp header file
     1.5 + *
     1.6 + * Copyright Jens Maurer 2000-2001
     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: triangle_distribution.hpp,v 1.11 2004/07/27 03:43:32 dgregor Exp $
    1.14 + *
    1.15 + * Revision history
    1.16 + *  2001-02-18  moved to individual header files
    1.17 + */
    1.18 +
    1.19 +#ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
    1.20 +#define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
    1.21 +
    1.22 +#include <cmath>
    1.23 +#include <cassert>
    1.24 +#include <boost/random/uniform_01.hpp>
    1.25 +
    1.26 +namespace boost {
    1.27 +
    1.28 +// triangle distribution, with a smallest, b most probable, and c largest
    1.29 +// value.
    1.30 +template<class RealType = double>
    1.31 +class triangle_distribution
    1.32 +{
    1.33 +public:
    1.34 +  typedef RealType input_type;
    1.35 +  typedef RealType result_type;
    1.36 +
    1.37 +  explicit triangle_distribution(result_type a = result_type(0),
    1.38 +                                 result_type b = result_type(0.5),
    1.39 +                                 result_type c = result_type(1))
    1.40 +    : _a(a), _b(b), _c(c)
    1.41 +  {
    1.42 +    assert(_a <= _b && _b <= _c);
    1.43 +    init();
    1.44 +  }
    1.45 +
    1.46 +  // compiler-generated copy ctor and assignment operator are fine
    1.47 +  result_type a() const { return _a; }
    1.48 +  result_type b() const { return _b; }
    1.49 +  result_type c() const { return _c; }
    1.50 +
    1.51 +  void 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 +    using std::sqrt;
    1.58 +#endif
    1.59 +    result_type u = eng();
    1.60 +    if( u <= q1 )
    1.61 +      return _a + p1*sqrt(u);
    1.62 +    else
    1.63 +      return _c - d3*sqrt(d2*u-d1);
    1.64 +  }
    1.65 +
    1.66 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
    1.67 +  template<class CharT, class Traits>
    1.68 +  friend std::basic_ostream<CharT,Traits>&
    1.69 +  operator<<(std::basic_ostream<CharT,Traits>& os, const triangle_distribution& td)
    1.70 +  {
    1.71 +    os << td._a << " " << td._b << " " << td._c;
    1.72 +    return os;
    1.73 +  }
    1.74 +
    1.75 +  template<class CharT, class Traits>
    1.76 +  friend std::basic_istream<CharT,Traits>&
    1.77 +  operator>>(std::basic_istream<CharT,Traits>& is, triangle_distribution& td)
    1.78 +  {
    1.79 +    is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
    1.80 +    td.init();
    1.81 +    return is;
    1.82 +  }
    1.83 +#endif
    1.84 +
    1.85 +private:
    1.86 +  void init()
    1.87 +  {
    1.88 +#ifndef BOOST_NO_STDC_NAMESPACE
    1.89 +    using std::sqrt;
    1.90 +#endif
    1.91 +    d1 = _b - _a;
    1.92 +    d2 = _c - _a;
    1.93 +    d3 = sqrt(_c - _b);
    1.94 +    q1 = d1 / d2;
    1.95 +    p1 = sqrt(d1 * d2);
    1.96 +  }
    1.97 +
    1.98 +  result_type _a, _b, _c;
    1.99 +  result_type d1, d2, d3, q1, p1;
   1.100 +};
   1.101 +
   1.102 +} // namespace boost
   1.103 +
   1.104 +#endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP