18 #include <type_traits> 24 template <
typename _MatrixType>
48 return std::numeric_limits<result_type>::min();
52 return std::numeric_limits<result_type>::max();
86 std::uniform_int_distribution<uint32_t>
m_uint32;
87 std::uniform_int_distribution<uint64_t>
m_uint64;
129 template <
typename T,
typename U,
typename V>
131 T& ret_number,
const U min_val,
const V max_val)
133 const T range = max_val - min_val + 1;
136 ret_number = min_val + (rnd % range);
141 template <
typename return_t =
double>
144 double k = 2.3283064370807973754314699618685e-10;
145 return static_cast<return_t
>(
156 MAT& matrix,
const double unif_min = 0,
const double unif_max = 1)
158 for (
size_t r = 0; r < matrix.rows(); r++)
159 for (
size_t c = 0; c < matrix.cols(); c++)
169 VEC& v,
const double unif_min = 0,
const double unif_max = 1)
171 const size_t N = v.size();
172 for (
size_t c = 0; c < N; c++)
173 v[c] =
static_cast<typename std::decay<decltype(v[c])>::type
>(
193 template <
typename return_t =
double>
207 MAT& matrix,
const double mean = 0,
const double std = 1)
209 for (decltype(matrix.rows()) r = 0; r < matrix.rows(); r++)
210 for (decltype(matrix.cols()) c = 0; c < matrix.cols(); c++)
219 template <
class MATRIX,
class AUXVECTOR_T = MATRIX>
221 const size_t dim,
const double std_scale = 1.0,
222 const double diagonal_epsilon = 1e-8)
224 AUXVECTOR_T r(dim, 1);
229 for (
size_t i = 0; i < dim; i++)
230 cov(i, i) += diagonal_epsilon;
240 VEC& v,
const double mean = 0,
const double std = 1)
242 const size_t N = v.size();
243 for (
size_t c = 0; c < N; c++)
244 v[c] =
static_cast<std::remove_reference_t<decltype(v[c])>
>(
254 template <
typename T,
typename MATRIX>
256 std::vector<T>& out_result,
const MATRIX&
cov,
257 const std::vector<T>*
mean =
nullptr)
261 throw std::runtime_error(
262 "drawGaussianMultivariate(): cov is not square.");
264 throw std::runtime_error(
265 "drawGaussianMultivariate(): mean and cov sizes ");
269 out_result.resize(dim, 0);
275 cov.eigenVectors(Z, D);
277 D = D.
array().sqrt().matrix();
278 Z.matProductOf_AB(Z, D);
279 for (
size_t i = 0; i < dim; i++)
282 for (
size_t d = 0; d < dim; d++) out_result[d] += (Z(d, i) * rnd);
285 for (
size_t d = 0; d < dim; d++) out_result[d] += (*
mean)[d];
294 template <
class VECTORLIKE,
class COVMATRIX>
296 VECTORLIKE& out_result,
const COVMATRIX&
cov,
297 const VECTORLIKE*
mean =
nullptr)
301 throw std::runtime_error(
302 "drawGaussianMultivariate(): cov is not square.");
303 if (
mean &&
size_t(
mean->size()) != N)
304 throw std::runtime_error(
305 "drawGaussianMultivariate(): mean and cov sizes ");
309 std::vector<typename COVMATRIX::Scalar> eigVals;
314 for (
typename COVMATRIX::Index c = 0; c < eigVecs.cols(); c++)
316 const auto s = std::sqrt(eigVals[c]);
317 for (
typename COVMATRIX::Index r = 0; r < eigVecs.rows(); r++)
322 out_result.assign(N, 0);
324 for (
size_t i = 0; i < N; i++)
327 for (
size_t d = 0; d < N; d++)
328 out_result[d] += eigVecs.coeff(d, i) * rnd;
331 for (
size_t d = 0; d < N; d++) out_result[d] += (*
mean)[d];
341 template <
typename VECTOR_OF_VECTORS,
typename COVMATRIX>
343 VECTOR_OF_VECTORS& ret,
size_t desiredSamples,
const COVMATRIX&
cov,
344 const typename VECTOR_OF_VECTORS::value_type*
mean =
nullptr)
348 throw std::runtime_error(
349 "drawGaussianMultivariateMany(): cov is not square.");
350 if (
mean &&
size_t(
mean->size()) != N)
351 throw std::runtime_error(
352 "drawGaussianMultivariateMany(): mean and cov sizes ");
356 std::vector<typename COVMATRIX::Scalar> eigVals;
361 for (
typename COVMATRIX::Index c = 0; c < eigVecs.cols(); c++)
363 const auto s = std::sqrt(eigVals[c]);
364 for (
typename COVMATRIX::Index r = 0; r < eigVecs.rows(); r++)
369 ret.resize(desiredSamples);
370 for (
size_t k = 0; k < desiredSamples; k++)
373 for (
size_t i = 0; i < N; i++)
376 for (
size_t d = 0; d < N; d++)
377 ret[k][d] += eigVecs.coeff(d, i) * rnd;
380 for (
size_t d = 0; d < N; d++) ret[k][d] += (*
mean)[d];
395 out_result = in_vector;
396 const size_t N = out_result.size();
424 MAT& matrix,
const double unif_min = 0,
const double unif_max = 1)
426 for (
typename MAT::Index r = 0; r < matrix.rows(); r++)
427 for (
typename MAT::Index c = 0; c < matrix.cols(); c++)
437 std::vector<T>& v_out,
const T& unif_min = 0,
const T& unif_max = 1)
439 size_t n = v_out.size();
440 for (
size_t r = 0; r < n; r++)
451 MAT& matrix,
const double mean = 0,
const double std = 1)
453 for (
typename MAT::Index r = 0; r < matrix.rows(); r++)
454 for (
typename MAT::Index c = 0; c < matrix.cols(); c++)
464 std::vector<T>& v_out,
const T&
mean = 0,
const T&
std = 1)
466 size_t n = v_out.size();
467 for (
size_t r = 0; r < n; r++)
485 const std::vector<T>& in_vector, std::vector<T>& out_result)
503 template <
typename T,
typename MATRIX>
505 const MATRIX&
cov,
size_t desiredSamples, std::vector<std::vector<T>>& ret,
506 std::vector<T>* samplesLikelihoods =
nullptr)
509 ret, desiredSamples,
cov,
static_cast<const std::vector<T>*
>(
nullptr),
518 template <
typename T,
typename MATRIXLIKE>
520 const MATRIXLIKE&
cov,
size_t desiredSamples,
521 std::vector<std::vector<T>>& ret)
531 template <
typename T,
typename MATRIX>
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
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 ...
MATRIX 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*...
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 resize(size_t row, size_t col)
void shuffle(RandomIt first, RandomIt last, URBG &&g)
Uniform shuffle a sequence.
void drawGaussian1DVector(VEC &v, const double mean=0, const double std=1)
Fills the given vector with independent, 1D-normally distributed samples.
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.
std::uniform_int_distribution< uint64_t > m_uint64
std::normal_distribution< double > m_normdistribution
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) ...
bool eig_symmetric(Derived &eVecs, std::vector< Scalar > &eVals, bool sorted=true) const
Read: eig()
void matProductOf_AAt(const MAT_A &A)
this = A * AT
void drawUniformMatrix(MAT &matrix, const double unif_min=0, const double unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
return_t drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
void drawGaussianMultivariate(VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
Portable MT19937 random generator, C++11 UniformRandomBitGenerator compliant.
static constexpr result_type min()
void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
static constexpr result_type max()
void Randomize(const uint32_t seed)
Randomize the generators.
return_t drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
CMatrixDouble cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
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.
size_type rows() const
Number of rows in the matrix.
size_type cols() const
Number of columns in the matrix.
ptrdiff_t random_generator_for_STL(ptrdiff_t i)
A random number generator for usage in STL algorithms expecting a function like this (eg...
uint64_t drawUniform64bit()
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32b...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void drawGaussianMultivariate(std::vector< T > &out_result, const MATRIX &cov, const std::vector< T > *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
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.
void randomize()
Randomize the generators, based on std::random_device.
double mean(const CONTAINER &v)
Computes the mean value of a vector.
std::uniform_int_distribution< uint32_t > m_uint32
CRandomGenerator()
Default constructor: initialize random seed based on current time.
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.
Generator_MT19937 m_MT19937
Data used internally by the MT19937 PRNG algorithm.
void randomNormalMultiDimensionalMany(const MATRIX &cov, size_t desiredSamples, std::vector< std::vector< T >> &ret, std::vector< T > *samplesLikelihoods=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
void randomNormalMultiDimensional(const MATRIX &cov, std::vector< T > &out_result)
Generate multidimensional random samples according to a given covariance matrix.
void seed(const uint32_t seed)
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
void drawUniformUnsignedInt(uint64_t &ret_number)
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.