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