epoc32/include/stdapis/boost/random/normal_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/normal_distribution.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,111 @@
     1.4 +/* boost random/normal_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: normal_distribution.hpp,v 1.20 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_NORMAL_DISTRIBUTION_HPP
    1.20 +#define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
    1.21 +
    1.22 +#include <cmath>
    1.23 +#include <cassert>
    1.24 +#include <iostream>
    1.25 +#include <boost/limits.hpp>
    1.26 +#include <boost/static_assert.hpp>
    1.27 +
    1.28 +namespace boost {
    1.29 +
    1.30 +// deterministic polar method, uses trigonometric functions
    1.31 +template<class RealType = double>
    1.32 +class normal_distribution
    1.33 +{
    1.34 +public:
    1.35 +  typedef RealType input_type;
    1.36 +  typedef RealType result_type;
    1.37 +
    1.38 +#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
    1.39 +    BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
    1.40 +#endif
    1.41 +
    1.42 +  explicit normal_distribution(const result_type& mean = result_type(0),
    1.43 +                               const result_type& sigma = result_type(1))
    1.44 +    : _mean(mean), _sigma(sigma), _valid(false)
    1.45 +  {
    1.46 +    assert(sigma >= result_type(0));
    1.47 +  }
    1.48 +
    1.49 +  // compiler-generated copy constructor is NOT fine, need to purge cache
    1.50 +  normal_distribution(const normal_distribution& other)
    1.51 +    : _mean(other._mean), _sigma(other._sigma), _valid(false)
    1.52 +  {
    1.53 +  }
    1.54 +
    1.55 +  // compiler-generated copy ctor and assignment operator are fine
    1.56 +
    1.57 +  RealType mean() const { return _mean; }
    1.58 +  RealType sigma() const { return _sigma; }
    1.59 +
    1.60 +  void reset() { _valid = false; }
    1.61 +
    1.62 +  template<class Engine>
    1.63 +  result_type operator()(Engine& eng)
    1.64 +  {
    1.65 +#ifndef BOOST_NO_STDC_NAMESPACE
    1.66 +    // allow for Koenig lookup
    1.67 +    using std::sqrt; using std::log; using std::sin; using std::cos;
    1.68 +#endif
    1.69 +    if(!_valid) {
    1.70 +      _r1 = eng();
    1.71 +      _r2 = eng();
    1.72 +      _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
    1.73 +      _valid = true;
    1.74 +    } else {
    1.75 +      _valid = false;
    1.76 +    }
    1.77 +    // Can we have a boost::mathconst please?
    1.78 +    const result_type pi = result_type(3.14159265358979323846);
    1.79 +    
    1.80 +    return _cached_rho * (_valid ?
    1.81 +                          cos(result_type(2)*pi*_r1) :
    1.82 +                          sin(result_type(2)*pi*_r1))
    1.83 +      * _sigma + _mean;
    1.84 +  }
    1.85 +
    1.86 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
    1.87 +  template<class CharT, class Traits>
    1.88 +  friend std::basic_ostream<CharT,Traits>&
    1.89 +  operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
    1.90 +  {
    1.91 +    os << nd._mean << " " << nd._sigma << " "
    1.92 +       << nd._valid << " " << nd._cached_rho << " " << nd._r1;
    1.93 +    return os;
    1.94 +  }
    1.95 +
    1.96 +  template<class CharT, class Traits>
    1.97 +  friend std::basic_istream<CharT,Traits>&
    1.98 +  operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
    1.99 +  {
   1.100 +    is >> std::ws >> nd._mean >> std::ws >> nd._sigma
   1.101 +       >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
   1.102 +       >> std::ws >> nd._r1;
   1.103 +    return is;
   1.104 +  }
   1.105 +#endif
   1.106 +private:
   1.107 +  result_type _mean, _sigma;
   1.108 +  result_type _r1, _r2, _cached_rho;
   1.109 +  bool _valid;
   1.110 +};
   1.111 +
   1.112 +} // namespace boost
   1.113 +
   1.114 +#endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP