1 /* boost random/lognormal_distribution.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: lognormal_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
19 #include <cmath> // std::exp, std::sqrt
22 #include <boost/limits.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/random/normal_distribution.hpp>
26 #ifdef BOOST_NO_STDC_NAMESPACE
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)
42 template<class RealType = double>
43 class lognormal_distribution
46 typedef typename normal_distribution<RealType>::input_type input_type;
47 typedef RealType result_type;
49 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
50 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
53 explicit lognormal_distribution(result_type mean = result_type(1),
54 result_type sigma = result_type(1))
55 : _mean(mean), _sigma(sigma)
57 assert(mean > result_type(0));
61 // compiler-generated copy ctor and assignment operator are fine
63 RealType& mean() const { return _mean; }
64 RealType& sigma() const { return _sigma; }
65 void reset() { _normal.reset(); }
67 template<class Engine>
68 result_type operator()(Engine& eng)
70 #ifndef BOOST_NO_STDC_NAMESPACE
71 // allow for Koenig lookup
74 return exp(_normal(eng) * _nsigma + _nmean);
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)
82 os << ld._normal << " " << ld._mean << " " << ld._sigma;
86 template<class CharT, class Traits>
87 friend std::basic_istream<CharT,Traits>&
88 operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
90 is >> std::ws >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
99 #ifndef BOOST_NO_STDC_NAMESPACE
100 // allow for Koenig lookup
101 using std::exp; using std::log; using std::sqrt;
103 _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
104 _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
107 RealType _mean, _sigma;
108 RealType _nmean, _nsigma;
109 normal_distribution<result_type> _normal;
114 #endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP