18 #include <type_traits> 24 template <
typename _MatrixType>
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);
145 2.3283064370807973754314699618685e-10;
155 MAT&
matrix,
const double unif_min = 0,
const double unif_max = 1)
157 for (
size_t r = 0;
r <
matrix.rows();
r++)
158 for (
size_t c = 0;
c <
matrix.cols();
c++)
168 VEC&
v,
const double unif_min = 0,
const double unif_max = 1)
170 const size_t N =
v.size();
171 for (
size_t c = 0;
c < N;
c++)
217 template <
class MATRIX,
class AUXVECTOR_T = MATRIX>
219 const size_t dim,
const double std_scale = 1.0,
220 const double diagonal_epsilon = 1e-8)
222 AUXVECTOR_T
r(dim, 1);
227 for (
size_t i = 0; i < dim; i++)
228 cov(i, i) += diagonal_epsilon;
238 VEC&
v,
const double mean = 0,
const double std = 1)
240 const size_t N =
v.size();
241 for (
size_t c = 0;
c < N;
c++)
242 v[
c] =
static_cast<std::remove_reference_t<decltype(v[c])>
>(
252 template <
typename T,
typename MATRIX>
254 std::vector<T>& out_result,
const MATRIX&
cov,
255 const std::vector<T>*
mean =
nullptr)
259 throw std::runtime_error(
260 "drawGaussianMultivariate(): cov is not square.");
262 throw std::runtime_error(
263 "drawGaussianMultivariate(): mean and cov sizes ");
267 out_result.resize(dim, 0);
273 cov.eigenVectors(Z, D);
275 D = D.
array().sqrt().matrix();
276 Z.matProductOf_AB(Z, D);
277 for (
size_t i = 0; i < dim; i++)
280 for (
size_t d = 0; d < dim; d++) out_result[d] += (Z(d, i) * rnd);
283 for (
size_t d = 0; d < dim; d++) out_result[d] += (*
mean)[d];
292 template <
class VECTORLIKE,
class COVMATRIX>
294 VECTORLIKE& out_result,
const COVMATRIX&
cov,
295 const VECTORLIKE*
mean =
nullptr)
299 throw std::runtime_error(
300 "drawGaussianMultivariate(): cov is not square.");
301 if (
mean &&
size_t(
mean->size()) != N)
302 throw std::runtime_error(
303 "drawGaussianMultivariate(): mean and cov sizes ");
307 std::vector<typename COVMATRIX::Scalar> eigVals;
312 for (
typename COVMATRIX::Index
c = 0;
c < eigVecs.cols();
c++)
314 const auto s = std::sqrt(eigVals[
c]);
315 for (
typename COVMATRIX::Index
r = 0;
r < eigVecs.rows();
r++)
320 out_result.assign(N, 0);
322 for (
size_t i = 0; i < N; i++)
325 for (
size_t d = 0; d < N; d++)
326 out_result[d] += eigVecs.coeff(d, i) * rnd;
329 for (
size_t d = 0; d < N; d++) out_result[d] += (*
mean)[d];
339 template <
typename VECTOR_OF_VECTORS,
typename COVMATRIX>
341 VECTOR_OF_VECTORS& ret,
size_t desiredSamples,
const COVMATRIX&
cov,
342 const typename VECTOR_OF_VECTORS::value_type*
mean =
nullptr)
346 throw std::runtime_error(
347 "drawGaussianMultivariateMany(): cov is not square.");
348 if (
mean &&
size_t(
mean->size()) != N)
349 throw std::runtime_error(
350 "drawGaussianMultivariateMany(): mean and cov sizes ");
354 std::vector<typename COVMATRIX::Scalar> eigVals;
359 for (
typename COVMATRIX::Index
c = 0;
c < eigVecs.cols();
c++)
361 const auto s = std::sqrt(eigVals[
c]);
362 for (
typename COVMATRIX::Index
r = 0;
r < eigVecs.rows();
r++)
367 ret.resize(desiredSamples);
368 for (
size_t k = 0; k < desiredSamples; k++)
371 for (
size_t i = 0; i < N; i++)
374 for (
size_t d = 0; d < N; d++)
375 ret[k][d] += eigVecs.coeff(d, i) * rnd;
378 for (
size_t d = 0; d < N; d++) ret[k][d] += (*
mean)[d];
393 out_result = in_vector;
394 const size_t N = out_result.size();
422 MAT&
matrix,
const double unif_min = 0,
const double unif_max = 1)
424 for (
typename MAT::Index
r = 0;
r <
matrix.rows();
r++)
425 for (
typename MAT::Index
c = 0;
c <
matrix.cols();
c++)
435 std::vector<T>& v_out,
const T& unif_min = 0,
const T& unif_max = 1)
437 size_t n = v_out.size();
438 for (
size_t r = 0;
r <
n;
r++)
451 for (
typename MAT::Index
r = 0;
r <
matrix.rows();
r++)
452 for (
typename MAT::Index
c = 0;
c <
matrix.cols();
c++)
462 std::vector<T>& v_out,
const T&
mean = 0,
const T&
std = 1)
464 size_t n = v_out.size();
465 for (
size_t r = 0;
r <
n;
r++)
483 const std::vector<T>& in_vector, std::vector<T>& out_result)
501 template <
typename T,
typename MATRIX>
503 const MATRIX&
cov,
size_t desiredSamples, std::vector<std::vector<T>>& ret,
504 std::vector<T>* samplesLikelihoods =
nullptr)
507 ret, desiredSamples,
cov,
static_cast<const std::vector<T>*
>(
nullptr),
516 template <
typename T,
typename MATRIXLIKE>
518 const MATRIXLIKE&
cov,
size_t desiredSamples,
519 std::vector<std::vector<T>>& ret)
529 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.
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 ...
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()
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
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.
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.
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...
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
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)
unsigned __int32 uint32_t
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.
GLuint GLuint GLsizei GLenum type
void drawUniformUnsignedInt(uint64_t &ret_number)
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.