9 #ifndef RandomGenerator_H 10 #define RandomGenerator_H 17 namespace math {
template <
class T>
class CMatrixTemplateNumeric; }
48 void MT19937_generateNumbers();
49 void MT19937_initializeGenerator(
const uint32_t &seed);
82 template <
typename T,
typename U,
typename V>
85 const T
range = max_val-min_val+1;
87 drawUniformUnsignedInt(rnd);
88 ret_number=min_val+ (rnd%
range);
93 return Min + (Max-Min)* drawUniform32bit() * 2.3283064370807973754314699618685e-10;
103 const double unif_min = 0,
104 const double unif_max = 1 )
106 for (
size_t r=0;
r<
matrix.getRowCount();
r++)
107 for (
size_t c=0;
c<
matrix.getColCount();
c++)
108 matrix.get_unsafe(
r,
c) =
static_cast<typename
MAT::Scalar>( drawUniform(unif_min,unif_max) );
117 const double unif_min = 0,
118 const double unif_max = 1 )
120 const size_t N =
v.size();
121 for (
size_t c=0;
c<N;
c++)
133 double drawGaussian1D_normalized(
double *likelihood = NULL);
140 return mean+
std*drawGaussian1D_normalized();
150 const double mean = 0,
151 const double std = 1 )
153 for (
size_t r=0;
r<
matrix.getRowCount();
r++)
154 for (
size_t c=0;
c<
matrix.getColCount();
c++)
160 mrpt::math::CMatrixDouble drawDefinitePositiveMatrix(
const size_t dim,
const double std_scale = 1.0,
const double diagonal_epsilon = 1e-8);
168 const double mean = 0,
169 const double std = 1 )
171 const size_t N =
v.size();
172 for (
size_t c=0;
c<N;
c++)
181 template <
typename T>
182 void drawGaussianMultivariate(
183 std::vector<T> &out_result,
185 const std::vector<T>*
mean = NULL
194 template <
class VECTORLIKE,
class COVMATRIX>
196 VECTORLIKE &out_result,
197 const COVMATRIX &
cov,
198 const VECTORLIKE*
mean = NULL
201 const size_t N =
cov.rows();
206 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject> eigensolver(
cov);
208 typename Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::MatrixType eigVecs = eigensolver.eigenvectors();
209 typename Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::RealVectorType eigVals = eigensolver.eigenvalues();
213 eigVals = eigVals.array().sqrt();
214 for (
typename COVMATRIX::Index i=0;i<eigVecs.cols();i++)
215 eigVecs.col(i) *= eigVals[i];
218 out_result.assign(N,0);
220 for (
size_t i=0;i<N;i++)
223 for (
size_t d=0;d<N;d++)
224 out_result[d]+= eigVecs.coeff(d,i) * rnd;
227 for (
size_t d=0;d<N;d++)
228 out_result[d]+= (*
mean)[d];
237 template <
typename VECTOR_OF_VECTORS,
typename COVMATRIX>
239 VECTOR_OF_VECTORS &ret,
240 size_t desiredSamples,
241 const COVMATRIX &
cov,
242 const typename VECTOR_OF_VECTORS::value_type *
mean = NULL )
248 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject> eigensolver(
cov);
250 typename Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::MatrixType eigVecs = eigensolver.eigenvectors();
251 typename Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::RealVectorType eigVals = eigensolver.eigenvalues();
255 eigVals = eigVals.array().sqrt();
256 for (
typename COVMATRIX::Index i=0;i<eigVecs.cols();i++)
257 eigVecs.col(i) *= eigVals[i];
260 ret.resize(desiredSamples);
261 const size_t N =
cov.cols();
262 for (
size_t k=0;k<desiredSamples;k++)
265 for (
size_t i=0;i<N;i++)
268 for (
size_t d=0;d<N;d++)
269 ret[k][d]+= eigVecs.coeff(d,i) * rnd;
272 for (
size_t d=0;d<N;d++)
273 ret[k][d]+= (*
mean)[d];
289 out_result = in_vector;
290 const size_t N = out_result.size();
292 std::random_shuffle( &out_result[0],&out_result[N-1] );
318 const double unif_min = 0,
319 const double unif_max = 1 )
321 for (
size_t r=0;
r<
matrix.getRowCount();
r++)
322 for (
size_t c=0;
c<
matrix.getColCount();
c++)
331 std::vector<T> &v_out,
332 const T& unif_min = 0,
333 const T& unif_max = 1 )
335 size_t n = v_out.size();
336 for (
size_t r=0;
r<
n;
r++)
347 const double mean = 0,
348 const double std = 1 )
350 for (
size_t r=0;
r<
matrix.getRowCount();
r++)
351 for (
size_t c=0;
c<
matrix.getColCount();
c++)
360 std::vector<T> &v_out,
364 size_t n = v_out.size();
365 for (
size_t r=0;
r<
n;
r++)
383 const std::vector<T> &in_vector,
384 std::vector<T> &out_result)
394 template <
typename T>
397 std::vector<T> &out_result)
412 template <
typename T>
415 size_t desiredSamples,
416 std::vector< std::vector<T> > &ret,
417 std::vector<T> *samplesLikelihoods = NULL)
426 template <
typename T,
typename MATRIXLIKE>
428 const MATRIXLIKE &
cov,
429 size_t desiredSamples,
430 std::vector< std::vector<T> > &ret )
439 template <
typename T,
typename MATRIXLIKE>
441 const MATRIXLIKE &
cov,
442 std::vector<T> &out_result)
#define ASSERT_EQUAL_(__A, __B)
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
void permuteVector(const VEC &in_vector, VEC &out_result)
Returns a random permutation of a vector: all the elements of the input vector are in the output but ...
void drawUniformUnsignedInt(uint32_t &ret_number)
You can call this overloaded method with either 32 or 64bit unsigned ints for the sake of general cod...
void drawGaussian1DVector(VEC &v, const double mean=0, const double std=1)
Fills the given vector with independent, 1D-normally distributed samples.
void randomNormalMultiDimensional(const mrpt::math::CMatrixTemplateNumeric< T > &cov, std::vector< T > &out_result)
Generate multidimensional random samples according to a given covariance matrix.
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
void drawGaussian1DMatrix(MAT &matrix, const double mean=0, const double std=1)
Fills the given matrix with independent, 1D-normally distributed samples.
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
void randomPermutation(const std::vector< T > &in_vector, std::vector< T > &out_result)
Returns a random permutation of a vector: all the elements of the input vector are in the output but ...
void drawUniformUnsignedIntRange(T &ret_number, const U min_val, const V max_val)
Return a uniform unsigned integer in the range [min_val,max_val] (both inclusive) ...
double drawGaussian1D_normalized(double *likelihood=NULL)
Generate a normalized (mean=0, std=1) normally distributed sample.
void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=NULL)
Generate a given number of multidimensional random samples according to a given covariance matrix...
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
void drawUniformMatrix(MAT &matrix, const double unif_min=0, const double unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
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...
Data used internally by the MT19937 PRNG algorithm.
void drawGaussianMultivariate(VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean=NULL)
Generate multidimensional random samples according to a given covariance matrix.
void Randomize(const uint32_t seed)
Randomize the generators.
void vectorRandomNormal(std::vector< T > &v_out, const T &mean=0, const T &std=1)
Generates a random vector with independent, normally distributed samples.
CRandomGenerator(const uint32_t seed)
Constructor for providing a custom random seed to initialize the PRNG.
ptrdiff_t random_generator_for_STL(ptrdiff_t i)
A random number generator for usage in STL algorithms expecting a function like this (eg...
void randomNormalMultiDimensionalMany(const mrpt::math::CMatrixTemplateNumeric< T > &cov, size_t desiredSamples, std::vector< std::vector< T > > &ret, std::vector< T > *samplesLikelihoods=NULL)
Generate a given number of multidimensional random samples according to a given covariance matrix...
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
void matrixRandomNormal(MAT &matrix, const double mean=0, const double std=1)
Fills the given matrix with independent, normally distributed samples.
void matrixRandomUni(MAT &matrix, const double unif_min=0, const double unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
CRandomGenerator()
Default constructor: initialize random seed based on current time.
CONTAINER::value_type element_t
void vectorRandomUni(std::vector< T > &v_out, const T &unif_min=0, const T &unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
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
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
void drawUniformUnsignedInt(uint64_t &ret_number)