Main MRPT website > C++ reference for MRPT 1.9.9
RandomGenerator.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include <mrpt/system/datetime.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::random;
18 using namespace mrpt::math;
19 using namespace mrpt::utils;
20 using namespace std;
21 
22 // The global instance of CRandomGenerator for single-thread programs:
24 
26 {
27  return randomGenerator;
28 }
29 
30 
31 // MT19937 algorithm
32 // http://en.wikipedia.org/wiki/Mersenne_twister
33 // Initialize the generator from a seed
35 {
36  m_MT19937.seed(seed);
37 }
38 
39 uint64_t CRandomGenerator::drawUniform64bit() { return m_uint64(m_MT19937); }
40 // MT19937 algorithm
41 // http://en.wikipedia.org/wiki/Mersenne_twister
42 uint32_t CRandomGenerator::drawUniform32bit() { return m_uint32(m_MT19937); }
43 /*---------------------------------------------------------------
44  Randomize
45  ---------------------------------------------------------------*/
47 {
48  MT19937_initializeGenerator(seed);
49 }
50 
51 /*---------------------------------------------------------------
52  Randomize
53  ---------------------------------------------------------------*/
55 {
56  MT19937_initializeGenerator(
57  static_cast<uint32_t>(mrpt::system::getCurrentTime()));
58 }
59 
60 /*---------------------------------------------------------------
61  normalizedGaussian
62  ---------------------------------------------------------------*/
64 {
65  return m_normdistribution(m_MT19937);
66 }
67 
68 /** Generates a random definite-positive matrix of the given size, using the
69  * formula C = v*v^t + epsilon*I, with "v" being a vector of gaussian random
70  * samples.
71 */
73  const size_t dim, const double std_scale, const double diagonal_epsilon)
74 {
75  CMatrixDouble r(dim, 1);
76  drawGaussian1DMatrix(r, 0, std_scale);
77  CMatrixDouble cov(dim, dim);
78  cov.multiply_AAt(r); // random semi-definite positive matrix:
79  for (size_t i = 0; i < dim; i++)
80  cov(i, i) += diagonal_epsilon; // make sure it's definite-positive
81  return cov;
82 }
83 
84 /*---------------------------------------------------------------
85  drawGaussianMultivariate
86  ---------------------------------------------------------------*/
87 template <typename T>
89  std::vector<T>& out_result, const CMatrixTemplateNumeric<T>& cov,
90  const std::vector<T>* mean)
91 {
92  ASSERT_(cov.getRowCount() == cov.getColCount());
93  const size_t dim = cov.getColCount();
94 
95  if (mean) ASSERT_(mean->size() == dim)
96 
98 
100 
101  // Set size of output vector:
102  out_result.clear();
103  out_result.resize(dim, 0);
104 
105  /** Computes the eigenvalues/eigenvector decomposition of this matrix,
106  * so that: M = Z · D · Z<sup>T</sup>, where columns in Z are the
107  * eigenvectors and the diagonal matrix D contains the eigenvalues
108  * as diagonal elements, sorted in <i>ascending</i> order.
109  */
110  cov.eigenVectors(Z, D);
111 
112  // Scale eigenvectors with eigenvalues:
113  D = D.array().sqrt().matrix();
114  Z.multiply(Z, D);
115 
116  for (size_t i = 0; i < dim; i++)
117  {
118  T rnd = this->drawGaussian1D_normalized();
119  for (size_t d = 0; d < dim; d++)
120  out_result[d] += (Z.get_unsafe(d, i) * rnd);
121  }
122  if (mean)
123  for (size_t d = 0; d < dim; d++) out_result[d] += (*mean)[d];
124 
126  printf("\nEXCEPTION: Dumping variables for debugging:\n");
127  std::cout << "Z:\n"
128  << Z << "D:\n"
129  << D << "Cov:\n"
130  << cov;
131  try {
132  cov.eigenVectors(Z, D);
133  std::cout << "Original Z:" << Z << "Original D:" << D;
134  } catch (...){};);
135 }
136 
137 // Instantiations:
138 template void CRandomGenerator::drawGaussianMultivariate<double>(
139  std::vector<double>& out_result, const CMatrixTemplateNumeric<double>& cov,
140  const std::vector<double>* mean);
141 template void CRandomGenerator::drawGaussianMultivariate<float>(
142  std::vector<float>& out_result, const CMatrixTemplateNumeric<float>& cov,
143  const std::vector<float>* mean);
144 
145 namespace mrpt
146 {
147 namespace random
148 {
150 {
152 }
153 
154 double RandomNormal(double mean, double std)
155 {
157 }
158 
160 double RandomUni(const double min, const double max)
161 {
162  return min +
163  (max - min) * getRandomGenerator().drawUniform32bit() *
164  2.3283064370807973754314699618685e-10; // 0xFFFFFFFF ^ -1
165 }
166 }
167 } // end of namespace
A namespace of pseudo-random numbers genrators of diferent distributions.
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
double RandomNormal(double mean, double std)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
#define MRPT_END_WITH_CLEAN_UP(stuff)
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:73
STL namespace.
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
static CRandomGenerator randomGenerator
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
uint32_t RandomUniInt()
uint64_t drawUniform64bit()
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32b...
#define MRPT_START
mrpt::math::CMatrixDouble drawDefinitePositiveMatrix(const size_t dim, const double std_scale=1.0, const double diagonal_epsilon=1e-8)
Generates a random definite-positive matrix of the given size, using the formula C = v*v^t + epsilon*...
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void randomize()
Randomize the generators, based on current time.
void MT19937_initializeGenerator(const uint32_t &seed)
#define ASSERT_(f)
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
Definition: ops_matrices.h:148
double RandomUni(const double min, const double max)
void drawGaussianMultivariate(std::vector< T > &out_result, const mrpt::math::CMatrixTemplateNumeric< T > &cov, const std::vector< T > *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
unsigned __int32 uint32_t
Definition: rptypes.h:47
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.
double normalizedGaussian()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019