os/ossrv/ossrv_pub/boost_apis/boost/random/triangle_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/triangle_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: triangle_distribution.hpp,v 1.11 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_TRIANGLE_DISTRIBUTION_HPP
sl@0
    17
#define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
sl@0
    18
sl@0
    19
#include <cmath>
sl@0
    20
#include <cassert>
sl@0
    21
#include <boost/random/uniform_01.hpp>
sl@0
    22
sl@0
    23
namespace boost {
sl@0
    24
sl@0
    25
// triangle distribution, with a smallest, b most probable, and c largest
sl@0
    26
// value.
sl@0
    27
template<class RealType = double>
sl@0
    28
class triangle_distribution
sl@0
    29
{
sl@0
    30
public:
sl@0
    31
  typedef RealType input_type;
sl@0
    32
  typedef RealType result_type;
sl@0
    33
sl@0
    34
  explicit triangle_distribution(result_type a = result_type(0),
sl@0
    35
                                 result_type b = result_type(0.5),
sl@0
    36
                                 result_type c = result_type(1))
sl@0
    37
    : _a(a), _b(b), _c(c)
sl@0
    38
  {
sl@0
    39
    assert(_a <= _b && _b <= _c);
sl@0
    40
    init();
sl@0
    41
  }
sl@0
    42
sl@0
    43
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    44
  result_type a() const { return _a; }
sl@0
    45
  result_type b() const { return _b; }
sl@0
    46
  result_type c() const { return _c; }
sl@0
    47
sl@0
    48
  void reset() { }
sl@0
    49
sl@0
    50
  template<class Engine>
sl@0
    51
  result_type operator()(Engine& eng)
sl@0
    52
  {
sl@0
    53
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    54
    using std::sqrt;
sl@0
    55
#endif
sl@0
    56
    result_type u = eng();
sl@0
    57
    if( u <= q1 )
sl@0
    58
      return _a + p1*sqrt(u);
sl@0
    59
    else
sl@0
    60
      return _c - d3*sqrt(d2*u-d1);
sl@0
    61
  }
sl@0
    62
sl@0
    63
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    64
  template<class CharT, class Traits>
sl@0
    65
  friend std::basic_ostream<CharT,Traits>&
sl@0
    66
  operator<<(std::basic_ostream<CharT,Traits>& os, const triangle_distribution& td)
sl@0
    67
  {
sl@0
    68
    os << td._a << " " << td._b << " " << td._c;
sl@0
    69
    return os;
sl@0
    70
  }
sl@0
    71
sl@0
    72
  template<class CharT, class Traits>
sl@0
    73
  friend std::basic_istream<CharT,Traits>&
sl@0
    74
  operator>>(std::basic_istream<CharT,Traits>& is, triangle_distribution& td)
sl@0
    75
  {
sl@0
    76
    is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
sl@0
    77
    td.init();
sl@0
    78
    return is;
sl@0
    79
  }
sl@0
    80
#endif
sl@0
    81
sl@0
    82
private:
sl@0
    83
  void init()
sl@0
    84
  {
sl@0
    85
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    86
    using std::sqrt;
sl@0
    87
#endif
sl@0
    88
    d1 = _b - _a;
sl@0
    89
    d2 = _c - _a;
sl@0
    90
    d3 = sqrt(_c - _b);
sl@0
    91
    q1 = d1 / d2;
sl@0
    92
    p1 = sqrt(d1 * d2);
sl@0
    93
  }
sl@0
    94
sl@0
    95
  result_type _a, _b, _c;
sl@0
    96
  result_type d1, d2, d3, q1, p1;
sl@0
    97
};
sl@0
    98
sl@0
    99
} // namespace boost
sl@0
   100
sl@0
   101
#endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP