os/ossrv/ossrv_pub/boost_apis/boost/random/cauchy_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/cauchy_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: cauchy_distribution.hpp,v 1.15 2004/07/27 03:43:32 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
sl@0
    17
#define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
sl@0
    18
sl@0
    19
#include <cmath>
sl@0
    20
#include <iostream>
sl@0
    21
#include <boost/limits.hpp>
sl@0
    22
#include <boost/static_assert.hpp>
sl@0
    23
sl@0
    24
namespace boost {
sl@0
    25
sl@0
    26
#if defined(__GNUC__) && (__GNUC__ < 3)
sl@0
    27
// Special gcc workaround: gcc 2.95.x ignores using-declarations
sl@0
    28
// in template classes (confirmed by gcc author Martin v. Loewis)
sl@0
    29
  using std::tan;
sl@0
    30
#endif
sl@0
    31
sl@0
    32
// Cauchy distribution: p(x) = sigma/(pi*(sigma**2 + (x-median)**2))
sl@0
    33
template<class RealType = double>
sl@0
    34
class cauchy_distribution
sl@0
    35
{
sl@0
    36
public:
sl@0
    37
  typedef RealType input_type;
sl@0
    38
  typedef RealType result_type;
sl@0
    39
sl@0
    40
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    41
  BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
sl@0
    42
#endif
sl@0
    43
sl@0
    44
  explicit cauchy_distribution(result_type median = result_type(0), 
sl@0
    45
                               result_type sigma = result_type(1))
sl@0
    46
    : _median(median), _sigma(sigma) { }
sl@0
    47
sl@0
    48
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    49
sl@0
    50
  result_type median() const { return _median; }
sl@0
    51
  result_type sigma() const { return _sigma; }
sl@0
    52
  void reset() { }
sl@0
    53
sl@0
    54
  template<class Engine>
sl@0
    55
  result_type operator()(Engine& eng)
sl@0
    56
  {
sl@0
    57
    // Can we have a boost::mathconst please?
sl@0
    58
    const result_type pi = result_type(3.14159265358979323846);
sl@0
    59
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    60
    using std::tan;
sl@0
    61
#endif
sl@0
    62
    return _median + _sigma * tan(pi*(eng()-result_type(0.5)));
sl@0
    63
  }
sl@0
    64
sl@0
    65
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    66
  template<class CharT, class Traits>
sl@0
    67
  friend std::basic_ostream<CharT,Traits>&
sl@0
    68
  operator<<(std::basic_ostream<CharT,Traits>& os, const cauchy_distribution& cd)
sl@0
    69
  {
sl@0
    70
    os << cd._median << " " << cd._sigma;
sl@0
    71
    return os;
sl@0
    72
  }
sl@0
    73
sl@0
    74
  template<class CharT, class Traits>
sl@0
    75
  friend std::basic_istream<CharT,Traits>&
sl@0
    76
  operator>>(std::basic_istream<CharT,Traits>& is, cauchy_distribution& cd)
sl@0
    77
  {
sl@0
    78
    is >> std::ws >> cd._median >> std::ws >> cd._sigma;
sl@0
    79
    return is;
sl@0
    80
  }
sl@0
    81
#endif
sl@0
    82
sl@0
    83
private:
sl@0
    84
  result_type _median, _sigma;
sl@0
    85
};
sl@0
    86
sl@0
    87
} // namespace boost
sl@0
    88
sl@0
    89
#endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP