Matrix.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_MATRIX_H
  11. #define EIGEN_MATRIX_H
  12. namespace Eigen {
  13. namespace internal {
  14. template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
  15. struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
  16. {
  17. private:
  18. enum { size = internal::size_at_compile_time<_Rows,_Cols>::ret };
  19. typedef typename find_best_packet<_Scalar,size>::type PacketScalar;
  20. enum {
  21. row_major_bit = _Options&RowMajor ? RowMajorBit : 0,
  22. is_dynamic_size_storage = _MaxRows==Dynamic || _MaxCols==Dynamic,
  23. max_size = is_dynamic_size_storage ? Dynamic : _MaxRows*_MaxCols,
  24. default_alignment = compute_default_alignment<_Scalar,max_size>::value,
  25. actual_alignment = ((_Options&DontAlign)==0) ? default_alignment : 0,
  26. required_alignment = unpacket_traits<PacketScalar>::alignment,
  27. packet_access_bit = (packet_traits<_Scalar>::Vectorizable && (EIGEN_UNALIGNED_VECTORIZE || (actual_alignment>=required_alignment))) ? PacketAccessBit : 0
  28. };
  29. public:
  30. typedef _Scalar Scalar;
  31. typedef Dense StorageKind;
  32. typedef Eigen::Index StorageIndex;
  33. typedef MatrixXpr XprKind;
  34. enum {
  35. RowsAtCompileTime = _Rows,
  36. ColsAtCompileTime = _Cols,
  37. MaxRowsAtCompileTime = _MaxRows,
  38. MaxColsAtCompileTime = _MaxCols,
  39. Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
  40. Options = _Options,
  41. InnerStrideAtCompileTime = 1,
  42. OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime,
  43. // FIXME, the following flag in only used to define NeedsToAlign in PlainObjectBase
  44. EvaluatorFlags = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit,
  45. Alignment = actual_alignment
  46. };
  47. };
  48. }
  49. /** \class Matrix
  50. * \ingroup Core_Module
  51. *
  52. * \brief The matrix class, also used for vectors and row-vectors
  53. *
  54. * The %Matrix class is the work-horse for all \em dense (\ref dense "note") matrices and vectors within Eigen.
  55. * Vectors are matrices with one column, and row-vectors are matrices with one row.
  56. *
  57. * The %Matrix class encompasses \em both fixed-size and dynamic-size objects (\ref fixedsize "note").
  58. *
  59. * The first three template parameters are required:
  60. * \tparam _Scalar Numeric type, e.g. float, double, int or std::complex<float>.
  61. * User defined scalar types are supported as well (see \ref user_defined_scalars "here").
  62. * \tparam _Rows Number of rows, or \b Dynamic
  63. * \tparam _Cols Number of columns, or \b Dynamic
  64. *
  65. * The remaining template parameters are optional -- in most cases you don't have to worry about them.
  66. * \tparam _Options A combination of either \b #RowMajor or \b #ColMajor, and of either
  67. * \b #AutoAlign or \b #DontAlign.
  68. * The former controls \ref TopicStorageOrders "storage order", and defaults to column-major. The latter controls alignment, which is required
  69. * for vectorization. It defaults to aligning matrices except for fixed sizes that aren't a multiple of the packet size.
  70. * \tparam _MaxRows Maximum number of rows. Defaults to \a _Rows (\ref maxrows "note").
  71. * \tparam _MaxCols Maximum number of columns. Defaults to \a _Cols (\ref maxrows "note").
  72. *
  73. * Eigen provides a number of typedefs covering the usual cases. Here are some examples:
  74. *
  75. * \li \c Matrix2d is a 2x2 square matrix of doubles (\c Matrix<double, 2, 2>)
  76. * \li \c Vector4f is a vector of 4 floats (\c Matrix<float, 4, 1>)
  77. * \li \c RowVector3i is a row-vector of 3 ints (\c Matrix<int, 1, 3>)
  78. *
  79. * \li \c MatrixXf is a dynamic-size matrix of floats (\c Matrix<float, Dynamic, Dynamic>)
  80. * \li \c VectorXf is a dynamic-size vector of floats (\c Matrix<float, Dynamic, 1>)
  81. *
  82. * \li \c Matrix2Xf is a partially fixed-size (dynamic-size) matrix of floats (\c Matrix<float, 2, Dynamic>)
  83. * \li \c MatrixX3d is a partially dynamic-size (fixed-size) matrix of double (\c Matrix<double, Dynamic, 3>)
  84. *
  85. * See \link matrixtypedefs this page \endlink for a complete list of predefined \em %Matrix and \em Vector typedefs.
  86. *
  87. * You can access elements of vectors and matrices using normal subscripting:
  88. *
  89. * \code
  90. * Eigen::VectorXd v(10);
  91. * v[0] = 0.1;
  92. * v[1] = 0.2;
  93. * v(0) = 0.3;
  94. * v(1) = 0.4;
  95. *
  96. * Eigen::MatrixXi m(10, 10);
  97. * m(0, 1) = 1;
  98. * m(0, 2) = 2;
  99. * m(0, 3) = 3;
  100. * \endcode
  101. *
  102. * This class can be extended with the help of the plugin mechanism described on the page
  103. * \ref TopicCustomizing_Plugins by defining the preprocessor symbol \c EIGEN_MATRIX_PLUGIN.
  104. *
  105. * <i><b>Some notes:</b></i>
  106. *
  107. * <dl>
  108. * <dt><b>\anchor dense Dense versus sparse:</b></dt>
  109. * <dd>This %Matrix class handles dense, not sparse matrices and vectors. For sparse matrices and vectors, see the Sparse module.
  110. *
  111. * Dense matrices and vectors are plain usual arrays of coefficients. All the coefficients are stored, in an ordinary contiguous array.
  112. * This is unlike Sparse matrices and vectors where the coefficients are stored as a list of nonzero coefficients.</dd>
  113. *
  114. * <dt><b>\anchor fixedsize Fixed-size versus dynamic-size:</b></dt>
  115. * <dd>Fixed-size means that the numbers of rows and columns are known are compile-time. In this case, Eigen allocates the array
  116. * of coefficients as a fixed-size array, as a class member. This makes sense for very small matrices, typically up to 4x4, sometimes up
  117. * to 16x16. Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time.
  118. *
  119. * Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. In this case they are runtime
  120. * variables, and the array of coefficients is allocated dynamically on the heap.
  121. *
  122. * Note that \em dense matrices, be they Fixed-size or Dynamic-size, <em>do not</em> expand dynamically in the sense of a std::map.
  123. * If you want this behavior, see the Sparse module.</dd>
  124. *
  125. * <dt><b>\anchor maxrows _MaxRows and _MaxCols:</b></dt>
  126. * <dd>In most cases, one just leaves these parameters to the default values.
  127. * These parameters mean the maximum size of rows and columns that the matrix may have. They are useful in cases
  128. * when the exact numbers of rows and columns are not known are compile-time, but it is known at compile-time that they cannot
  129. * exceed a certain value. This happens when taking dynamic-size blocks inside fixed-size matrices: in this case _MaxRows and _MaxCols
  130. * are the dimensions of the original matrix, while _Rows and _Cols are Dynamic.</dd>
  131. * </dl>
  132. *
  133. * <i><b>ABI and storage layout</b></i>
  134. *
  135. * The table below summarizes the ABI of some possible Matrix instances which is fixed thorough the lifetime of Eigen 3.
  136. * <table class="manual">
  137. * <tr><th>Matrix type</th><th>Equivalent C structure</th></tr>
  138. * <tr><td>\code Matrix<T,Dynamic,Dynamic> \endcode</td><td>\code
  139. * struct {
  140. * T *data; // with (size_t(data)%EIGEN_MAX_ALIGN_BYTES)==0
  141. * Eigen::Index rows, cols;
  142. * };
  143. * \endcode</td></tr>
  144. * <tr class="alt"><td>\code
  145. * Matrix<T,Dynamic,1>
  146. * Matrix<T,1,Dynamic> \endcode</td><td>\code
  147. * struct {
  148. * T *data; // with (size_t(data)%EIGEN_MAX_ALIGN_BYTES)==0
  149. * Eigen::Index size;
  150. * };
  151. * \endcode</td></tr>
  152. * <tr><td>\code Matrix<T,Rows,Cols> \endcode</td><td>\code
  153. * struct {
  154. * T data[Rows*Cols]; // with (size_t(data)%A(Rows*Cols*sizeof(T)))==0
  155. * };
  156. * \endcode</td></tr>
  157. * <tr class="alt"><td>\code Matrix<T,Dynamic,Dynamic,0,MaxRows,MaxCols> \endcode</td><td>\code
  158. * struct {
  159. * T data[MaxRows*MaxCols]; // with (size_t(data)%A(MaxRows*MaxCols*sizeof(T)))==0
  160. * Eigen::Index rows, cols;
  161. * };
  162. * \endcode</td></tr>
  163. * </table>
  164. * Note that in this table Rows, Cols, MaxRows and MaxCols are all positive integers. A(S) is defined to the largest possible power-of-two
  165. * smaller to EIGEN_MAX_STATIC_ALIGN_BYTES.
  166. *
  167. * \see MatrixBase for the majority of the API methods for matrices, \ref TopicClassHierarchy,
  168. * \ref TopicStorageOrders
  169. */
  170. template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
  171. class Matrix
  172. : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
  173. {
  174. public:
  175. /** \brief Base class typedef.
  176. * \sa PlainObjectBase
  177. */
  178. typedef PlainObjectBase<Matrix> Base;
  179. enum { Options = _Options };
  180. EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
  181. typedef typename Base::PlainObject PlainObject;
  182. using Base::base;
  183. using Base::coeffRef;
  184. /**
  185. * \brief Assigns matrices to each other.
  186. *
  187. * \note This is a special case of the templated operator=. Its purpose is
  188. * to prevent a default operator= from hiding the templated operator=.
  189. *
  190. * \callgraph
  191. */
  192. EIGEN_DEVICE_FUNC
  193. EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
  194. {
  195. return Base::_set(other);
  196. }
  197. /** \internal
  198. * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
  199. *
  200. * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
  201. * it will be initialized.
  202. *
  203. * Note that copying a row-vector into a vector (and conversely) is allowed.
  204. * The resizing, if any, is then done in the appropriate way so that row-vectors
  205. * remain row-vectors and vectors remain vectors.
  206. */
  207. template<typename OtherDerived>
  208. EIGEN_DEVICE_FUNC
  209. EIGEN_STRONG_INLINE Matrix& operator=(const DenseBase<OtherDerived>& other)
  210. {
  211. return Base::_set(other);
  212. }
  213. /* Here, doxygen failed to copy the brief information when using \copydoc */
  214. /**
  215. * \brief Copies the generic expression \a other into *this.
  216. * \copydetails DenseBase::operator=(const EigenBase<OtherDerived> &other)
  217. */
  218. template<typename OtherDerived>
  219. EIGEN_DEVICE_FUNC
  220. EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
  221. {
  222. return Base::operator=(other);
  223. }
  224. template<typename OtherDerived>
  225. EIGEN_DEVICE_FUNC
  226. EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
  227. {
  228. return Base::operator=(func);
  229. }
  230. /** \brief Default constructor.
  231. *
  232. * For fixed-size matrices, does nothing.
  233. *
  234. * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
  235. * is called a null matrix. This constructor is the unique way to create null matrices: resizing
  236. * a matrix to 0 is not supported.
  237. *
  238. * \sa resize(Index,Index)
  239. */
  240. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  241. Matrix() : Base()
  242. {
  243. Base::_check_template_params();
  244. EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
  245. }
  246. // FIXME is it still needed
  247. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  248. explicit Matrix(internal::constructor_without_unaligned_array_assert)
  249. : Base(internal::constructor_without_unaligned_array_assert())
  250. { Base::_check_template_params(); EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
  251. #if EIGEN_HAS_RVALUE_REFERENCES
  252. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  253. Matrix(Matrix&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
  254. : Base(std::move(other))
  255. {
  256. Base::_check_template_params();
  257. }
  258. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  259. Matrix& operator=(Matrix&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value)
  260. {
  261. Base::operator=(std::move(other));
  262. return *this;
  263. }
  264. #endif
  265. #if EIGEN_HAS_CXX11
  266. /** \copydoc PlainObjectBase(const Scalar&, const Scalar&, const Scalar&, const Scalar&, const ArgTypes&... args)
  267. *
  268. * Example: \include Matrix_variadic_ctor_cxx11.cpp
  269. * Output: \verbinclude Matrix_variadic_ctor_cxx11.out
  270. *
  271. * \sa Matrix(const std::initializer_list<std::initializer_list<Scalar>>&)
  272. */
  273. template <typename... ArgTypes>
  274. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  275. Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
  276. : Base(a0, a1, a2, a3, args...) {}
  277. /** \brief Constructs a Matrix and initializes it from the coefficients given as initializer-lists grouped by row. \cpp11
  278. *
  279. * In the general case, the constructor takes a list of rows, each row being represented as a list of coefficients:
  280. *
  281. * Example: \include Matrix_initializer_list_23_cxx11.cpp
  282. * Output: \verbinclude Matrix_initializer_list_23_cxx11.out
  283. *
  284. * Each of the inner initializer lists must contain the exact same number of elements, otherwise an assertion is triggered.
  285. *
  286. * In the case of a compile-time column vector, implicit transposition from a single row is allowed.
  287. * Therefore <code>VectorXd{{1,2,3,4,5}}</code> is legal and the more verbose syntax
  288. * <code>RowVectorXd{{1},{2},{3},{4},{5}}</code> can be avoided:
  289. *
  290. * Example: \include Matrix_initializer_list_vector_cxx11.cpp
  291. * Output: \verbinclude Matrix_initializer_list_vector_cxx11.out
  292. *
  293. * In the case of fixed-sized matrices, the initializer list sizes must exactly match the matrix sizes,
  294. * and implicit transposition is allowed for compile-time vectors only.
  295. *
  296. * \sa Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
  297. */
  298. EIGEN_DEVICE_FUNC
  299. explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<std::initializer_list<Scalar>>& list) : Base(list) {}
  300. #endif // end EIGEN_HAS_CXX11
  301. #ifndef EIGEN_PARSED_BY_DOXYGEN
  302. // This constructor is for both 1x1 matrices and dynamic vectors
  303. template<typename T>
  304. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  305. explicit Matrix(const T& x)
  306. {
  307. Base::_check_template_params();
  308. Base::template _init1<T>(x);
  309. }
  310. template<typename T0, typename T1>
  311. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  312. Matrix(const T0& x, const T1& y)
  313. {
  314. Base::_check_template_params();
  315. Base::template _init2<T0,T1>(x, y);
  316. }
  317. #else
  318. /** \brief Constructs a fixed-sized matrix initialized with coefficients starting at \a data */
  319. EIGEN_DEVICE_FUNC
  320. explicit Matrix(const Scalar *data);
  321. /** \brief Constructs a vector or row-vector with given dimension. \only_for_vectors
  322. *
  323. * This is useful for dynamic-size vectors. For fixed-size vectors,
  324. * it is redundant to pass these parameters, so one should use the default constructor
  325. * Matrix() instead.
  326. *
  327. * \warning This constructor is disabled for fixed-size \c 1x1 matrices. For instance,
  328. * calling Matrix<double,1,1>(1) will call the initialization constructor: Matrix(const Scalar&).
  329. * For fixed-size \c 1x1 matrices it is therefore recommended to use the default
  330. * constructor Matrix() instead, especially when using one of the non standard
  331. * \c EIGEN_INITIALIZE_MATRICES_BY_{ZERO,\c NAN} macros (see \ref TopicPreprocessorDirectives).
  332. */
  333. EIGEN_STRONG_INLINE explicit Matrix(Index dim);
  334. /** \brief Constructs an initialized 1x1 matrix with the given coefficient
  335. * \sa Matrix(const Scalar&, const Scalar&, const Scalar&, const Scalar&, const ArgTypes&...) */
  336. Matrix(const Scalar& x);
  337. /** \brief Constructs an uninitialized matrix with \a rows rows and \a cols columns.
  338. *
  339. * This is useful for dynamic-size matrices. For fixed-size matrices,
  340. * it is redundant to pass these parameters, so one should use the default constructor
  341. * Matrix() instead.
  342. *
  343. * \warning This constructor is disabled for fixed-size \c 1x2 and \c 2x1 vectors. For instance,
  344. * calling Matrix2f(2,1) will call the initialization constructor: Matrix(const Scalar& x, const Scalar& y).
  345. * For fixed-size \c 1x2 or \c 2x1 vectors it is therefore recommended to use the default
  346. * constructor Matrix() instead, especially when using one of the non standard
  347. * \c EIGEN_INITIALIZE_MATRICES_BY_{ZERO,\c NAN} macros (see \ref TopicPreprocessorDirectives).
  348. */
  349. EIGEN_DEVICE_FUNC
  350. Matrix(Index rows, Index cols);
  351. /** \brief Constructs an initialized 2D vector with given coefficients
  352. * \sa Matrix(const Scalar&, const Scalar&, const Scalar&, const Scalar&, const ArgTypes&...) */
  353. Matrix(const Scalar& x, const Scalar& y);
  354. #endif // end EIGEN_PARSED_BY_DOXYGEN
  355. /** \brief Constructs an initialized 3D vector with given coefficients
  356. * \sa Matrix(const Scalar&, const Scalar&, const Scalar&, const Scalar&, const ArgTypes&...)
  357. */
  358. EIGEN_DEVICE_FUNC
  359. EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
  360. {
  361. Base::_check_template_params();
  362. EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
  363. m_storage.data()[0] = x;
  364. m_storage.data()[1] = y;
  365. m_storage.data()[2] = z;
  366. }
  367. /** \brief Constructs an initialized 4D vector with given coefficients
  368. * \sa Matrix(const Scalar&, const Scalar&, const Scalar&, const Scalar&, const ArgTypes&...)
  369. */
  370. EIGEN_DEVICE_FUNC
  371. EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
  372. {
  373. Base::_check_template_params();
  374. EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
  375. m_storage.data()[0] = x;
  376. m_storage.data()[1] = y;
  377. m_storage.data()[2] = z;
  378. m_storage.data()[3] = w;
  379. }
  380. /** \brief Copy constructor */
  381. EIGEN_DEVICE_FUNC
  382. EIGEN_STRONG_INLINE Matrix(const Matrix& other) : Base(other)
  383. { }
  384. /** \brief Copy constructor for generic expressions.
  385. * \sa MatrixBase::operator=(const EigenBase<OtherDerived>&)
  386. */
  387. template<typename OtherDerived>
  388. EIGEN_DEVICE_FUNC
  389. EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
  390. : Base(other.derived())
  391. { }
  392. EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
  393. inline Index innerStride() const EIGEN_NOEXCEPT { return 1; }
  394. EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
  395. inline Index outerStride() const EIGEN_NOEXCEPT { return this->innerSize(); }
  396. /////////// Geometry module ///////////
  397. template<typename OtherDerived>
  398. EIGEN_DEVICE_FUNC
  399. explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
  400. template<typename OtherDerived>
  401. EIGEN_DEVICE_FUNC
  402. Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
  403. // allow to extend Matrix outside Eigen
  404. #ifdef EIGEN_MATRIX_PLUGIN
  405. #include EIGEN_MATRIX_PLUGIN
  406. #endif
  407. protected:
  408. template <typename Derived, typename OtherDerived, bool IsVector>
  409. friend struct internal::conservative_resize_like_impl;
  410. using Base::m_storage;
  411. };
  412. /** \defgroup matrixtypedefs Global matrix typedefs
  413. *
  414. * \ingroup Core_Module
  415. *
  416. * %Eigen defines several typedef shortcuts for most common matrix and vector types.
  417. *
  418. * The general patterns are the following:
  419. *
  420. * \c MatrixSizeType where \c Size can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
  421. * and where \c Type can be \c i for integer, \c f for float, \c d for double, \c cf for complex float, \c cd
  422. * for complex double.
  423. *
  424. * For example, \c Matrix3d is a fixed-size 3x3 matrix type of doubles, and \c MatrixXf is a dynamic-size matrix of floats.
  425. *
  426. * There are also \c VectorSizeType and \c RowVectorSizeType which are self-explanatory. For example, \c Vector4cf is
  427. * a fixed-size vector of 4 complex floats.
  428. *
  429. * With \cpp11, template alias are also defined for common sizes.
  430. * They follow the same pattern as above except that the scalar type suffix is replaced by a
  431. * template parameter, i.e.:
  432. * - `MatrixSize<Type>` where `Size` can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size.
  433. * - `MatrixXSize<Type>` and `MatrixSizeX<Type>` where `Size` can be \c 2,\c 3,\c 4 for hybrid dynamic/fixed matrices.
  434. * - `VectorSize<Type>` and `RowVectorSize<Type>` for column and row vectors.
  435. *
  436. * With \cpp11, you can also use fully generic column and row vector types: `Vector<Type,Size>` and `RowVector<Type,Size>`.
  437. *
  438. * \sa class Matrix
  439. */
  440. #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
  441. /** \ingroup matrixtypedefs */ \
  442. typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
  443. /** \ingroup matrixtypedefs */ \
  444. typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
  445. /** \ingroup matrixtypedefs */ \
  446. typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
  447. #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
  448. /** \ingroup matrixtypedefs */ \
  449. typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
  450. /** \ingroup matrixtypedefs */ \
  451. typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
  452. #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
  453. EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
  454. EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
  455. EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
  456. EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
  457. EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
  458. EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
  459. EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
  460. EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
  461. EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
  462. EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
  463. EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
  464. EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
  465. #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
  466. #undef EIGEN_MAKE_TYPEDEFS
  467. #undef EIGEN_MAKE_FIXED_TYPEDEFS
  468. #if EIGEN_HAS_CXX11
  469. #define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix) \
  470. /** \ingroup matrixtypedefs */ \
  471. /** \brief \cpp11 */ \
  472. template <typename Type> \
  473. using Matrix##SizeSuffix = Matrix<Type, Size, Size>; \
  474. /** \ingroup matrixtypedefs */ \
  475. /** \brief \cpp11 */ \
  476. template <typename Type> \
  477. using Vector##SizeSuffix = Matrix<Type, Size, 1>; \
  478. /** \ingroup matrixtypedefs */ \
  479. /** \brief \cpp11 */ \
  480. template <typename Type> \
  481. using RowVector##SizeSuffix = Matrix<Type, 1, Size>;
  482. #define EIGEN_MAKE_FIXED_TYPEDEFS(Size) \
  483. /** \ingroup matrixtypedefs */ \
  484. /** \brief \cpp11 */ \
  485. template <typename Type> \
  486. using Matrix##Size##X = Matrix<Type, Size, Dynamic>; \
  487. /** \ingroup matrixtypedefs */ \
  488. /** \brief \cpp11 */ \
  489. template <typename Type> \
  490. using Matrix##X##Size = Matrix<Type, Dynamic, Size>;
  491. EIGEN_MAKE_TYPEDEFS(2, 2)
  492. EIGEN_MAKE_TYPEDEFS(3, 3)
  493. EIGEN_MAKE_TYPEDEFS(4, 4)
  494. EIGEN_MAKE_TYPEDEFS(Dynamic, X)
  495. EIGEN_MAKE_FIXED_TYPEDEFS(2)
  496. EIGEN_MAKE_FIXED_TYPEDEFS(3)
  497. EIGEN_MAKE_FIXED_TYPEDEFS(4)
  498. /** \ingroup matrixtypedefs
  499. * \brief \cpp11 */
  500. template <typename Type, int Size>
  501. using Vector = Matrix<Type, Size, 1>;
  502. /** \ingroup matrixtypedefs
  503. * \brief \cpp11 */
  504. template <typename Type, int Size>
  505. using RowVector = Matrix<Type, 1, Size>;
  506. #undef EIGEN_MAKE_TYPEDEFS
  507. #undef EIGEN_MAKE_FIXED_TYPEDEFS
  508. #endif // EIGEN_HAS_CXX11
  509. } // end namespace Eigen
  510. #endif // EIGEN_MATRIX_H