MIRA
NormalRandomGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_NORMALRANDOMGENERATOR_H_
48 #define _MIRA_NORMALRANDOMGENERATOR_H_
49 
50 #include <math/Math.h>
51 #include <math/Eigen.h>
52 
53 #include <utils/PParam.h>
54 #include <math/RandomGenerator.h>
55 #ifndef Q_MOC_RUN
56 #include <boost/random/uniform_01.hpp>
57 #endif
58 
59 namespace mira {
60 
62 
64 namespace detail {
65 template <typename T>
66 class NormalRandomDistributionBase
67 {
68 public:
69 
70  NormalRandomDistributionBase() : first(true) {}
71 
77  template <typename Engine>
78  T normal(Engine& eng)
79  {
80  if(first) {
81  float u;
82  u = boost::uniform_01<T>()(eng);
83  v = boost::uniform_01<T>()(eng);
84  sqrt_2ln_u = std::sqrt(-T(2) * std::log(T(1)-u));
85  // using log(1-u) above instead of log(u), since u is element of [0,1)
86  // i.e. u may become 0, where log(0) = -inf. When using log(1-u)
87  // we can avoid the -inf.
88  }
89  first = !first;
90  return sqrt_2ln_u * (first ? std::sin(T(2)*pi<T>()*v) :
91  std::cos(T(2)*pi<T>()*v));
92  }
93 
94 private:
95  bool first;
96  T sqrt_2ln_u,v;
97 };
98 }
100 
102 
124 template <int D, typename T=float>
125 class NormalRandomDistribution : private detail::NormalRandomDistributionBase<T>
126 {
127 public:
128  typedef T input_type;
130  typedef detail::NormalRandomDistributionBase<T> Base;
131 
133 
134 public:
135 
137  mL = VarianceType::Identity();
138  }
139 
141  setSigma(sigma);
142  }
143 
155  bool setSigma(const Eigen::Matrix<T,D,D>& sigma) {
156  // Cholesky decomposition: sigma = L*L'
157  auto llt = sigma.llt();
158  if(llt.info()!=Eigen::Success)
159  return false;
160  mL = llt.matrixL();
161  return true;
162  }
163 
167  template <typename Engine>
169  result_type n;
170  for(int i=0;i<D;++i)
171  n(i) = this->normal(eng);
172  return mL*n;
173  }
174 
175 private:
177 };
178 
179 // specialization for univariate distribution (D=1)
180 template <typename T>
181 class NormalRandomDistribution<1,T> : private detail::NormalRandomDistributionBase<T>
182 {
183 public:
184  typedef T input_type;
185  typedef T result_type;
186 
187  typedef T VarianceType;
188 
189 public:
190 
191  NormalRandomDistribution() : mSigma(T(1)) {}
192 
193  NormalRandomDistribution(T sigma) : mSigma(sigma) {}
194 
198  void setSigma(T sigma) {
199  mSigma = sigma;
200  }
201 
202 
203  template <typename Engine>
204  T operator()(Engine& eng) {
205  return mSigma * this->normal(eng);
206  }
207 
208 private:
209  T mSigma;
210 };
211 
213 
238 template <int D, typename T=float>
239 class NormalRandomGenerator : public RandomGenerator<NormalRandomDistribution<D,T>>
240 {
241  // if new C++11 typedefs are supported this could become a templated typedef.
242 public:
245 };
246 
247 
248 
249 
251 
252 } // namespace
253 
254 #endif
T VarianceType
Definition: NormalRandomGenerator.h:187
Eigen::Matrix< T, D, D > VarianceType
Definition: NormalRandomGenerator.h:132
#define MIRA_PPARAM(...)
Preprocessor workaround to handle single parameters that contain a comma.
Definition: PParam.h:61
Include file for all eigen related things.
Includes often needed math headers and methods and provides additional constants. ...
T input_type
Definition: NormalRandomGenerator.h:128
NormalRandomDistribution()
Definition: NormalRandomGenerator.h:136
Preprocessor workaround to handle single parameters that contain a comma.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
STL namespace.
Eigen::Matrix< T, D, 1 > result_type
Definition: NormalRandomGenerator.h:129
NormalRandomDistribution(const Eigen::Matrix< T, D, D > &sigma)
Definition: NormalRandomGenerator.h:140
Template class to easily generate random generators using the boost::random distributions and generat...
Definition: RandomGenerator.h:96
void setSigma(T sigma)
Sets the specified covariance matrix.
Definition: NormalRandomGenerator.h:198
detail::NormalRandomDistributionBase< T > Base
Definition: NormalRandomGenerator.h:130
NormalRandomGenerator()
Definition: NormalRandomGenerator.h:243
NormalRandomDistribution(T sigma)
Definition: NormalRandomGenerator.h:193
T result_type
Definition: NormalRandomGenerator.h:185
NormalRandomDistribution()
Definition: NormalRandomGenerator.h:191
Eigen::Matrix< T, D, 1 > operator()(Engine &eng)
Draws a sample from the normal distribution.
Definition: NormalRandomGenerator.h:168
bool setSigma(const Eigen::Matrix< T, D, D > &sigma)
Sets the specified covariance matrix.
Definition: NormalRandomGenerator.h:155
T operator()(Engine &eng)
Definition: NormalRandomGenerator.h:204
Random distribution for drawing samples from univariate or multivariate normal distributions.
Definition: NormalRandomGenerator.h:125
T input_type
Definition: NormalRandomGenerator.h:184
#define MIRA_RANDOM_GENERATOR_COMMON(Derived, TDistribution)
Macro to be used in derived random generators to supply the default interface such copy constructors ...
Definition: RandomGenerator.h:249
Random generator for drawing samples from univariate or multivariate normal distributions.
Definition: NormalRandomGenerator.h:239
Helper singleton to easily generate random generators using the boost::random distributions and gener...