Installation
You can install EigenRand by just downloading the source codes from the repository. Since EigenRand is a header-only library like Eigen, none of binaries needs to be installed. All you need is Eigen 3.3.7 and C++11 compiler.
Simple Random Matrix Generators
#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
MatrixXf mat = Rand::balanced<MatrixXf>(4, 4, urng);
std::cout << mat << std::endl;
ArrayXXf arr = Rand::normal<ArrayXXf>(10, 10, urng);
std::cout << arr << std::endl;
return 0;
}
std::mt19937_64 Vmt19937_64
same as std::mt19937_64 when EIGEN_DONT_VECTORIZE, Pmt19937_64<internal::Packet4i> when SSE2 enabled ...
Definition: PacketRandomEngine.h:583
Random Matrix Functions with suffix '-Like'
Basically, in order to call each random distribution function of EigenRand, template parameters must be passed following the dense matrix or array type to be created. But, if you have an instance of Eigen::Matrix or Eigen::Array already, you can use -Like function to generate a random matrix or array with the same type and shape.
#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
MatrixXf mat{ 10, 10 };
std::cout << mat << std::endl;
return 0;
}
const BalancedType< Derived, Urng > balancedLike(const Derived &o, Urng &&urng)
generates reals in a range [-1, 1]
Definition: Basic.h:463
Every random distribution function has its corresponding -Like function.
Efficient Reusable Generator
In the example above, functions, such as Eigen::Rand::balancedLike
, Eigen::Rand::normal
and so on, creates a generator internally each time to be called. If you want to generate random matrices from the same distribution, consider using Generator classes as following:
#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
Rand::NormalGen<float> norm_gen{ 1.0, 2.0 };
MatrixXf mat = norm_gen.template generate<MatrixXf>(10, 10, urng);
std::cout << mat << std::endl;
mat = norm_gen.generateLike(mat, urng);
std::cout << mat << std::endl;
return 0;
}
Drawing samples from Multivariate Distribution
EigenRand provides generators for some multivariate distributions.
#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
Vector4f mean{ 0, 1, 2, 3 };
Matrix4f cov;
cov << 1, 1, 0, 0,
1, 2, 0, 0,
0, 0, 3, 1,
0, 0, 1, 2;
{
Rand::MvNormalGen<float, 4> gen1{ mean, cov };
Vector4f sample = gen1.generate(urng);
Matrix<float, 4, -1> samples = gen1.generate(urng, 10);
}
{
Matrix4f sample = gen3.generate(urng);
Matrix<float, 4, -1> samples = gen3.generate(urng, 10);
}
return 0;
}
auto makeMvNormalGen(const MatrixBase< MeanTy > &mean, const MatrixBase< CovTy > &cov) -> MvNormalGen< typename MatrixBase< MeanTy >::Scalar, MatrixBase< MeanTy >::RowsAtCompileTime >
helper function constructing Eigen::Rand::MvNormal
Definition: MvNormal.h:125
auto makeWishartGen(Index df, const MatrixBase< ScaleTy > &scale) -> WishartGen< typename MatrixBase< ScaleTy >::Scalar, MatrixBase< ScaleTy >::RowsAtCompileTime >
helper function constructing Eigen::Rand::WishartGen
Definition: MvNormal.h:296