30 m_MT19937_data.seed_initialized =
true;
31 m_MT19937_data.MT[0] = seed;
33 m_MT19937_data.MT[i] = static_cast<uint32_t>( 1812433253 * (m_MT19937_data.MT[i-1] ^ ( m_MT19937_data.MT[i-1] >> 30 )) + i);
38 #pragma warning(disable:4146) 57 if (!m_MT19937_data.seed_initialized) this->randomize();
68 for(
int i = N - M; i--; ++
p )
70 for(
int i = M; --i; ++
p )
72 *
p =
twist(
p[M-N],
p[0], m_MT19937_data.MT[0] );
79 return static_cast<uint64_t>(n1) | (static_cast<uint64_t>(n2)<<32);
86 if (!m_MT19937_data.index)
87 MT19937_generateNumbers();
89 uint32_t y = m_MT19937_data.MT[m_MT19937_data.index];
91 y ^= (
y << 7) & 2636928640U;
92 y ^= (
y << 15) & 4022730752U;
96 m_MT19937_data.index++;
97 if (m_MT19937_data.index>=624) m_MT19937_data.index=0;
107 MT19937_initializeGenerator(seed);
108 m_MT19937_data.index = 0;
117 m_MT19937_data.index = 0;
127 if (!m_std_gauss_set)
132 v1 = this->drawUniform(-1.0,1.0);
133 v2 = this->drawUniform(-1.0,1.0);
136 }
while (
r >= 1.0 ||
r==0.0);
137 const double fac = std::sqrt(-2.0*log(
r)/
r);
140 m_std_gauss_next =
v1 * fac;
141 m_std_gauss_set =
true;
145 const double x =
v2*fac;
146 *likelihood = 0.39894228040143267794 * exp( -0.5*
x*
x );
156 m_std_gauss_set =
false;
160 const double x = m_std_gauss_next;
161 *likelihood = 0.39894228040143267794 * exp( -0.5*
x*
x );
166 return m_std_gauss_next;
175 const double std_scale,
176 const double diagonal_epsilon)
179 drawGaussian1DMatrix(
r, 0,std_scale);
182 for (
size_t i=0;i<dim;i++)
cov(i,i)+=diagonal_epsilon;
190 template <
typename T>
192 std::vector<T> &out_result,
194 const std::vector<T>*
mean )
197 const size_t dim =
cov.getColCount();
207 out_result.resize(dim,0);
214 cov.eigenVectors( Z, D );
217 D = D.array().sqrt().matrix();
220 for (
size_t i=0;i<dim;i++)
222 T rnd = this->drawGaussian1D_normalized();
223 for (
size_t d=0;d<dim;d++)
224 out_result[d]+= ( Z.get_unsafe(d,i)* rnd );
227 for (
size_t d=0;d<dim;d++) out_result[d]+= (*
mean)[d];
230 printf(
"\nEXCEPTION: Dumping variables for debugging:\n"); \
231 std::cout <<
"Z:\n" << Z <<
"D:\n" << D <<
"Cov:\n" <<
cov; \
234 cov.eigenVectors(Z,D); \
235 std::cout <<
"Original Z:" << Z <<
"Original D:" << D; \
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.
uint32_t loBit(const uint32_t u)
#define MRPT_END_WITH_CLEAN_UP(stuff)
mrpt::system::TTimeStamp BASE_IMPEXP getCurrentTime()
Returns the current (UTC) system time.
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
double drawGaussian1D_normalized(double *likelihood=NULL)
Generate a normalized (mean=0, std=1) normally distributed sample.
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
This base provides a set of functions for maths stuff.
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...
void MT19937_generateNumbers()
double normalizedGaussian(double *likelihood)
uint32_t hiBit(const uint32_t u)
uint64_t drawUniform64bit()
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32b...
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
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
uint32_t mixBits(const uint32_t u, const uint32_t v)
void randomize()
Randomize the generators, based on current time.
void MT19937_initializeGenerator(const uint32_t &seed)
uint32_t twist(const uint32_t m, const uint32_t s0, const uint32_t s1)
double RandomUni(const double min, const double max)
GLfloat GLfloat GLfloat v2
void drawGaussianMultivariate(std::vector< T > &out_result, const mrpt::math::CMatrixTemplateNumeric< T > &cov, const std::vector< T > *mean=NULL)
Generate multidimensional random samples according to a given covariance matrix.
unsigned __int32 uint32_t
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
uint32_t loBits(const uint32_t u)