blob: fc0ce5b1e005935487446eb91b461a4de3861ab8 [file] [log] [blame]
Benoit Jacob43388342010-06-25 10:04:35 -04001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialMatrixClass The Matrix class
Benoit Jacob43388342010-06-25 10:04:35 -04004
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01005\eigenAutoToc
Benoit Jacobe078bb22010-06-26 14:00:00 -04006
Jitse Niesen3428d802010-07-06 10:48:25 +01007In Eigen, all matrices and vectors are objects of the Matrix template class.
Benoit Jacob43388342010-06-25 10:04:35 -04008Vectors are just a special case of matrices, with either 1 row or 1 column.
9
Jitse Niesen3428d802010-07-06 10:48:25 +010010\section TutorialMatrixFirst3Params The first three template parameters of Matrix
Benoit Jacob43388342010-06-25 10:04:35 -040011
Jitse Niesen3428d802010-07-06 10:48:25 +010012The Matrix class takes six template parameters, but for now it's enough to
13learn about the first three first parameters. The three remaining parameters have default
Benoit Jacob43388342010-06-25 10:04:35 -040014values, which for now we will leave untouched, and which we
Benoit Jacobe078bb22010-06-26 14:00:00 -040015\ref TutorialMatrixOptTemplParams "discuss below".
Benoit Jacob43388342010-06-25 10:04:35 -040016
Jitse Niesen3428d802010-07-06 10:48:25 +010017The three mandatory template parameters of Matrix are:
Benoit Jacob43388342010-06-25 10:04:35 -040018\code
19Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
20\endcode
21\li \c Scalar is the scalar type, i.e. the type of the coefficients.
22 That is, if you want a matrix of floats, choose \c float here.
Benoit Jacobe078bb22010-06-26 14:00:00 -040023 See \ref TopicScalarTypes "Scalar types" for a list of all supported
Benoit Jacob43388342010-06-25 10:04:35 -040024 scalar types and for how to extend support to new types.
25\li \c RowsAtCompileTime and \c ColsAtCompileTime are the number of rows
Jitse Niesen3428d802010-07-06 10:48:25 +010026 and columns of the matrix as known at compile time (see
27 \ref TutorialMatrixDynamic "below" for what to do if the number is not
28 known at compile time).
Benoit Jacob43388342010-06-25 10:04:35 -040029
30We offer a lot of convenience typedefs to cover the usual cases. For example, \c Matrix4f is
31a 4x4 matrix of floats. Here is how it is defined by Eigen:
32\code
Jitse Niesen3428d802010-07-06 10:48:25 +010033typedef Matrix<float, 4, 4> Matrix4f;
Benoit Jacob43388342010-06-25 10:04:35 -040034\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -040035We discuss \ref TutorialMatrixTypedefs "below" these convenience typedefs.
Benoit Jacob43388342010-06-25 10:04:35 -040036
Benoit Jacobe078bb22010-06-26 14:00:00 -040037\section TutorialMatrixVectors Vectors
Benoit Jacob43388342010-06-25 10:04:35 -040038
39As mentioned above, in Eigen, vectors are just a special case of
40matrices, with either 1 row or 1 column. The case where they have 1 column is the most common;
41such vectors are called column-vectors, often abbreviated as just vectors. In the other case
42where they have 1 row, they are called row-vectors.
43
Jitse Niesen3428d802010-07-06 10:48:25 +010044For example, the convenience typedef \c Vector3f is a (column) vector of 3 floats. It is defined as follows by Eigen:
Benoit Jacob43388342010-06-25 10:04:35 -040045\code
46typedef Matrix<float, 3, 1> Vector3f;
47\endcode
Jitse Niesen3428d802010-07-06 10:48:25 +010048We also offer convenience typedefs for row-vectors, for example:
Benoit Jacob43388342010-06-25 10:04:35 -040049\code
50typedef Matrix<int, 1, 2> RowVector2i;
51\endcode
52
Benoit Jacobe078bb22010-06-26 14:00:00 -040053\section TutorialMatrixDynamic The special value Dynamic
Benoit Jacob43388342010-06-25 10:04:35 -040054
55Of course, Eigen is not limited to matrices whose dimensions are known at compile time.
Jitse Niesen3428d802010-07-06 10:48:25 +010056The \c RowsAtCompileTime and \c ColsAtCompileTime template parameters can take the special
Benoit Jacob43388342010-06-25 10:04:35 -040057value \c Dynamic which indicates that the size is unknown at compile time, so must
Jitse Niesen3428d802010-07-06 10:48:25 +010058be handled as a run-time variable. In Eigen terminology, such a size is referred to as a
Benoit Jacob43388342010-06-25 10:04:35 -040059\em dynamic \em size; while a size that is known at compile time is called a
60\em fixed \em size. For example, the convenience typedef \c MatrixXd, meaning
61a matrix of doubles with dynamic size, is defined as follows:
62\code
Jitse Niesen3428d802010-07-06 10:48:25 +010063typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
Benoit Jacob43388342010-06-25 10:04:35 -040064\endcode
65And similarly, we define a self-explanatory typedef \c VectorXi as follows:
66\code
Jitse Niesen3428d802010-07-06 10:48:25 +010067typedef Matrix<int, Dynamic, 1> VectorXi;
Benoit Jacob43388342010-06-25 10:04:35 -040068\endcode
69You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
70\code
71Matrix<float, 3, Dynamic>
72\endcode
73
Benoit Jacobe078bb22010-06-26 14:00:00 -040074\section TutorialMatrixConstructors Constructors
Benoit Jacob43388342010-06-25 10:04:35 -040075
Benoit Jacob26129222010-10-15 09:44:43 -040076A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients. You can do:
Benoit Jacob43388342010-06-25 10:04:35 -040077\code
78Matrix3f a;
79MatrixXf b;
80\endcode
81Here,
Jitse Niesen4e6d7462013-06-18 14:35:12 +010082\li \c a is a 3-by-3 matrix, with a plain float[9] array of uninitialized coefficients,
83\li \c b is a dynamic-size matrix whose size is currently 0-by-0, and whose array of
Benoit Jacob43388342010-06-25 10:04:35 -040084coefficients hasn't yet been allocated at all.
85
86Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
87For vectors, just pass the vector size. They allocate the array of coefficients
88with the given size, but don't initialize the coefficients themselves:
89\code
90MatrixXf a(10,15);
91VectorXf b(30);
92\endcode
93Here,
94\li \c a is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients.
95\li \c b is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.
96
97In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
98constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
99\code
100Matrix3f a(3,3);
101\endcode
102and is a no-operation.
103
David Tellenbachdb152b92019-01-21 16:25:57 +0100104Additionally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4:
Benoit Jacob43388342010-06-25 10:04:35 -0400105\code
106Vector2d a(5.0, 6.0);
107Vector3d b(5.0, 6.0, 7.0);
108Vector4d c(5.0, 6.0, 7.0, 8.0);
109\endcode
110
David Tellenbachdb152b92019-01-21 16:25:57 +0100111If C++11 is enabled, matrices can be constructed and initialized using initializer lists. In the case of fixed-sized vectors
112and rowvectors a simple initializer list can be passed:
113\code
114Vector2i a {1, 2}; // A vector containing the elements {1, 2}
115Matrix<int, 4, 1> b {1, 2, 3, 4}; // A row-vector containing the elements {1, 2, 3, 4}
116Matrix<int, 1, 4> c {1, 2, 3, 4}; // A vector containing the elements {1, 2, 3, 4}
117\endcode
118
119In the case of fixed or dynamically sized matrices an initializer list containing an initializer list for each row
120can be passed. If the matrix is fixed-sized, the number of elements that are passed must match the dimensions.
121\code
122MatrixXi a {
123 {1, 2}, // first row
124 {3, 4} // second row
125};
126Matrix<double, 2, 3> b {
127 {2.0, 3.0, 4.0},
128 {5.0, 6.0, 7.0},
129};
130\endcode
131
132In the case of vectors and rowvectors, the following shorthand notation can be used:
133\code
134VectorXd a {{1.5, 2.5, 3.5}}; // A vector with 3 rows
135RowVectorXd b {{1.0, 2.0, 3.0, 4.0}}; // A rowvector with 4 columns
136\endcode
137
Benoit Jacobe078bb22010-06-26 14:00:00 -0400138\section TutorialMatrixCoeffAccessors Coefficient accessors
Benoit Jacob43388342010-06-25 10:04:35 -0400139
140The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators.
141For matrices, the row index is always passed first. For vectors, just pass one index.
142The numbering starts at 0. This example is self-explanatory:
Benoit Jacob43388342010-06-25 10:04:35 -0400143
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200144<table class="example">
145<tr><th>Example:</th><th>Output:</th></tr>
146<tr><td>
147\include tut_matrix_coefficient_accessors.cpp
Jitse Niesen8e776c92010-07-12 12:02:31 +0100148</td>
149<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200150\verbinclude tut_matrix_coefficient_accessors.out
Jitse Niesen8e776c92010-07-12 12:02:31 +0100151</td></tr></table>
152
153Note that the syntax <tt> m(index) </tt>
Benoit Jacob43388342010-06-25 10:04:35 -0400154is not restricted to vectors, it is also available for general matrices, meaning index-based access
155in the array of coefficients. This however depends on the matrix's storage order. All Eigen matrices default to
Benoit Jacobe078bb22010-06-26 14:00:00 -0400156column-major storage order, but this can be changed to row-major, see \ref TopicStorageOrders "Storage orders".
Benoit Jacob43388342010-06-25 10:04:35 -0400157
158The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to
159take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language
160would make matrix[i,j] compile to the same thing as matrix[j] !
161
Gael Guennebaudaae59942010-06-28 00:22:47 +0200162\section TutorialMatrixCommaInitializer Comma-initialization
163
Jitse Niesen8e776c92010-07-12 12:02:31 +0100164%Matrix and vector coefficients can be conveniently set using the so-called \em comma-initializer syntax.
Gael Guennebaudaae59942010-06-28 00:22:47 +0200165For now, it is enough to know this example:
Jitse Niesen8e776c92010-07-12 12:02:31 +0100166
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200167<table class="example">
168<tr><th>Example:</th><th>Output:</th></tr>
169<tr>
170<td>\include Tutorial_commainit_01.cpp </td>
171<td>\verbinclude Tutorial_commainit_01.out </td>
172</tr></table>
Jitse Niesen8e776c92010-07-12 12:02:31 +0100173
174
Jitse Niesen3428d802010-07-06 10:48:25 +0100175The right-hand side can also contain matrix expressions as discussed in \ref TutorialAdvancedInitialization "this page".
Gael Guennebaudaae59942010-06-28 00:22:47 +0200176
Benoit Jacobe078bb22010-06-26 14:00:00 -0400177\section TutorialMatrixSizesResizing Resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400178
Benoit Jacob8c17fab2010-10-20 09:34:13 -0400179The 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 Niesen8e776c92010-07-12 12:02:31 +0100180
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200181<table class="example">
182<tr><th>Example:</th><th>Output:</th></tr>
183<tr>
184<td>\include tut_matrix_resize.cpp </td>
185<td>\verbinclude tut_matrix_resize.out </td>
186</tr></table>
Benoit Jacob43388342010-06-25 10:04:35 -0400187
Jitse Niesen3428d802010-07-06 10:48:25 +0100188The 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 Jacob8c17fab2010-10-20 09:34:13 -0400189If 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 Jacob43388342010-06-25 10:04:35 -0400190
191All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
192resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
193but the following code is legal:
Jitse Niesen8e776c92010-07-12 12:02:31 +0100194
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200195<table class="example">
196<tr><th>Example:</th><th>Output:</th></tr>
197<tr>
198<td>\include tut_matrix_resize_fixed_size.cpp </td>
199<td>\verbinclude tut_matrix_resize_fixed_size.out </td>
200</tr></table>
Jitse Niesen8e776c92010-07-12 12:02:31 +0100201
Benoit Jacob43388342010-06-25 10:04:35 -0400202
Benoit Jacobe078bb22010-06-26 14:00:00 -0400203\section TutorialMatrixAssignment Assignment and resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400204
Jitse Niesen3428d802010-07-06 10:48:25 +0100205Assignment 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 Niesen8e776c92010-07-12 12:02:31 +0100206
Benoit Jacob9fa54d42010-10-19 08:42:49 -0400207<table class="example">
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200208<tr><th>Example:</th><th>Output:</th></tr>
209<tr>
210<td>\include tut_matrix_assignment_resizing.cpp </td>
211<td>\verbinclude tut_matrix_assignment_resizing.out </td>
212</tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400213
Jitse Niesen3428d802010-07-06 10:48:25 +0100214Of course, if the left-hand side is of fixed size, resizing it is not allowed.
Benoit Jacobe078bb22010-06-26 14:00:00 -0400215
216If you do not want this automatic resizing to happen (for example for debugging purposes), you can disable it, see
217\ref TopicResizing "this page".
218
219
220\section TutorialMatrixFixedVsDynamic Fixed vs. Dynamic size
221
222When should one use fixed sizes (e.g. \c Matrix4f), and when should one prefer dynamic sizes (e.g. \c MatrixXf)?
Benoit Jacob43388342010-06-25 10:04:35 -0400223The simple answer is: use fixed
224sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes,
225especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial
226to performance, as it allows Eigen to avoid dynamic memory allocation and to unroll
Jitse Niesen4e6d7462013-06-18 14:35:12 +0100227loops. Internally, a fixed-size Eigen matrix is just a plain array, i.e. doing
Benoit Jacob43388342010-06-25 10:04:35 -0400228\code Matrix4f mymatrix; \endcode
229really amounts to just doing
230\code float mymatrix[16]; \endcode
Jitse Niesen30701642010-06-29 11:42:51 +0100231so this really has zero runtime cost. By contrast, the array of a dynamic-size matrix
Benoit Jacob43388342010-06-25 10:04:35 -0400232is always allocated on the heap, so doing
233\code MatrixXf mymatrix(rows,columns); \endcode
234amounts to doing
235\code float *mymatrix = new float[rows*columns]; \endcode
236and in addition to that, the MatrixXf object stores its number of rows and columns as
237member variables.
238
239The limitation of using fixed sizes, of course, is that this is only possible
240when you know the sizes at compile time. Also, for large enough sizes, say for sizes
241greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible.
Jitse Niesen4e6d7462013-06-18 14:35:12 +0100242Worse, trying to create a very large matrix using fixed sizes inside a function could result in a
243stack overflow, since Eigen will try to allocate the array automatically as a local variable, and
244this is normally done on the stack.
Benoit Jacob43388342010-06-25 10:04:35 -0400245Finally, depending on circumstances, Eigen can also be more aggressive trying to vectorize
Benoit Jacobe078bb22010-06-26 14:00:00 -0400246(use SIMD instructions) when dynamic sizes are used, see \ref TopicVectorization "Vectorization".
Benoit Jacob43388342010-06-25 10:04:35 -0400247
Benoit Jacobe078bb22010-06-26 14:00:00 -0400248\section TutorialMatrixOptTemplParams Optional template parameters
Benoit Jacob43388342010-06-25 10:04:35 -0400249
Jitse Niesen3428d802010-07-06 10:48:25 +0100250We mentioned at the beginning of this page that the Matrix class takes six template parameters,
251but so far we only discussed the first three. The remaining three parameters are optional. Here is
Benoit Jacob43388342010-06-25 10:04:35 -0400252the complete list of template parameters:
253\code
254Matrix<typename Scalar,
255 int RowsAtCompileTime,
256 int ColsAtCompileTime,
257 int Options = 0,
258 int MaxRowsAtCompileTime = RowsAtCompileTime,
259 int MaxColsAtCompileTime = ColsAtCompileTime>
260\endcode
Jitse Niesen3428d802010-07-06 10:48:25 +0100261\li \c Options is a bit field. Here, we discuss only one bit: \c RowMajor. It specifies that the matrices
262 of this type use row-major storage order; by default, the storage order is column-major. See the page on
Benoit Jacobe078bb22010-06-26 14:00:00 -0400263 \ref TopicStorageOrders "storage orders". For example, this type means row-major 3x3 matrices:
Benoit Jacob43388342010-06-25 10:04:35 -0400264 \code
Jitse Niesen3428d802010-07-06 10:48:25 +0100265 Matrix<float, 3, 3, RowMajor>
Benoit Jacob43388342010-06-25 10:04:35 -0400266 \endcode
267\li \c MaxRowsAtCompileTime and \c MaxColsAtCompileTime are useful when you want to specify that, even though
Jitse Niesen3428d802010-07-06 10:48:25 +0100268 the exact sizes of your matrices are not known at compile time, a fixed upper bound is known at
Benoit Jacob43388342010-06-25 10:04:35 -0400269 compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation.
Jitse Niesen4e6d7462013-06-18 14:35:12 +0100270 For example the following matrix type uses a plain array of 12 floats, without dynamic memory allocation:
Benoit Jacob43388342010-06-25 10:04:35 -0400271 \code
Jitse Niesen3428d802010-07-06 10:48:25 +0100272 Matrix<float, Dynamic, Dynamic, 0, 3, 4>
Benoit Jacob43388342010-06-25 10:04:35 -0400273 \endcode
274
Benoit Jacobe078bb22010-06-26 14:00:00 -0400275\section TutorialMatrixTypedefs Convenience typedefs
Benoit Jacob43388342010-06-25 10:04:35 -0400276
277Eigen defines the following Matrix typedefs:
Benoit Jacob08c17c42010-07-01 20:29:13 -0400278\li MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
279\li VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
280\li RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.
Benoit Jacob43388342010-06-25 10:04:35 -0400281
282Where:
Jitse Niesen3428d802010-07-06 10:48:25 +0100283\li N can be any one of \c 2, \c 3, \c 4, or \c X (meaning \c Dynamic).
Benoit Jacob08c17c42010-07-01 20:29:13 -0400284\li t can be any one of \c i (meaning int), \c f (meaning float), \c d (meaning double),
Benoit Jacob43388342010-06-25 10:04:35 -0400285 \c cf (meaning complex<float>), or \c cd (meaning complex<double>). The fact that typedefs are only
Jitse Niesen3428d802010-07-06 10:48:25 +0100286 defined for these five types doesn't mean that they are the only supported scalar types. For example,
Benoit Jacobe078bb22010-06-26 14:00:00 -0400287 all standard integer types are supported, see \ref TopicScalarTypes "Scalar types".
288
Benoit Jacob43388342010-06-25 10:04:35 -0400289
290*/
291
Jitse Niesen30701642010-06-29 11:42:51 +0100292}