Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:10

0001 /* boost random/cauchy_distribution.hpp header file
0002  *
0003  * Copyright Jens Maurer 2000-2001
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  *
0012  * Revision history
0013  *  2001-02-18  moved to individual header files
0014  */
0015 
0016 #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
0017 #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
0018 
0019 #include <boost/config/no_tr1/cmath.hpp>
0020 #include <iosfwd>
0021 #include <istream>
0022 #include <boost/limits.hpp>
0023 #include <boost/random/detail/config.hpp>
0024 #include <boost/random/detail/operators.hpp>
0025 #include <boost/random/uniform_01.hpp>
0026 
0027 namespace boost {
0028 namespace random {
0029 
0030 // Cauchy distribution: 
0031 
0032 /**
0033  * The cauchy distribution is a continuous distribution with two
0034  * parameters, median and sigma.
0035  *
0036  * It has \f$\displaystyle p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$
0037  */
0038 template<class RealType = double>
0039 class cauchy_distribution
0040 {
0041 public:
0042     typedef RealType input_type;
0043     typedef RealType result_type;
0044 
0045     class param_type
0046     {
0047     public:
0048 
0049         typedef cauchy_distribution distribution_type;
0050 
0051         /** Constructs the parameters of the cauchy distribution. */
0052         explicit param_type(RealType median_arg = RealType(0.0),
0053                             RealType sigma_arg = RealType(1.0))
0054           : _median(median_arg), _sigma(sigma_arg) {}
0055 
0056         // backwards compatibility for Boost.Random
0057 
0058         /** Returns the median of the distribution. */
0059         RealType median() const { return _median; }
0060         /** Returns the sigma parameter of the distribution. */
0061         RealType sigma() const { return _sigma; }
0062 
0063         // The new names in C++0x.
0064 
0065         /** Returns the median of the distribution. */
0066         RealType a() const { return _median; }
0067         /** Returns the sigma parameter of the distribution. */
0068         RealType b() const { return _sigma; }
0069 
0070         /** Writes the parameters to a std::ostream. */
0071         BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0072         {
0073             os << parm._median << " " << parm._sigma;
0074             return os;
0075         }
0076 
0077         /** Reads the parameters from a std::istream. */
0078         BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0079         {
0080             is >> parm._median >> std::ws >> parm._sigma;
0081             return is;
0082         }
0083 
0084         /** Returns true if the two sets of parameters are equal. */
0085         BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0086         { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
0087 
0088         /** Returns true if the two sets of parameters are different. */
0089         BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0090 
0091     private:
0092         RealType _median;
0093         RealType _sigma;
0094     };
0095 
0096     /**
0097      * Constructs a \cauchy_distribution with the paramters @c median
0098      * and @c sigma.
0099      */
0100     explicit cauchy_distribution(RealType median_arg = RealType(0.0), 
0101                                  RealType sigma_arg = RealType(1.0))
0102       : _median(median_arg), _sigma(sigma_arg) { }
0103     
0104     /**
0105      * Constructs a \cauchy_distribution from it's parameters.
0106      */
0107     explicit cauchy_distribution(const param_type& parm)
0108       : _median(parm.median()), _sigma(parm.sigma()) { }
0109 
0110     // compiler-generated copy ctor and assignment operator are fine
0111 
0112     // backwards compatibility for Boost.Random
0113 
0114     /** Returns: the "median" parameter of the distribution */
0115     RealType median() const { return _median; }
0116     /** Returns: the "sigma" parameter of the distribution */
0117     RealType sigma() const { return _sigma; }
0118     
0119     // The new names in C++0x
0120 
0121     /** Returns: the "median" parameter of the distribution */
0122     RealType a() const { return _median; }
0123     /** Returns: the "sigma" parameter of the distribution */
0124     RealType b() const { return _sigma; }
0125 
0126     /** Returns the smallest value that the distribution can produce. */
0127     RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0128     { return -(std::numeric_limits<RealType>::infinity)(); }
0129 
0130     /** Returns the largest value that the distribution can produce. */
0131     RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0132     { return (std::numeric_limits<RealType>::infinity)(); }
0133 
0134     param_type param() const { return param_type(_median, _sigma); }
0135 
0136     void param(const param_type& parm)
0137     {
0138         _median = parm.median();
0139         _sigma = parm.sigma();
0140     }
0141 
0142     /**
0143      * Effects: Subsequent uses of the distribution do not depend
0144      * on values produced by any engine prior to invoking reset.
0145      */
0146     void reset() { }
0147 
0148     /**
0149      * Returns: A random variate distributed according to the
0150      * cauchy distribution.
0151      */
0152     template<class Engine>
0153     result_type operator()(Engine& eng)
0154     {
0155         // Can we have a boost::mathconst please?
0156         const result_type pi = result_type(3.14159265358979323846);
0157         using std::tan;
0158         RealType val = uniform_01<RealType>()(eng)-result_type(0.5);
0159         return _median + _sigma * tan(pi*val);
0160     }
0161 
0162     /**
0163      * Returns: A random variate distributed according to the
0164      * cauchy distribution with parameters specified by param.
0165      */
0166     template<class Engine>
0167     result_type operator()(Engine& eng, const param_type& parm)
0168     {
0169         return cauchy_distribution(parm)(eng);
0170     }
0171 
0172     /**
0173      * Writes the distribution to a @c std::ostream.
0174      */
0175     BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, cauchy_distribution, cd)
0176     {
0177         os << cd._median << " " << cd._sigma;
0178         return os;
0179     }
0180 
0181     /**
0182      * Reads the distribution from a @c std::istream.
0183      */
0184     BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, cauchy_distribution, cd)
0185     {
0186         is >> cd._median >> std::ws >> cd._sigma;
0187         return is;
0188     }
0189 
0190     /**
0191      * Returns true if the two distributions will produce
0192      * identical sequences of values, given equal generators.
0193      */
0194     BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(cauchy_distribution, lhs, rhs)
0195     { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
0196 
0197     /**
0198      * Returns true if the two distributions may produce
0199      * different sequences of values, given equal generators.
0200      */
0201     BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(cauchy_distribution)
0202 
0203 private:
0204     RealType _median;
0205     RealType _sigma;
0206 };
0207 
0208 } // namespace random
0209 
0210 using random::cauchy_distribution;
0211 
0212 } // namespace boost
0213 
0214 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP