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.

265 lines
12 KiB

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