epoc32/include/stdapis/boost/random/normal_distribution.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/* boost random/normal_distribution.hpp header file
williamr@2
     2
 *
williamr@2
     3
 * Copyright Jens Maurer 2000-2001
williamr@2
     4
 * Distributed under the Boost Software License, Version 1.0. (See
williamr@2
     5
 * accompanying file LICENSE_1_0.txt or copy at
williamr@2
     6
 * http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     7
 *
williamr@2
     8
 * See http://www.boost.org for most recent version including documentation.
williamr@2
     9
 *
williamr@2
    10
 * $Id: normal_distribution.hpp,v 1.20 2004/07/27 03:43:32 dgregor Exp $
williamr@2
    11
 *
williamr@2
    12
 * Revision history
williamr@2
    13
 *  2001-02-18  moved to individual header files
williamr@2
    14
 */
williamr@2
    15
williamr@2
    16
#ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
williamr@2
    17
#define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
williamr@2
    18
williamr@2
    19
#include <cmath>
williamr@2
    20
#include <cassert>
williamr@2
    21
#include <iostream>
williamr@2
    22
#include <boost/limits.hpp>
williamr@2
    23
#include <boost/static_assert.hpp>
williamr@2
    24
williamr@2
    25
namespace boost {
williamr@2
    26
williamr@2
    27
// deterministic polar method, uses trigonometric functions
williamr@2
    28
template<class RealType = double>
williamr@2
    29
class normal_distribution
williamr@2
    30
{
williamr@2
    31
public:
williamr@2
    32
  typedef RealType input_type;
williamr@2
    33
  typedef RealType result_type;
williamr@2
    34
williamr@2
    35
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
williamr@2
    36
    BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
williamr@2
    37
#endif
williamr@2
    38
williamr@2
    39
  explicit normal_distribution(const result_type& mean = result_type(0),
williamr@2
    40
                               const result_type& sigma = result_type(1))
williamr@2
    41
    : _mean(mean), _sigma(sigma), _valid(false)
williamr@2
    42
  {
williamr@2
    43
    assert(sigma >= result_type(0));
williamr@2
    44
  }
williamr@2
    45
williamr@2
    46
  // compiler-generated copy constructor is NOT fine, need to purge cache
williamr@2
    47
  normal_distribution(const normal_distribution& other)
williamr@2
    48
    : _mean(other._mean), _sigma(other._sigma), _valid(false)
williamr@2
    49
  {
williamr@2
    50
  }
williamr@2
    51
williamr@2
    52
  // compiler-generated copy ctor and assignment operator are fine
williamr@2
    53
williamr@2
    54
  RealType mean() const { return _mean; }
williamr@2
    55
  RealType sigma() const { return _sigma; }
williamr@2
    56
williamr@2
    57
  void reset() { _valid = false; }
williamr@2
    58
williamr@2
    59
  template<class Engine>
williamr@2
    60
  result_type operator()(Engine& eng)
williamr@2
    61
  {
williamr@2
    62
#ifndef BOOST_NO_STDC_NAMESPACE
williamr@2
    63
    // allow for Koenig lookup
williamr@2
    64
    using std::sqrt; using std::log; using std::sin; using std::cos;
williamr@2
    65
#endif
williamr@2
    66
    if(!_valid) {
williamr@2
    67
      _r1 = eng();
williamr@2
    68
      _r2 = eng();
williamr@2
    69
      _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
williamr@2
    70
      _valid = true;
williamr@2
    71
    } else {
williamr@2
    72
      _valid = false;
williamr@2
    73
    }
williamr@2
    74
    // Can we have a boost::mathconst please?
williamr@2
    75
    const result_type pi = result_type(3.14159265358979323846);
williamr@2
    76
    
williamr@2
    77
    return _cached_rho * (_valid ?
williamr@2
    78
                          cos(result_type(2)*pi*_r1) :
williamr@2
    79
                          sin(result_type(2)*pi*_r1))
williamr@2
    80
      * _sigma + _mean;
williamr@2
    81
  }
williamr@2
    82
williamr@2
    83
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
williamr@2
    84
  template<class CharT, class Traits>
williamr@2
    85
  friend std::basic_ostream<CharT,Traits>&
williamr@2
    86
  operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
williamr@2
    87
  {
williamr@2
    88
    os << nd._mean << " " << nd._sigma << " "
williamr@2
    89
       << nd._valid << " " << nd._cached_rho << " " << nd._r1;
williamr@2
    90
    return os;
williamr@2
    91
  }
williamr@2
    92
williamr@2
    93
  template<class CharT, class Traits>
williamr@2
    94
  friend std::basic_istream<CharT,Traits>&
williamr@2
    95
  operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
williamr@2
    96
  {
williamr@2
    97
    is >> std::ws >> nd._mean >> std::ws >> nd._sigma
williamr@2
    98
       >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
williamr@2
    99
       >> std::ws >> nd._r1;
williamr@2
   100
    return is;
williamr@2
   101
  }
williamr@2
   102
#endif
williamr@2
   103
private:
williamr@2
   104
  result_type _mean, _sigma;
williamr@2
   105
  result_type _r1, _r2, _cached_rho;
williamr@2
   106
  bool _valid;
williamr@2
   107
};
williamr@2
   108
williamr@2
   109
} // namespace boost
williamr@2
   110
williamr@2
   111
#endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP