Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/random/lognormal_distribution.hpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.1 KB
Line 
1/* boost random/lognormal_distribution.hpp header file
2 *
3 * Copyright Jens Maurer 2000-2001
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id: lognormal_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
11 *
12 * Revision history
13 *  2001-02-18  moved to individual header files
14 */
15
16#ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
17#define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
18
19#include <cmath>      // std::exp, std::sqrt
20#include <cassert>
21#include <iostream>
22#include <boost/limits.hpp>
23#include <boost/static_assert.hpp>
24#include <boost/random/normal_distribution.hpp>
25
26#ifdef BOOST_NO_STDC_NAMESPACE
27namespace std {
28  using ::log;
29  using ::sqrt;
30}
31#endif
32
33namespace boost {
34
35#if defined(__GNUC__) && (__GNUC__ < 3)
36// Special gcc workaround: gcc 2.95.x ignores using-declarations
37// in template classes (confirmed by gcc author Martin v. Loewis)
38  using std::sqrt;
39  using std::exp;
40#endif
41
42template<class RealType = double>
43class lognormal_distribution
44{
45public:
46  typedef typename normal_distribution<RealType>::input_type input_type;
47  typedef RealType result_type;
48
49#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
50    BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
51#endif
52
53  explicit lognormal_distribution(result_type mean = result_type(1),
54                                  result_type sigma = result_type(1))
55    : _mean(mean), _sigma(sigma)
56  { 
57    assert(mean > result_type(0));
58    init();
59  }
60
61  // compiler-generated copy ctor and assignment operator are fine
62
63  RealType& mean() const { return _mean; }
64  RealType& sigma() const { return _sigma; }
65  void reset() { _normal.reset(); }
66
67  template<class Engine>
68  result_type operator()(Engine& eng)
69  {
70#ifndef BOOST_NO_STDC_NAMESPACE
71    // allow for Koenig lookup
72    using std::exp;
73#endif
74    return exp(_normal(eng) * _nsigma + _nmean);
75  }
76
77#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
78  template<class CharT, class Traits>
79  friend std::basic_ostream<CharT,Traits>&
80  operator<<(std::basic_ostream<CharT,Traits>& os, const lognormal_distribution& ld)
81  {
82    os << ld._normal << " " << ld._mean << " " << ld._sigma;
83    return os;
84  }
85
86  template<class CharT, class Traits>
87  friend std::basic_istream<CharT,Traits>&
88  operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
89  {
90    is >> std::ws >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
91    ld.init();
92    return is;
93  }
94#endif
95
96private:
97  void init()
98  {
99#ifndef BOOST_NO_STDC_NAMESPACE
100    // allow for Koenig lookup
101    using std::exp; using std::log; using std::sqrt;
102#endif
103    _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
104    _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
105  }
106
107  RealType _mean, _sigma;
108  RealType _nmean, _nsigma;
109  normal_distribution<result_type> _normal;
110};
111
112} // namespace boost
113
114#endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
Note: See TracBrowser for help on using the repository browser.