diff options
author | David Tellenbach <david.tellenbach@tellnotes.org> | 2019-01-21 16:25:57 +0100 |
---|---|---|
committer | David Tellenbach <david.tellenbach@tellnotes.org> | 2019-01-21 16:25:57 +0100 |
commit | db152b9ee6effd3799f70a621f495c427cb3c33f (patch) | |
tree | 25a9c96f0cdfbed28072ea7e2f8602412ce6de7f /Eigen/src/Core/Matrix.h | |
parent | 543529da6a1eabf415f4f8b56495fad76b57ba22 (diff) |
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
Diffstat (limited to 'Eigen/src/Core/Matrix.h')
-rw-r--r-- | Eigen/src/Core/Matrix.h | 50 |
1 files changed, 49 insertions, 1 deletions
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<T0,T1>(x, y); } + + #if EIGEN_HAS_CXX11 + template<typename T> + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<T>& list, + typename internal::enable_if<internal::is_same<T, Scalar>::value, T>::type* = 0, + typename internal::enable_if<RowsAtCompileTime != Dynamic + && ColsAtCompileTime != Dynamic + && IsVectorAtCompileTime == 1, T>::type* = 0) : Base(list) {} + + EIGEN_DEVICE_FUNC + explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<std::initializer_list<Scalar>>& 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<Scalar>& 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<Scalar>& 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<std::initializer_list<Scalar>>& list); + + #endif // end EIGEN_PARSED_BY_DOXYGEN /** \brief Constructs an initialized 3D vector with given coefficients */ EIGEN_DEVICE_FUNC |