1 /* boost random/cauchy_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: cauchy_distribution.hpp,v 1.15 2004/07/27 03:43:32 dgregor Exp $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
26 #if defined(__GNUC__) && (__GNUC__ < 3)
27 // Special gcc workaround: gcc 2.95.x ignores using-declarations
28 // in template classes (confirmed by gcc author Martin v. Loewis)
32 // Cauchy distribution: p(x) = sigma/(pi*(sigma**2 + (x-median)**2))
33 template<class RealType = double>
34 class cauchy_distribution
37 typedef RealType input_type;
38 typedef RealType result_type;
40 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
41 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
44 explicit cauchy_distribution(result_type median = result_type(0),
45 result_type sigma = result_type(1))
46 : _median(median), _sigma(sigma) { }
48 // compiler-generated copy ctor and assignment operator are fine
50 result_type median() const { return _median; }
51 result_type sigma() const { return _sigma; }
54 template<class Engine>
55 result_type operator()(Engine& eng)
57 // Can we have a boost::mathconst please?
58 const result_type pi = result_type(3.14159265358979323846);
59 #ifndef BOOST_NO_STDC_NAMESPACE
62 return _median + _sigma * tan(pi*(eng()-result_type(0.5)));
65 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
66 template<class CharT, class Traits>
67 friend std::basic_ostream<CharT,Traits>&
68 operator<<(std::basic_ostream<CharT,Traits>& os, const cauchy_distribution& cd)
70 os << cd._median << " " << cd._sigma;
74 template<class CharT, class Traits>
75 friend std::basic_istream<CharT,Traits>&
76 operator>>(std::basic_istream<CharT,Traits>& is, cauchy_distribution& cd)
78 is >> std::ws >> cd._median >> std::ws >> cd._sigma;
84 result_type _median, _sigma;
89 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP