1 /* boost random/cauchy_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: cauchy_distribution.hpp,v 1.1.1.1 2006/03/20 20:15:10 ewalkup Exp $
11  *
12  * Revision history
13  *  2001-02-18  moved to individual header files
14  */
15 
16 #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
18 
19 #include <cmath>
20 #include <iostream>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 
24 namespace boost {
25 
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)
29   using std::tan;
30 #endif
31 
32 // Cauchy distribution: p(x) = sigma/(pi*(sigma**2 + (x-median)**2))
33 template<class RealType = double>
34 class cauchy_distribution
35 {
36 public:
37   typedef RealType input_type;
38   typedef RealType result_type;
39 
40 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
41   BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
42 #endif
43 
cauchy_distribution(result_type median=result_type (0),result_type sigma=result_type (1))44   explicit cauchy_distribution(result_type median = result_type(0),
45                                result_type sigma = result_type(1))
46     : _median(median), _sigma(sigma) { }
47 
48   // compiler-generated copy ctor and assignment operator are fine
49 
median() const50   result_type median() const { return _median; }
sigma() const51   result_type sigma() const { return _sigma; }
reset()52   void reset() { }
53 
54   template<class Engine>
operator ()(Engine & eng)55   result_type operator()(Engine& eng)
56   {
57     // Can we have a boost::mathconst please?
58     const result_type pi = result_type(3.14159265358979323846);
59 #ifndef BOOST_NO_STDC_NAMESPACE
60     using std::tan;
61 #endif
62     return _median + _sigma * tan(pi*(eng()-result_type(0.5)));
63   }
64 
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>&
operator <<(std::basic_ostream<CharT,Traits> & os,const cauchy_distribution & cd)68   operator<<(std::basic_ostream<CharT,Traits>& os, const cauchy_distribution& cd)
69   {
70     os << cd._median << " " << cd._sigma;
71     return os;
72   }
73 
74   template<class CharT, class Traits>
75   friend std::basic_istream<CharT,Traits>&
operator >>(std::basic_istream<CharT,Traits> & is,cauchy_distribution & cd)76   operator>>(std::basic_istream<CharT,Traits>& is, cauchy_distribution& cd)
77   {
78     is >> std::ws >> cd._median >> std::ws >> cd._sigma;
79     return is;
80   }
81 #endif
82 
83 private:
84   result_type _median, _sigma;
85 };
86 
87 } // namespace boost
88 
89 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
90