From db152b9ee6effd3799f70a621f495c427cb3c33f Mon Sep 17 00:00:00 2001 From: David Tellenbach Date: Mon, 21 Jan 2019 16:25:57 +0100 Subject: PR 572: Add initializer list constructors to Matrix and Array (include unit tests and doc) - {1,2,3,4,5,...} for fixed-size vectors only - {{1,2,3},{4,5,6}} for the general cases - {{1,2,3,4,5,....}} is allowed for both row and column-vector --- doc/TutorialMatrixClass.dox | 29 ++++++++++++++++++++++++- doc/snippets/Array_initializer_list2_cxx11.cpp | 3 +++ doc/snippets/Array_initializer_list_cxx11.cpp | 6 +++++ doc/snippets/Matrix_initializer_list2_cxx11.cpp | 3 +++ doc/snippets/Matrix_initializer_list_cxx11.cpp | 6 +++++ doc/snippets/Tutorial_std_sort_rows.cpp | 5 ----- doc/snippets/Tutorial_std_sort_rows_cxx11.cpp | 5 +++++ 7 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 doc/snippets/Array_initializer_list2_cxx11.cpp create mode 100644 doc/snippets/Array_initializer_list_cxx11.cpp create mode 100644 doc/snippets/Matrix_initializer_list2_cxx11.cpp create mode 100644 doc/snippets/Matrix_initializer_list_cxx11.cpp delete mode 100644 doc/snippets/Tutorial_std_sort_rows.cpp create mode 100644 doc/snippets/Tutorial_std_sort_rows_cxx11.cpp (limited to 'doc') diff --git a/doc/TutorialMatrixClass.dox b/doc/TutorialMatrixClass.dox index 7ea0cd789..fc0ce5b1e 100644 --- a/doc/TutorialMatrixClass.dox +++ b/doc/TutorialMatrixClass.dox @@ -101,13 +101,40 @@ Matrix3f a(3,3); \endcode and is a no-operation. -Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4: +Additionally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4: \code Vector2d a(5.0, 6.0); Vector3d b(5.0, 6.0, 7.0); Vector4d c(5.0, 6.0, 7.0, 8.0); \endcode +If C++11 is enabled, matrices can be constructed and initialized using initializer lists. In the case of fixed-sized vectors +and rowvectors a simple initializer list can be passed: +\code +Vector2i a {1, 2}; // A vector containing the elements {1, 2} +Matrix b {1, 2, 3, 4}; // A row-vector containing the elements {1, 2, 3, 4} +Matrix c {1, 2, 3, 4}; // A vector containing the elements {1, 2, 3, 4} +\endcode + +In the case of fixed or dynamically sized matrices an initializer list containing an initializer list for each row +can be passed. If the matrix is fixed-sized, the number of elements that are passed must match the dimensions. +\code +MatrixXi a { + {1, 2}, // first row + {3, 4} // second row +}; +Matrix b { + {2.0, 3.0, 4.0}, + {5.0, 6.0, 7.0}, +}; +\endcode + +In the case of vectors and rowvectors, the following shorthand notation can be used: +\code +VectorXd a {{1.5, 2.5, 3.5}}; // A vector with 3 rows +RowVectorXd b {{1.0, 2.0, 3.0, 4.0}}; // A rowvector with 4 columns +\endcode + \section TutorialMatrixCoeffAccessors Coefficient accessors The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators. diff --git a/doc/snippets/Array_initializer_list2_cxx11.cpp b/doc/snippets/Array_initializer_list2_cxx11.cpp new file mode 100644 index 000000000..20e74546a --- /dev/null +++ b/doc/snippets/Array_initializer_list2_cxx11.cpp @@ -0,0 +1,3 @@ +Array a {1, 2, 3, 4, 5, 6}; +Array b {1, 2, 3}; +cout << a << "\n\n" << b << endl; \ No newline at end of file diff --git a/doc/snippets/Array_initializer_list_cxx11.cpp b/doc/snippets/Array_initializer_list_cxx11.cpp new file mode 100644 index 000000000..d2f46e268 --- /dev/null +++ b/doc/snippets/Array_initializer_list_cxx11.cpp @@ -0,0 +1,6 @@ +Array a { + {1, 2, 3}, + {3, 4, 5} +}; +Array v {{1, 2, 3, 4, 5}}; +cout << a << "\n\n" << v << endl; \ No newline at end of file diff --git a/doc/snippets/Matrix_initializer_list2_cxx11.cpp b/doc/snippets/Matrix_initializer_list2_cxx11.cpp new file mode 100644 index 000000000..2fde52b8d --- /dev/null +++ b/doc/snippets/Matrix_initializer_list2_cxx11.cpp @@ -0,0 +1,3 @@ +Matrix a {1, 2, 3, 4, 5, 6}; +Matrix b {1, 2, 3}; +cout << a << "\n\n" << b << endl; \ No newline at end of file diff --git a/doc/snippets/Matrix_initializer_list_cxx11.cpp b/doc/snippets/Matrix_initializer_list_cxx11.cpp new file mode 100644 index 000000000..d68787ab6 --- /dev/null +++ b/doc/snippets/Matrix_initializer_list_cxx11.cpp @@ -0,0 +1,6 @@ +Matrix m { + {1, 2, 3}, + {4, 5, 6} +}; +VectorXi v {{1, 2}}; +cout << m << "\n\n" << v << endl; \ No newline at end of file diff --git a/doc/snippets/Tutorial_std_sort_rows.cpp b/doc/snippets/Tutorial_std_sort_rows.cpp deleted file mode 100644 index fdd850d13..000000000 --- a/doc/snippets/Tutorial_std_sort_rows.cpp +++ /dev/null @@ -1,5 +0,0 @@ -ArrayXXi A = ArrayXXi::Random(4,4).abs(); -cout << "Here is the initial matrix A:\n" << A << "\n"; -for(auto row : A.rowwise()) - std::sort(row.begin(), row.end()); -cout << "Here is the sorted matrix A:\n" << A << "\n"; \ No newline at end of file diff --git a/doc/snippets/Tutorial_std_sort_rows_cxx11.cpp b/doc/snippets/Tutorial_std_sort_rows_cxx11.cpp new file mode 100644 index 000000000..fdd850d13 --- /dev/null +++ b/doc/snippets/Tutorial_std_sort_rows_cxx11.cpp @@ -0,0 +1,5 @@ +ArrayXXi A = ArrayXXi::Random(4,4).abs(); +cout << "Here is the initial matrix A:\n" << A << "\n"; +for(auto row : A.rowwise()) + std::sort(row.begin(), row.end()); +cout << "Here is the sorted matrix A:\n" << A << "\n"; \ No newline at end of file -- cgit v1.2.3