aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2014-01-07 20:23:35 +0100
committerGravatar Gael Guennebaud <g.gael@free.fr>2014-01-07 20:23:35 +0100
commit92190a1cafc3a8613e1200cd4ed81dbc509a93bd (patch)
tree43084ae6898fa9bd36e5d71363cc8dcdf8ca0a6c
parentac409f51f185a4654f9a5c38ab5d1652824836f6 (diff)
Add an example showing how to use C++11 random distributions
-rw-r--r--Eigen/src/Core/CwiseNullaryOp.h3
-rw-r--r--Eigen/src/Core/Random.h2
-rw-r--r--doc/special_examples/CMakeLists.txt19
-rw-r--r--doc/special_examples/random_cpp11.cpp14
4 files changed, 38 insertions, 0 deletions
diff --git a/Eigen/src/Core/CwiseNullaryOp.h b/Eigen/src/Core/CwiseNullaryOp.h
index 1d4ee50a8..124383114 100644
--- a/Eigen/src/Core/CwiseNullaryOp.h
+++ b/Eigen/src/Core/CwiseNullaryOp.h
@@ -138,6 +138,9 @@ DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& f
*
* The template parameter \a CustomNullaryOp is the type of the functor.
*
+ * Here is an example with C++11 random generators: \include random_cpp11.cpp
+ * Output: \verbinclude random_cpp11.out
+ *
* \sa class CwiseNullaryOp
*/
template<typename Derived>
diff --git a/Eigen/src/Core/Random.h b/Eigen/src/Core/Random.h
index a4492ddb6..fdd43ed0c 100644
--- a/Eigen/src/Core/Random.h
+++ b/Eigen/src/Core/Random.h
@@ -48,6 +48,8 @@ struct functor_traits<scalar_random_op<Scalar> >
* a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
* behavior with expressions involving random matrices.
*
+ * See DenseBase::NullaryExpr(Index, const CustomNullaryOp&) for an example using C++11 random generators.
+ *
* \sa DenseBase::setRandom(), DenseBase::Random(Index), DenseBase::Random()
*/
template<typename Derived>
diff --git a/doc/special_examples/CMakeLists.txt b/doc/special_examples/CMakeLists.txt
index 0c9b3c3ba..45e2339e3 100644
--- a/doc/special_examples/CMakeLists.txt
+++ b/doc/special_examples/CMakeLists.txt
@@ -19,3 +19,22 @@ if(QT4_FOUND)
add_dependencies(all_examples Tutorial_sparse_example)
endif(QT4_FOUND)
+
+check_cxx_compiler_flag("-std=c++11" EIGEN_COMPILER_SUPPORT_CPP11)
+if(EIGEN_COMPILER_SUPPORT_CPP11)
+ add_executable(random_cpp11 random_cpp11.cpp)
+ target_link_libraries(random_cpp11 ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO})
+ add_dependencies(all_examples random_cpp11)
+ ei_add_target_property(random_cpp11 COMPILE_FLAGS "-std=c++11")
+
+ get_target_property(random_cpp11_exec
+ random_cpp11 LOCATION)
+ add_custom_command(
+ TARGET random_cpp11
+ POST_BUILD
+ COMMAND ${random_cpp11_exec}
+ ARGS >${CMAKE_CURRENT_BINARY_DIR}/random_cpp11.out
+ )
+
+
+endif() \ No newline at end of file
diff --git a/doc/special_examples/random_cpp11.cpp b/doc/special_examples/random_cpp11.cpp
new file mode 100644
index 000000000..ccd7c77d0
--- /dev/null
+++ b/doc/special_examples/random_cpp11.cpp
@@ -0,0 +1,14 @@
+#include <Eigen/Core>
+#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";
+}