~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to boost/boost/random/lognormal_distribution.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* boost random/lognormal_distribution.hpp header file
 
2
 *
 
3
 * Copyright Jens Maurer 2000-2001
 
4
 * Permission to use, copy, modify, sell, and distribute this software
 
5
 * is hereby granted without fee provided that the above copyright notice
 
6
 * appears in all copies and that both that copyright notice and this
 
7
 * permission notice appear in supporting documentation,
 
8
 *
 
9
 * Jens Maurer makes no representations about the suitability of this
 
10
 * software for any purpose. It is provided "as is" without express or
 
11
 * implied warranty.
 
12
 *
 
13
 * See http://www.boost.org for most recent version including documentation.
 
14
 *
 
15
 * $Id: lognormal_distribution.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
 
16
 *
 
17
 * Revision history
 
18
 *  2001-02-18  moved to individual header files
 
19
 */
 
20
 
 
21
#ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
 
22
#define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
 
23
 
 
24
#include <cmath>      // std::exp, std::sqrt
 
25
#include <cassert>
 
26
#include <boost/random/normal_distribution.hpp>
 
27
 
 
28
#ifdef BOOST_NO_STDC_NAMESPACE
 
29
namespace std {
 
30
  using ::log;
 
31
  using ::sqrt;
 
32
}
 
33
#endif
 
34
 
 
35
namespace boost {
 
36
 
 
37
#if defined(__GNUC__) && (__GNUC__ < 3)
 
38
// Special gcc workaround: gcc 2.95.x ignores using-declarations
 
39
// in template classes (confirmed by gcc author Martin v. Loewis)
 
40
  using std::sqrt;
 
41
  using std::exp;
 
42
#endif
 
43
 
 
44
template<class UniformRandomNumberGenerator, class RealType = double,
 
45
        class Adaptor = uniform_01<UniformRandomNumberGenerator, RealType> >
 
46
class lognormal_distribution
 
47
{
 
48
public:
 
49
  typedef Adaptor adaptor_type;
 
50
  typedef UniformRandomNumberGenerator base_type;
 
51
  typedef RealType result_type;
 
52
 
 
53
  explicit lognormal_distribution(base_type & rng,
 
54
                                  result_type mean = result_type(1),
 
55
                                  result_type sigma = result_type(1))
 
56
    : _mean(mean), _sigma(sigma),
 
57
      _rng(rng, std::log(mean*mean/std::sqrt(sigma*sigma + mean*mean)),
 
58
           std::sqrt(std::log(sigma*sigma/mean/mean+result_type(1))))
 
59
  { 
 
60
    assert(mean > result_type(0));
 
61
  }
 
62
 
 
63
  // compiler-generated copy ctor and assignment operator are fine
 
64
 
 
65
  adaptor_type& adaptor() { return _rng.adaptor(); }
 
66
  base_type& base() const { return _rng.base(); }
 
67
  RealType mean() const { return _mean; }
 
68
  RealType sigma() const { return _sigma; }
 
69
  void reset() { _rng.reset(); }
 
70
 
 
71
  result_type operator()()
 
72
  {
 
73
#ifndef BOOST_NO_STDC_NAMESPACE
 
74
    // allow for Koenig lookup
 
75
    using std::exp;
 
76
#endif
 
77
    return exp(_rng());
 
78
  }
 
79
 
 
80
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
 
81
  friend bool operator==(const lognormal_distribution& x, 
 
82
                         const lognormal_distribution& y)
 
83
  { return x._rng == y._rng; }
 
84
 
 
85
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
 
86
  template<class CharT, class Traits>
 
87
  friend std::basic_ostream<CharT,Traits>&
 
88
  operator<<(std::basic_ostream<CharT,Traits>& os, const lognormal_distribution& ld)
 
89
  {
 
90
    os << ld._rng << " " << ld._mean << " " << ld._sigma;
 
91
    return os;
 
92
  }
 
93
 
 
94
  template<class CharT, class Traits>
 
95
  friend std::basic_istream<CharT,Traits>&
 
96
  operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
 
97
  {
 
98
    is >> std::ws >> ld._rng >> std::ws >> ld._mean >> std::ws >> ld._sigma;
 
99
    return is;
 
100
  }
 
101
#endif
 
102
 
 
103
#else
 
104
  // Use a member function
 
105
  bool operator==(const lognormal_distribution& rhs) const
 
106
  { return _rng == rhs._rng;  }
 
107
#endif
 
108
private:
 
109
  RealType _mean, _sigma;
 
110
  normal_distribution<base_type, result_type, adaptor_type> _rng;
 
111
};
 
112
 
 
113
} // namespace boost
 
114
 
 
115
#endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP