You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
13 KiB

  1. namespace Eigen {
  2. /** \page TutorialMatrixClass Tutorial page 1 - The %Matrix class
  3. \ingroup Tutorial
  4. \li \b Previous: \ref GettingStarted
  5. \li \b Next: \ref TutorialMatrixArithmetic
  6. We assume that you have already read the quick \link GettingStarted "getting started" \endlink tutorial.
  7. This page is the first one in a much longer multi-page tutorial.
  8. \b Table \b of \b contents
  9. - \ref TutorialMatrixFirst3Params
  10. - \ref TutorialMatrixVectors
  11. - \ref TutorialMatrixDynamic
  12. - \ref TutorialMatrixConstructors
  13. - \ref TutorialMatrixCoeffAccessors
  14. - \ref TutorialMatrixCommaInitializer
  15. - \ref TutorialMatrixSizesResizing
  16. - \ref TutorialMatrixAssignment
  17. - \ref TutorialMatrixFixedVsDynamic
  18. - \ref TutorialMatrixOptTemplParams
  19. - \ref TutorialMatrixTypedefs
  20. In Eigen, all matrices and vectors are objects of the Matrix template class.
  21. Vectors are just a special case of matrices, with either 1 row or 1 column.
  22. \section TutorialMatrixFirst3Params The first three template parameters of Matrix
  23. The Matrix class takes six template parameters, but for now it's enough to
  24. learn about the first three first parameters. The three remaining parameters have default
  25. values, which for now we will leave untouched, and which we
  26. \ref TutorialMatrixOptTemplParams "discuss below".
  27. The three mandatory template parameters of Matrix are:
  28. \code
  29. Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
  30. \endcode
  31. \li \c Scalar is the scalar type, i.e. the type of the coefficients.
  32. That is, if you want a matrix of floats, choose \c float here.
  33. See \ref TopicScalarTypes "Scalar types" for a list of all supported
  34. scalar types and for how to extend support to new types.
  35. \li \c RowsAtCompileTime and \c ColsAtCompileTime are the number of rows
  36. and columns of the matrix as known at compile time (see
  37. \ref TutorialMatrixDynamic "below" for what to do if the number is not
  38. known at compile time).
  39. We offer a lot of convenience typedefs to cover the usual cases. For example, \c Matrix4f is
  40. a 4x4 matrix of floats. Here is how it is defined by Eigen:
  41. \code
  42. typedef Matrix<float, 4, 4> Matrix4f;
  43. \endcode
  44. We discuss \ref TutorialMatrixTypedefs "below" these convenience typedefs.
  45. \section TutorialMatrixVectors Vectors
  46. As mentioned above, in Eigen, vectors are just a special case of
  47. matrices, with either 1 row or 1 column. The case where they have 1 column is the most common;
  48. such vectors are called column-vectors, often abbreviated as just vectors. In the other case
  49. where they have 1 row, they are called row-vectors.
  50. For example, the convenience typedef \c Vector3f is a (column) vector of 3 floats. It is defined as follows by Eigen:
  51. \code
  52. typedef Matrix<float, 3, 1> Vector3f;
  53. \endcode
  54. We also offer convenience typedefs for row-vectors, for example:
  55. \code
  56. typedef Matrix<int, 1, 2> RowVector2i;
  57. \endcode
  58. \section TutorialMatrixDynamic The special value Dynamic
  59. Of course, Eigen is not limited to matrices whose dimensions are known at compile time.
  60. The \c RowsAtCompileTime and \c ColsAtCompileTime template parameters can take the special
  61. value \c Dynamic which indicates that the size is unknown at compile time, so must
  62. be handled as a run-time variable. In Eigen terminology, such a size is referred to as a
  63. \em dynamic \em size; while a size that is known at compile time is called a
  64. \em fixed \em size. For example, the convenience typedef \c MatrixXd, meaning
  65. a matrix of doubles with dynamic size, is defined as follows:
  66. \code
  67. typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
  68. \endcode
  69. And similarly, we define a self-explanatory typedef \c VectorXi as follows:
  70. \code
  71. typedef Matrix<int, Dynamic, 1> VectorXi;
  72. \endcode
  73. You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
  74. \code
  75. Matrix<float, 3, Dynamic>
  76. \endcode
  77. \section TutorialMatrixConstructors Constructors
  78. A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients. You can do:
  79. \code
  80. Matrix3f a;
  81. MatrixXf b;
  82. \endcode
  83. Here,
  84. \li \c a is a 3x3 matrix, with a static float[9] array of uninitialized coefficients,
  85. \li \c b is a dynamic-size matrix whose size is currently 0x0, and whose array of
  86. coefficients hasn't yet been allocated at all.
  87. Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
  88. For vectors, just pass the vector size. They allocate the array of coefficients
  89. with the given size, but don't initialize the coefficients themselves:
  90. \code
  91. MatrixXf a(10,15);
  92. VectorXf b(30);
  93. \endcode
  94. Here,
  95. \li \c a is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients.
  96. \li \c b is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.
  97. In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
  98. constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
  99. \code
  100. Matrix3f a(3,3);
  101. \endcode
  102. and is a no-operation.
  103. Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4:
  104. \code
  105. Vector2d a(5.0, 6.0);
  106. Vector3d b(5.0, 6.0, 7.0);
  107. Vector4d c(5.0, 6.0, 7.0, 8.0);
  108. \endcode
  109. \section TutorialMatrixCoeffAccessors Coefficient accessors
  110. The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators.
  111. For matrices, the row index is always passed first. For vectors, just pass one index.
  112. The numbering starts at 0. This example is self-explanatory:
  113. <table class="example">
  114. <tr><th>Example:</th><th>Output:</th></tr>
  115. <tr><td>
  116. \include tut_matrix_coefficient_accessors.cpp
  117. </td>
  118. <td>
  119. \verbinclude tut_matrix_coefficient_accessors.out
  120. </td></tr></table>
  121. Note that the syntax <tt> m(index) </tt>
  122. is not restricted to vectors, it is also available for general matrices, meaning index-based access
  123. in the array of coefficients. This however depends on the matrix's storage order. All Eigen matrices default to
  124. column-major storage order, but this can be changed to row-major, see \ref TopicStorageOrders "Storage orders".
  125. The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to
  126. take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language
  127. would make matrix[i,j] compile to the same thing as matrix[j] !
  128. \section TutorialMatrixCommaInitializer Comma-initialization
  129. %Matrix and vector coefficients can be conveniently set using the so-called \em comma-initializer syntax.
  130. For now, it is enough to know this example:
  131. <table class="example">
  132. <tr><th>Example:</th><th>Output:</th></tr>
  133. <tr>
  134. <td>\include Tutorial_commainit_01.cpp </td>
  135. <td>\verbinclude Tutorial_commainit_01.out </td>
  136. </tr></table>
  137. The right-hand side can also contain matrix expressions as discussed in \ref TutorialAdvancedInitialization "this page".
  138. \section TutorialMatrixSizesResizing Resizing
  139. The current size of a matrix can be retrieved by \link EigenBase::rows() rows()\endlink, \link EigenBase::cols() cols() \endlink and \link EigenBase::size() size()\endlink. These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the \link PlainObjectBase::resize(Index,Index) resize() \endlink method.
  140. <table class="example">
  141. <tr><th>Example:</th><th>Output:</th></tr>
  142. <tr>
  143. <td>\include tut_matrix_resize.cpp </td>
  144. <td>\verbinclude tut_matrix_resize.out </td>
  145. </tr></table>
  146. The resize() method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change.
  147. If you want a conservative variant of resize() which does not change the coefficients, use \link PlainObjectBase::conservativeResize() conservativeResize()\endlink, see \ref TopicResizing "this page" for more details.
  148. All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
  149. resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
  150. but the following code is legal:
  151. <table class="example">
  152. <tr><th>Example:</th><th>Output:</th></tr>
  153. <tr>
  154. <td>\include tut_matrix_resize_fixed_size.cpp </td>
  155. <td>\verbinclude tut_matrix_resize_fixed_size.out </td>
  156. </tr></table>
  157. \section TutorialMatrixAssignment Assignment and resizing
  158. Assignment is the action of copying a matrix into another, using \c operator=. Eigen resizes the matrix on the left-hand side automatically so that it matches the size of the matrix on the right-hand size. For example:
  159. <table class="example">
  160. <tr><th>Example:</th><th>Output:</th></tr>
  161. <tr>
  162. <td>\include tut_matrix_assignment_resizing.cpp </td>
  163. <td>\verbinclude tut_matrix_assignment_resizing.out </td>
  164. </tr></table>
  165. Of course, if the left-hand side is of fixed size, resizing it is not allowed.
  166. If you do not want this automatic resizing to happen (for example for debugging purposes), you can disable it, see
  167. \ref TopicResizing "this page".
  168. \section TutorialMatrixFixedVsDynamic Fixed vs. Dynamic size
  169. When should one use fixed sizes (e.g. \c Matrix4f), and when should one prefer dynamic sizes (e.g. \c MatrixXf)?
  170. The simple answer is: use fixed
  171. sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes,
  172. especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial
  173. to performance, as it allows Eigen to avoid dynamic memory allocation and to unroll
  174. loops. Internally, a fixed-size Eigen matrix is just a plain static array, i.e. doing
  175. \code Matrix4f mymatrix; \endcode
  176. really amounts to just doing
  177. \code float mymatrix[16]; \endcode
  178. so this really has zero runtime cost. By contrast, the array of a dynamic-size matrix
  179. is always allocated on the heap, so doing
  180. \code MatrixXf mymatrix(rows,columns); \endcode
  181. amounts to doing
  182. \code float *mymatrix = new float[rows*columns]; \endcode
  183. and in addition to that, the MatrixXf object stores its number of rows and columns as
  184. member variables.
  185. The limitation of using fixed sizes, of course, is that this is only possible
  186. when you know the sizes at compile time. Also, for large enough sizes, say for sizes
  187. greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible.
  188. Worse, trying to create a very large matrix using fixed sizes could result in a stack overflow,
  189. since Eigen will try to allocate the array as a static array, which by default goes on the stack.
  190. Finally, depending on circumstances, Eigen can also be more aggressive trying to vectorize
  191. (use SIMD instructions) when dynamic sizes are used, see \ref TopicVectorization "Vectorization".
  192. \section TutorialMatrixOptTemplParams Optional template parameters
  193. We mentioned at the beginning of this page that the Matrix class takes six template parameters,
  194. but so far we only discussed the first three. The remaining three parameters are optional. Here is
  195. the complete list of template parameters:
  196. \code
  197. Matrix<typename Scalar,
  198. int RowsAtCompileTime,
  199. int ColsAtCompileTime,
  200. int Options = 0,
  201. int MaxRowsAtCompileTime = RowsAtCompileTime,
  202. int MaxColsAtCompileTime = ColsAtCompileTime>
  203. \endcode
  204. \li \c Options is a bit field. Here, we discuss only one bit: \c RowMajor. It specifies that the matrices
  205. of this type use row-major storage order; by default, the storage order is column-major. See the page on
  206. \ref TopicStorageOrders "storage orders". For example, this type means row-major 3x3 matrices:
  207. \code
  208. Matrix<float, 3, 3, RowMajor>
  209. \endcode
  210. \li \c MaxRowsAtCompileTime and \c MaxColsAtCompileTime are useful when you want to specify that, even though
  211. the exact sizes of your matrices are not known at compile time, a fixed upper bound is known at
  212. compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation.
  213. For example the following matrix type uses a static array of 12 floats, without dynamic memory allocation:
  214. \code
  215. Matrix<float, Dynamic, Dynamic, 0, 3, 4>
  216. \endcode
  217. \section TutorialMatrixTypedefs Convenience typedefs
  218. Eigen defines the following Matrix typedefs:
  219. \li MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
  220. \li VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
  221. \li RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.
  222. Where:
  223. \li N can be any one of \c 2, \c 3, \c 4, or \c X (meaning \c Dynamic).
  224. \li t can be any one of \c i (meaning int), \c f (meaning float), \c d (meaning double),
  225. \c cf (meaning complex<float>), or \c cd (meaning complex<double>). The fact that typedefs are only
  226. defined for these five types doesn't mean that they are the only supported scalar types. For example,
  227. all standard integer types are supported, see \ref TopicScalarTypes "Scalar types".
  228. \li \b Next: \ref TutorialMatrixArithmetic
  229. */
  230. }