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