Uniform.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a uniform distribution.
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2010-01-01
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_RNG_UNIFORM_H
33 #define SHARK_RNG_UNIFORM_H
34 
36 #include "shark/Rng/Rng.h"
37 
38 #include <boost/random.hpp>
39 
40 namespace shark{
41 
42 /**
43 * \brief Implements a continuous uniform distribution.
44 */
45 template<typename RngType = DefaultRngType>
46 class Uniform
47 :
48  public AbstractDistribution,
49  public boost::variate_generator<RngType*,boost::uniform_real<> >
50 {
51 private:
52 
53  typedef boost::variate_generator<RngType*,boost::uniform_real<> > Base;
54 
55 public:
56 
57  /**
58  * \brief Default c'tor. Initializes the sampling interval and associates
59  * this distribution with the supplied RNG.
60  * \param [in,out] rng The RNG to associate this distribution with.
61  * \param [in] low_ The lower bound of the sampling interval.
62  * \param [in] high_ The upper bound of the sampling interval.
63  */
64  Uniform( RngType & rng, double low_ = 0., double high_ = 1. )
65  :Base(&rng,boost::uniform_real<>(std::min(low_,high_),std::max(low_,high_)))
66  {}
67 
68  /**
69  * \brief Injects the default sampling operator.
70  */
71  using Base::operator();
72 
73  /**
74  * \brief Resets the distribution to the supplied interval and samples a random number.
75  * \param [in] low_ The lower bound of the interval.
76  * \param [in] high_ The upper bound of the interval.
77  */
78  double operator()(double low_,double high_)
79  {
80  if(low_ == high_) return low_;
81  boost::uniform_real<> dist( std::min(low_,high_), std::max( high_, low_ ) );
82  return dist(Base::engine());
83  }
84 
85  /**
86  * \brief Accesses the lower bound of the interval.
87  */
88  double low()const
89  {
90  return Base::distribution().min();
91  }
92 
93  /**
94  * \brief Accesses the upper bound of the interval.
95  */
96  double high()const
97  {
98  return Base::distribution().max();
99  }
100 
101  /**
102  * \brief Adjusts the interval of the distribution.
103  * \param [in] low_ The lower bound of the interval.
104  * \param [in] high_ The upper bound of the interval.
105  */
106  void setRange(double low_,double high_)
107  {
108  boost::uniform_real<> dist(std::min(low_,high_),std::max(low_,high_));
109  Base::distribution()=dist;
110  }
111 
112  /**
113  * \brief Calculates the probability of x.
114  * \param [in] x The observation.
115  */
116  double p(double x) const {
117  return x >= low() && x < high() ? 1 / (high() - low()) : 0;
118  }
119 };
120 
121 ///\brief Draws a number uniformly in [lower,upper] by drawing random numbers from rng.
122 template<class RngType>
123 double uni(RngType& rng, double lower, double upper){
124  Uniform<RngType> dist(rng, lower, upper);
125  return dist();
126 }
127 
128 } // namespace shark {
129 
130 #endif // SHARK_RNG_UNIFORM_H