]> git.donarmstrong.com Git - rsem.git/blob - boost/random/cauchy_distribution.hpp
c11ba2bf7773f523a23e5d46a39b68fd6fa3b681
[rsem.git] / boost / random / cauchy_distribution.hpp
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 60755 2010-03-22 00:45:06Z steven_watanabe $
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 <boost/config/no_tr1/cmath.hpp>
20 #include <iostream>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/config.hpp>
24
25 namespace boost {
26
27 #if defined(__GNUC__) && (__GNUC__ < 3)
28 // Special gcc workaround: gcc 2.95.x ignores using-declarations
29 // in template classes (confirmed by gcc author Martin v. Loewis)
30   using std::tan;
31 #endif
32
33 // Cauchy distribution: 
34
35 /**
36  * The cauchy distribution is a continuous distribution with two
37  * parameters, sigma and median.
38  *
39  * It has \f$p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$
40  */
41 template<class RealType = double>
42 class cauchy_distribution
43 {
44 public:
45   typedef RealType input_type;
46   typedef RealType result_type;
47
48 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
49   BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
50 #endif
51
52   /**
53    * Constructs a \cauchy_distribution with the paramters @c median
54    * and @c sigma.
55    */
56   explicit cauchy_distribution(result_type median_arg = result_type(0), 
57                                result_type sigma_arg = result_type(1))
58     : _median(median_arg), _sigma(sigma_arg) { }
59
60   // compiler-generated copy ctor and assignment operator are fine
61
62   /**
63    * Returns: the "median" parameter of the distribution
64    */
65   result_type median() const { return _median; }
66   /**
67    * Returns: the "sigma" parameter of the distribution
68    */
69   result_type sigma() const { return _sigma; }
70   /**
71    * Effects: Subsequent uses of the distribution do not depend
72    * on values produced by any engine prior to invoking reset.
73    */
74   void reset() { }
75
76   /**
77    * Returns: A random variate distributed according to the
78    * cauchy distribution.
79    */
80   template<class Engine>
81   result_type operator()(Engine& eng)
82   {
83     // Can we have a boost::mathconst please?
84     const result_type pi = result_type(3.14159265358979323846);
85 #ifndef BOOST_NO_STDC_NAMESPACE
86     using std::tan;
87 #endif
88     return _median + _sigma * tan(pi*(eng()-result_type(0.5)));
89   }
90
91 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
92   /**
93    * Writes the parameters of the distribution to a @c std::ostream.
94    */
95   template<class CharT, class Traits>
96   friend std::basic_ostream<CharT,Traits>&
97   operator<<(std::basic_ostream<CharT,Traits>& os, const cauchy_distribution& cd)
98   {
99     os << cd._median << " " << cd._sigma;
100     return os;
101   }
102
103   /**
104    * Reads the parameters of the distribution from a @c std::istream.
105    */
106   template<class CharT, class Traits>
107   friend std::basic_istream<CharT,Traits>&
108   operator>>(std::basic_istream<CharT,Traits>& is, cauchy_distribution& cd)
109   {
110     is >> std::ws >> cd._median >> std::ws >> cd._sigma;
111     return is;
112   }
113 #endif
114
115 private:
116   result_type _median, _sigma;
117 };
118
119 } // namespace boost
120
121 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP