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