1 /* boost random/lognormal_distribution.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: lognormal_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
\r
17 #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
\r
19 #include <boost/config/no_tr1/cmath.hpp> // std::exp, std::sqrt
\r
22 #include <boost/limits.hpp>
\r
23 #include <boost/static_assert.hpp>
\r
24 #include <boost/random/detail/config.hpp>
\r
25 #include <boost/random/normal_distribution.hpp>
\r
27 #ifdef BOOST_NO_STDC_NAMESPACE
\r
36 #if defined(__GNUC__) && (__GNUC__ < 3)
\r
37 // Special gcc workaround: gcc 2.95.x ignores using-declarations
\r
38 // in template classes (confirmed by gcc author Martin v. Loewis)
\r
43 template<class RealType = double>
\r
44 class lognormal_distribution
\r
47 typedef typename normal_distribution<RealType>::input_type input_type;
\r
48 typedef RealType result_type;
\r
50 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
51 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
\r
54 explicit lognormal_distribution(result_type mean_arg = result_type(1),
\r
55 result_type sigma_arg = result_type(1))
\r
56 : _mean(mean_arg), _sigma(sigma_arg)
\r
58 assert(_mean > result_type(0));
\r
62 // compiler-generated copy ctor and assignment operator are fine
\r
64 RealType mean() const { return _mean; }
\r
65 RealType sigma() const { return _sigma; }
\r
66 void reset() { _normal.reset(); }
\r
68 template<class Engine>
\r
69 result_type operator()(Engine& eng)
\r
71 #ifndef BOOST_NO_STDC_NAMESPACE
\r
72 // allow for Koenig lookup
\r
75 return exp(_normal(eng) * _nsigma + _nmean);
\r
78 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
79 template<class CharT, class Traits>
\r
80 friend std::basic_ostream<CharT,Traits>&
\r
81 operator<<(std::basic_ostream<CharT,Traits>& os, const lognormal_distribution& ld)
\r
83 os << ld._normal << " " << ld._mean << " " << ld._sigma;
\r
87 template<class CharT, class Traits>
\r
88 friend std::basic_istream<CharT,Traits>&
\r
89 operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
\r
91 is >> std::ws >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
\r
100 #ifndef BOOST_NO_STDC_NAMESPACE
\r
101 // allow for Koenig lookup
\r
102 using std::exp; using std::log; using std::sqrt;
\r
104 _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
\r
105 _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
\r
108 RealType _mean, _sigma;
\r
109 RealType _nmean, _nsigma;
\r
110 normal_distribution<result_type> _normal;
\r
113 } // namespace boost
\r
115 #endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
\r