This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen

Bug 720

Summary: Gaussian NullaryExpr
Product: Eigen Reporter: nfoti01
Component: Core - generalAssignee: Nobody <eigen.nobody>
Status: RESOLVED WONTFIX    
Severity: enhancement CC: chtz, dalves31, gael.guennebaud, jacob.benoit.1
Priority: Normal    
Version: unspecified   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 1286    
Bug Blocks: 814    

Description nfoti01 2013-12-23 23:55:38 UTC
Drawing matrices with standard normal entries is so common and having to write the functor manually is such a pain (see stackoverflow.com/questions/6142576/sample-from-multivariate-normal-gaussian-distribution-in-c).  Could a `DenseBase::Gaussian` be added to generate matrices with standard normal entries.

Honestly, given how tedious it is to create new `NullaryExpr`s it'd be really useful to be able to generate matrices according to all of the distributions that C++ handles in the random library (normal, Poisson, Bernoulli, binomial, etc.).

I don't think this is too hard to implement, it's basically just redoing `Random` for the different distributions, however, I'm new to the project and wanted to get peoples' opinions.

Thanks.
Comment 1 Christoph Hertzberg 2013-12-24 02:41:40 UTC
I would somehow prefer merely to simplify the construction of nullary expressions. I.e., having an arbitrary functor object make the following valid:

  RNG rng; // random number generator which only needs to provide operator()
  MatrixXd A = MatrixXd::Nullary(rows, cols, rng);
  MatrixXd B;
  B.setNullary(rows, cols, rng); // alternative syntax (suggestion)

Currently, custom nullary functors must provide both operator() and packetOp; both have to accept two indexes and must be const (which requires a small hack for RNGs which modify their state).
Comment 2 nfoti01 2013-12-24 03:03:03 UTC
Simplifying how nullary expressions are constructed would be great as an alternative.  I will leave this to those with more experience for now, but this would make doing a lot of statistical computing and machine learning much easier.

Thanks.
Comment 3 Gael Guennebaud 2013-12-24 09:57:33 UTC
it's not that complicated:

#include <Eigen/Sparse>
#include <iostream>
#include <random>

using namespace Eigen;

int main() {
  std::default_random_engine generator;
  std::poisson_distribution<int> distribution(4.1);
  auto poisson = [&] (int) {return distribution(generator);};

  RowVectorXi v = RowVectorXi::NullaryExpr(10, poisson );
  std::cout << v << "\n";
}
Comment 4 nfoti01 2013-12-24 19:46:23 UTC
That's not bad at all.  The only example I could find was much more complicated.

Thanks.

This can probably be closed now.
Comment 5 Gael Guennebaud 2014-10-20 14:16:29 UTC
I don't think we can do any better for 3.3, but let's keep the discussion open for 3.4.
Comment 6 Daniel A Paladim 2017-05-22 22:33:23 UTC
I have implemented my own solution in which the matrices can be constructed with expressions of the form

default_random_engine engine;
MatrixXd mtx = MatrixXd::Distribution<normal_distribution<double>>(rows, cols, engine, mean, stddev);
MatrixXi mtx_b = MatrixXi::Distribution<geometric_distribution<int>>(rows, cols, engine, p);

Since this remains open, I wonder if you would like me to push it to the repository.
Comment 7 Gael Guennebaud 2019-02-20 15:07:54 UTC
Sorry Daniel for not providing feedback for so long. I guess that's too specific, so I'm closing.
Comment 8 Nobody 2019-12-04 12:53:58 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/720.