New user self-registration is disabled due to spam. Please email eigen-core-team @ lists.tuxfamily.org if you need an account.
Before reporting a bug, please make sure that your Eigen version is up-to-date!
Bug 720 - Gaussian NullaryExpr
Summary: Gaussian NullaryExpr
Status: RESOLVED WONTFIX
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: unspecified
Hardware: All All
: Normal enhancement
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on: 1286
Blocks: 3.4
  Show dependency treegraph
 
Reported: 2013-12-23 23:55 UTC by nfoti01
Modified: 2019-02-20 15:07 UTC (History)
4 users (show)



Attachments

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.

Note You need to log in before you can comment on or make changes to this bug.