From db152b9ee6effd3799f70a621f495c427cb3c33f Mon Sep 17 00:00:00 2001 From: David Tellenbach Date: Mon, 21 Jan 2019 16:25:57 +0100 Subject: [PATCH] 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 --- Eigen/src/Core/Array.h | 48 ++- Eigen/src/Core/Matrix.h | 50 ++- Eigen/src/Core/PlainObjectBase.h | 71 ++++ doc/TutorialMatrixClass.dox | 29 +- .../Array_initializer_list2_cxx11.cpp | 3 + doc/snippets/Array_initializer_list_cxx11.cpp | 6 + .../Matrix_initializer_list2_cxx11.cpp | 3 + .../Matrix_initializer_list_cxx11.cpp | 6 + ...s.cpp => Tutorial_std_sort_rows_cxx11.cpp} | 0 failtest/CMakeLists.txt | 5 + failtest/initializer_list_1.cpp | 14 + failtest/initializer_list_2.cpp | 16 + test/CMakeLists.txt | 3 + test/initializer_list_construction.cpp | 371 ++++++++++++++++++ 14 files changed, 622 insertions(+), 3 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 rename doc/snippets/{Tutorial_std_sort_rows.cpp => Tutorial_std_sort_rows_cxx11.cpp} (100%) create mode 100644 failtest/initializer_list_1.cpp create mode 100644 failtest/initializer_list_2.cpp create mode 100644 test/initializer_list_construction.cpp diff --git a/Eigen/src/Core/Array.h b/Eigen/src/Core/Array.h index 16770fc7b..7ef37de7c 100644 --- a/Eigen/src/Core/Array.h +++ b/Eigen/src/Core/Array.h @@ -178,6 +178,20 @@ class Array Base::_check_template_params(); this->template _init2(val0, val1); } + + #if EIGEN_HAS_CXX11 + template + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Array(const std::initializer_list& list, + typename internal::enable_if::value, T>::type* = 0, + typename internal::enable_if::type* = 0) : Base(list) {} + + EIGEN_DEVICE_FUNC + EIGEN_STRONG_INLINE Array(const std::initializer_list >& list) : Base(list) {} + #endif // end EIGEN_HAS_CXX11 + #else /** \brief Constructs a fixed-sized array initialized with coefficients starting at \a data */ EIGEN_DEVICE_FUNC explicit Array(const Scalar *data); @@ -199,7 +213,39 @@ class Array Array(Index rows, Index cols); /** constructs an initialized 2D vector with given coefficients */ Array(const Scalar& val0, const Scalar& val1); - #endif + + /** \copydoc PlainObjectBase::PlainObjectBase(const std::initializer_list& list) + * + * Example: \include Array_initializer_list2_cxx11.cpp + * Output: \verbinclude Array_initializer_list2_cxx11.out + * + * \sa Array::Array(const Scalar& val0, const Scalar& val1) + * \sa Array::Array(const Scalar& val0, const Scalar& val1, const Scalar& val2) */ + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Array(const std::initializer_list& list); + + /** + * \brief Constructs an array and initializes it by elements given by an initializer list of initializer lists \cpp11 + * + * This constructor distinguishes between the construction of arbitrary array and arrays with one fixed dimension, + * + * In the general case, the constructor takes an initializer list, representing the array rows, that contains for + * each row an initializer list, representing a single column, containing scalar values. Each of the inner + * initializer lists must contain the same number of elements. + * + * In the case of array with one fixed dimension, an initializer list containing just one other initializer list + * that contains the array elements can be passed. Therefore \c Array\c {{1,\c 2,\c 3,\c 4}} is + * legal and the more verbose syntax \c Array\c {{1},\c {2},\c {3},\c {4}} can be avoided. + * + * \warning In the case of fixed-sized arrays, the initializer list size must be equal to the array \a rows rows + * and \a cols columns. + * + * Example: \include Array_initializer_list_cxx11.cpp + * Output: \verbinclude Array_initializer_list_cxx11.out + */ + EIGEN_DEVICE_FUNC + EIGEN_STRONG_INLINE Array(const std::initializer_list >& list); + #endif // end EIGEN_PARSED_BY_DOXYGEN /** constructs an initialized 3D vector with given coefficients */ EIGEN_DEVICE_FUNC diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index 7f4a7af93..5b375b41d 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -301,6 +301,20 @@ class Matrix Base::_check_template_params(); Base::template _init2(x, y); } + + #if EIGEN_HAS_CXX11 + template + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list& list, + typename internal::enable_if::value, T>::type* = 0, + typename internal::enable_if::type* = 0) : Base(list) {} + + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list>& list) : Base(list) {} + #endif // end EIGEN_HAS_CXX11 + #else /** \brief Constructs a fixed-sized matrix initialized with coefficients starting at \a data */ EIGEN_DEVICE_FUNC @@ -338,7 +352,41 @@ class Matrix /** \brief Constructs an initialized 2D vector with given coefficients */ Matrix(const Scalar& x, const Scalar& y); - #endif + + /** \copydoc PlainObjectBase::PlainObjectBase(const std::initializer_list& list) + * + * Example: \include Matrix_initializer_list2_cxx11.cpp + * Output: \verbinclude Matrix_initializer_list2_cxx11.out + * + * \sa Matrix::Matrix(const Scalar& x, const Scalar& y, const Scalar& z) + * \sa Matrix::Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w) */ + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list& list); + + /** + * \brief Constructs a matrix and initializes it by elements given by an initializer list of initializer lists \cpp11 + * + * This constructor distinguishes between the construction of arbitrary matrices and matrices with one fixed dimension, + * i.e., vectors or rowvectors. + * + * In the general case, the constructor takes an initializer list, representing the matrix rows, that contains for + * each row an initializer list, representing a single column, containing scalar values. Each of the inner + * initializer lists must contain the same number of elements. + * + * In the case of matrices with one fixed dimension, an initializer list containing just one other initializer list + * that contains the matrix elements can be passed. Therefore \c VectorXi\c {{1,\c 2,\c 3,\c 4}} is legal and the more + * verbose syntax \c VectorXi\c {{1},\c {2},\c {3},\c {4}} can be avoided. + * + * \warning In the case of fixed-sized matrices, the initializer list size must be equal to the matrix \a rows rows + * and \a cols columns. + * + * Example: \include Matrix_initializer_list_cxx11.cpp + * Output: \verbinclude Matrix_initializer_list_cxx11.out + */ + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list>& list); + + #endif // end EIGEN_PARSED_BY_DOXYGEN /** \brief Constructs an initialized 3D vector with given coefficients */ EIGEN_DEVICE_FUNC diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h index f551dabb0..1a996b0aa 100644 --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -526,6 +526,77 @@ class PlainObjectBase : public internal::dense_xpr_base::type // EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED } + #ifdef EIGEN_PARSED_BY_DOXYGEN + /** + * \brief Construct a vector with fixed number of rows or a rowvector with fixed number of + * columns by passing an initializer list \cpp11 + * + * \only_for_vectors + * + * \warning To construct a vector or rowvector of fixed size, the number of values passed through + * the initializer list must match the the fixed number of rows in the vector case or + * the fixed number of columns in the rowvector case. */ + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list& list); + + /** + * \brief Constructs a Matrix or Array and initializes it by elements given by an initializer list of initializer + * lists \cpp11 */ + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list>& list); + #else // EIGEN_PARSED_BY_DOXYGEN + #if EIGEN_HAS_CXX11 + template + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list& list, + typename internal::enable_if::value, T>::type* = 0, + typename internal::enable_if::type* = 0) + : m_storage() + { + _check_template_params(); + EIGEN_STATIC_ASSERT_FIXED_SIZE(PlainObjectBase); + resize(list.size()); + std::copy(list.begin(), list.end(), m_storage.data()); + } + + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list>& list) + : m_storage() + { + _check_template_params(); + + size_t list_size = 0; + if (list.begin() != list.end()) { + list_size = list.begin()->size(); + } + + // This is to allow syntax like VectorXi {{1, 2, 3, 4}} + if (ColsAtCompileTime == 1 && list.size() == 1) { + eigen_assert(list_size == static_cast(RowsAtCompileTime) || RowsAtCompileTime == Dynamic); + resize(list_size, ColsAtCompileTime); + std::copy(list.begin()->begin(), list.begin()->end(), m_storage.data()); + } else { + eigen_assert(list.size() == static_cast(RowsAtCompileTime) || RowsAtCompileTime == Dynamic); + eigen_assert(list_size == static_cast(ColsAtCompileTime) || ColsAtCompileTime == Dynamic); + resize(list.size(), list_size); + + Index row_index = 0; + for (const std::initializer_list& row : list) { + eigen_assert(list_size == row.size()); + Index col_index = 0; + for (const Scalar& e : row) { + coeffRef(row_index, col_index) = e; + ++col_index; + } + ++row_index; + } + } + } + #endif // end EIGEN_HAS_CXX11 + #endif // end EIGEN_PARSED_BY_DOXYGEN + /** \sa PlainObjectBase::operator=(const EigenBase&) */ template EIGEN_DEVICE_FUNC 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_cxx11.cpp similarity index 100% rename from doc/snippets/Tutorial_std_sort_rows.cpp rename to doc/snippets/Tutorial_std_sort_rows_cxx11.cpp diff --git a/failtest/CMakeLists.txt b/failtest/CMakeLists.txt index f95503d7e..256e541e2 100644 --- a/failtest/CMakeLists.txt +++ b/failtest/CMakeLists.txt @@ -63,3 +63,8 @@ ei_add_failtest("bdcsvd_int") ei_add_failtest("eigensolver_int") ei_add_failtest("eigensolver_cplx") +if(EIGEN_TEST_CXX11) + ei_add_failtest("initializer_list_1") + ei_add_failtest("initializer_list_2") +endif() + diff --git a/failtest/initializer_list_1.cpp b/failtest/initializer_list_1.cpp new file mode 100644 index 000000000..92dfd1f65 --- /dev/null +++ b/failtest/initializer_list_1.cpp @@ -0,0 +1,14 @@ +#include "../Eigen/Core" + +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD +#define ROWS Dynamic +#else +#define ROWS 3 +#endif + +using namespace Eigen; + +int main() +{ + Matrix {1, 2, 3}; +} diff --git a/failtest/initializer_list_2.cpp b/failtest/initializer_list_2.cpp new file mode 100644 index 000000000..1996050a7 --- /dev/null +++ b/failtest/initializer_list_2.cpp @@ -0,0 +1,16 @@ +#include "../Eigen/Core" + +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD +#define ROWS Dynamic +#define COLS Dynamic +#else +#define ROWS 3 +#define COLS 1 +#endif + +using namespace Eigen; + +int main() +{ + Matrix {1, 2, 3}; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 794befa69..3dbb426eb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -287,6 +287,9 @@ ei_add_test(half_float) ei_add_test(array_of_string) ei_add_test(num_dimensions) ei_add_test(stl_iterators) +if(EIGEN_TEST_CXX11) + ei_add_test(initializer_list_construction) +endif() add_executable(bug1213 bug1213.cpp bug1213_main.cpp) diff --git a/test/initializer_list_construction.cpp b/test/initializer_list_construction.cpp new file mode 100644 index 000000000..5f281ea4e --- /dev/null +++ b/test/initializer_list_construction.cpp @@ -0,0 +1,371 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2019 David Tellenbach +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#define EIGEN_NO_STATIC_ASSERT + +#include "main.h" + +template::IsInteger> +struct TestMethodDispatching { + static void run() {} +}; + +template +struct TestMethodDispatching { + static void run() + { + { + Matrix m {3, 4}; + Array a {3, 4}; + VERIFY(m.rows() == 3); + VERIFY(m.cols() == 4); + VERIFY(a.rows() == 3); + VERIFY(a.cols() == 4); + } + { + Matrix m {3, 4}; + Array a {3, 4}; + VERIFY(m(0) == 3); + VERIFY(m(1) == 4); + VERIFY(a(0) == 3); + VERIFY(a(1) == 4); + } + { + Matrix m {3, 4}; + Array a {3, 4}; + VERIFY(m(0) == 3); + VERIFY(m(1) == 4); + VERIFY(a(0) == 3); + VERIFY(a(1) == 4); + } + } +}; + +template void singleInitializerListVectorConstruction() +{ + Scalar raw[4]; + for(int k = 0; k < 4; ++k) { + raw[k] = internal::random(); + } + { + Matrix m { raw[0], raw[1], raw[2], raw[3] }; + Array a { raw[0], raw[1], raw[2], raw[3] }; + for(int k = 0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k = 0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix(raw[0], raw[1], raw[2], raw[3]))); + VERIFY_IS_EQUAL(m, (Matrix({raw[0], raw[1], raw[2], raw[3]}))); + VERIFY((a == (Array(raw[0], raw[1], raw[2], raw[3]))).all()); + VERIFY((a == (Array({raw[0], raw[1], raw[2], raw[3]}))).all()); + } + { + Matrix m { raw[0], raw[1], raw[2], raw[3] }; + Array a { raw[0], raw[1], raw[2], raw[3] }; + for(int k = 0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k = 0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix(raw[0], raw[1], raw[2], raw[3]))); + VERIFY_IS_EQUAL(m, (Matrix({raw[0], raw[1], raw[2], raw[3]}))); + VERIFY((a == (Array(raw[0], raw[1], raw[2], raw[3]))).all()); + VERIFY((a == (Array({raw[0], raw[1], raw[2], raw[3]}))).all()); + } +} + + +template void initializerListVectorConstruction() +{ + Scalar raw[4]; + for(int k = 0; k < 4; ++k) { + raw[k] = internal::random(); + } + { + Matrix m { {raw[0]}, {raw[1]},{raw[2]},{raw[3]} }; + Array a { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }; + for(int k = 0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k = 0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))); + VERIFY((a == (Array({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))).all()); + } + { + Matrix m { {raw[0], raw[1], raw[2], raw[3]} }; + Array a { {raw[0], raw[1], raw[2], raw[3]} }; + for(int k = 0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k = 0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix({{raw[0],raw[1],raw[2],raw[3]}}))); + VERIFY((a == (Array({{raw[0],raw[1],raw[2],raw[3]}}))).all()); + } + { + Matrix m { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }; + Array a { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }; + for(int k=0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k=0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))); + VERIFY((a == (Array({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))).all()); + } + { + Matrix m {{raw[0],raw[1],raw[2],raw[3]}}; + Array a {{raw[0],raw[1],raw[2],raw[3]}}; + for(int k=0; k < 4; ++k) { + VERIFY(m(k) == raw[k]); + } + for(int k=0; k < 4; ++k) { + VERIFY(a(k) == raw[k]); + } + VERIFY_IS_EQUAL(m, (Matrix({{raw[0],raw[1],raw[2],raw[3]}}))); + VERIFY((a == (Array({{raw[0],raw[1],raw[2],raw[3]}}))).all()); + } +} + +template void initializerListMatrixConstruction() +{ + const Index RowsAtCompileTime = 5; + const Index ColsAtCompileTime = 4; + const Index SizeAtCompileTime = RowsAtCompileTime * ColsAtCompileTime; + + Scalar raw[SizeAtCompileTime]; + for (int i = 0; i < SizeAtCompileTime; ++i) { + raw[i] = internal::random(); + } + { + Matrix m {}; + VERIFY(m.cols() == 0); + VERIFY(m.rows() == 0); + VERIFY_IS_EQUAL(m, (Matrix())); + } + { + Matrix m { + {raw[0], raw[1], raw[2], raw[3]}, + {raw[4], raw[5], raw[6], raw[7]}, + {raw[8], raw[9], raw[10], raw[11]}, + {raw[12], raw[13], raw[14], raw[15]}, + {raw[16], raw[17], raw[18], raw[19]} + }; + + Matrix m2; + m2 << raw[0], raw[1], raw[2], raw[3], + raw[4], raw[5], raw[6], raw[7], + raw[8], raw[9], raw[10], raw[11], + raw[12], raw[13], raw[14], raw[15], + raw[16], raw[17], raw[18], raw[19]; + + int k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + VERIFY(m(i, j) == raw[k]); + ++k; + } + } + VERIFY_IS_EQUAL(m, m2); + } + { + Matrix m{ + {raw[0], raw[1], raw[2], raw[3]}, + {raw[4], raw[5], raw[6], raw[7]}, + {raw[8], raw[9], raw[10], raw[11]}, + {raw[12], raw[13], raw[14], raw[15]}, + {raw[16], raw[17], raw[18], raw[19]} + }; + + VERIFY(m.cols() == 4); + VERIFY(m.rows() == 5); + int k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + VERIFY(m(i, j) == raw[k]); + ++k; + } + } + + Matrix m2(RowsAtCompileTime, ColsAtCompileTime); + k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + m2(i, j) = raw[k]; + ++k; + } + } + VERIFY_IS_EQUAL(m, m2); + } +} + +template void initializerListArrayConstruction() +{ + const Index RowsAtCompileTime = 5; + const Index ColsAtCompileTime = 4; + const Index SizeAtCompileTime = RowsAtCompileTime * ColsAtCompileTime; + + Scalar raw[SizeAtCompileTime]; + for (int i = 0; i < SizeAtCompileTime; ++i) { + raw[i] = internal::random(); + } + { + Array a {}; + VERIFY(a.cols() == 0); + VERIFY(a.rows() == 0); + } + { + Array m { + {raw[0], raw[1], raw[2], raw[3]}, + {raw[4], raw[5], raw[6], raw[7]}, + {raw[8], raw[9], raw[10], raw[11]}, + {raw[12], raw[13], raw[14], raw[15]}, + {raw[16], raw[17], raw[18], raw[19]} + }; + + Array m2; + m2 << raw[0], raw[1], raw[2], raw[3], + raw[4], raw[5], raw[6], raw[7], + raw[8], raw[9], raw[10], raw[11], + raw[12], raw[13], raw[14], raw[15], + raw[16], raw[17], raw[18], raw[19]; + + int k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + VERIFY(m(i, j) == raw[k]); + ++k; + } + } + VERIFY_IS_APPROX(m, m2); + } + { + Array m { + {raw[0], raw[1], raw[2], raw[3]}, + {raw[4], raw[5], raw[6], raw[7]}, + {raw[8], raw[9], raw[10], raw[11]}, + {raw[12], raw[13], raw[14], raw[15]}, + {raw[16], raw[17], raw[18], raw[19]} + }; + + VERIFY(m.cols() == 4); + VERIFY(m.rows() == 5); + int k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + VERIFY(m(i, j) == raw[k]); + ++k; + } + } + + Array m2(RowsAtCompileTime, ColsAtCompileTime); + k = 0; + for(int i = 0; i < RowsAtCompileTime; ++i) { + for (int j = 0; j < ColsAtCompileTime; ++j) { + m2(i, j) = raw[k]; + ++k; + } + } + VERIFY_IS_APPROX(m, m2); + } +} + +template void dynamicVectorConstruction() +{ + const Index size = 4; + Scalar raw[size]; + for (int i = 0; i < size; ++i) { + raw[i] = internal::random(); + } + + typedef Matrix VectorX; + + { + VectorX v {{raw[0], raw[1], raw[2], raw[3]}}; + for (int i = 0; i < size; ++i) { + VERIFY(v(i) == raw[i]); + } + VERIFY(v.rows() == size); + VERIFY(v.cols() == 1); + VERIFY_IS_EQUAL(v, (VectorX {{raw[0], raw[1], raw[2], raw[3]}})); + } + + { + VERIFY_RAISES_ASSERT((VectorX {raw[0], raw[1], raw[2], raw[3]})); + } + { + VERIFY_RAISES_ASSERT((VectorX { + {raw[0], raw[1], raw[2], raw[3]}, + {raw[0], raw[1], raw[2], raw[3]}, + })); + } +} + +EIGEN_DECLARE_TEST(initializer_list_construction) +{ + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction()); + CALL_SUBTEST_1(initializerListVectorConstruction>()); + CALL_SUBTEST_1(initializerListVectorConstruction>()); + CALL_SUBTEST_1(initializerListVectorConstruction>()); + + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction()); + CALL_SUBTEST_2(initializerListMatrixConstruction>()); + CALL_SUBTEST_2(initializerListMatrixConstruction>()); + CALL_SUBTEST_2(initializerListMatrixConstruction>()); + + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction()); + CALL_SUBTEST_3(initializerListArrayConstruction>()); + CALL_SUBTEST_3(initializerListArrayConstruction>()); + CALL_SUBTEST_3(initializerListArrayConstruction>()); + + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction>()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction>()); + CALL_SUBTEST_4(singleInitializerListVectorConstruction>()); + + CALL_SUBTEST_5(TestMethodDispatching::run()); + CALL_SUBTEST_5(TestMethodDispatching::run()); + + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction()); + CALL_SUBTEST_6(dynamicVectorConstruction>()); + CALL_SUBTEST_6(dynamicVectorConstruction>()); + CALL_SUBTEST_6(dynamicVectorConstruction>()); +} \ No newline at end of file