blob: 5e3098361d70b489ea376b9faf79a1cc2856aae3 [file] [log] [blame]
Benoit Jacob43388342010-06-25 10:04:35 -04001namespace Eigen {
2
Jitse Niesen9fa4e9a2010-07-05 10:59:29 +01003/** \page TutorialMatrixClass Tutorial page 1 - The %Matrix class
Benoit Jacob43388342010-06-25 10:04:35 -04004
5\ingroup Tutorial
6
Benoit Jacobe078bb22010-06-26 14:00:00 -04007\li \b Previous: \ref GettingStarted
8\li \b Next: \ref TutorialMatrixArithmetic
9
Gael Guennebaudf98c7582010-06-27 20:21:12 +020010We assume that you have already read the quick \link GettingStarted "getting started" \endlink tutorial.
Benoit Jacob43388342010-06-25 10:04:35 -040011This page is the first one in a much longer multi-page tutorial.
12
Benoit Jacobe078bb22010-06-26 14:00:00 -040013\b Table \b of \b contents
14 - \ref TutorialMatrixFirst3Params
15 - \ref TutorialMatrixVectors
16 - \ref TutorialMatrixDynamic
17 - \ref TutorialMatrixConstructors
18 - \ref TutorialMatrixCoeffAccessors
Gael Guennebaudaae59942010-06-28 00:22:47 +020019 - \ref TutorialMatrixCommaInitializer
Benoit Jacobe078bb22010-06-26 14:00:00 -040020 - \ref TutorialMatrixSizesResizing
21 - \ref TutorialMatrixAssignment
22 - \ref TutorialMatrixFixedVsDynamic
23 - \ref TutorialMatrixOptTemplParams
24 - \ref TutorialMatrixTypedefs
25
Jitse Niesen3428d802010-07-06 10:48:25 +010026In Eigen, all matrices and vectors are objects of the Matrix template class.
Benoit Jacob43388342010-06-25 10:04:35 -040027Vectors are just a special case of matrices, with either 1 row or 1 column.
28
Jitse Niesen3428d802010-07-06 10:48:25 +010029\section TutorialMatrixFirst3Params The first three template parameters of Matrix
Benoit Jacob43388342010-06-25 10:04:35 -040030
Jitse Niesen3428d802010-07-06 10:48:25 +010031The Matrix class takes six template parameters, but for now it's enough to
32learn about the first three first parameters. The three remaining parameters have default
Benoit Jacob43388342010-06-25 10:04:35 -040033values, which for now we will leave untouched, and which we
Benoit Jacobe078bb22010-06-26 14:00:00 -040034\ref TutorialMatrixOptTemplParams "discuss below".
Benoit Jacob43388342010-06-25 10:04:35 -040035
Jitse Niesen3428d802010-07-06 10:48:25 +010036The three mandatory template parameters of Matrix are:
Benoit Jacob43388342010-06-25 10:04:35 -040037\code
38Matrix<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 Jacobe078bb22010-06-26 14:00:00 -040042 See \ref TopicScalarTypes "Scalar types" for a list of all supported
Benoit Jacob43388342010-06-25 10:04:35 -040043 scalar types and for how to extend support to new types.
44\li \c RowsAtCompileTime and \c ColsAtCompileTime are the number of rows
Jitse Niesen3428d802010-07-06 10:48:25 +010045 and columns of the matrix as known at compile time (see
46 \ref TutorialMatrixDynamic "below" for what to do if the number is not
47 known at compile time).
Benoit Jacob43388342010-06-25 10:04:35 -040048
49We offer a lot of convenience typedefs to cover the usual cases. For example, \c Matrix4f is
50a 4x4 matrix of floats. Here is how it is defined by Eigen:
51\code
Jitse Niesen3428d802010-07-06 10:48:25 +010052typedef Matrix<float, 4, 4> Matrix4f;
Benoit Jacob43388342010-06-25 10:04:35 -040053\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -040054We discuss \ref TutorialMatrixTypedefs "below" these convenience typedefs.
Benoit Jacob43388342010-06-25 10:04:35 -040055
Benoit Jacobe078bb22010-06-26 14:00:00 -040056\section TutorialMatrixVectors Vectors
Benoit Jacob43388342010-06-25 10:04:35 -040057
58As mentioned above, in Eigen, vectors are just a special case of
59matrices, with either 1 row or 1 column. The case where they have 1 column is the most common;
60such vectors are called column-vectors, often abbreviated as just vectors. In the other case
61where they have 1 row, they are called row-vectors.
62
Jitse Niesen3428d802010-07-06 10:48:25 +010063For 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 -040064\code
65typedef Matrix<float, 3, 1> Vector3f;
66\endcode
Jitse Niesen3428d802010-07-06 10:48:25 +010067We also offer convenience typedefs for row-vectors, for example:
Benoit Jacob43388342010-06-25 10:04:35 -040068\code
69typedef Matrix<int, 1, 2> RowVector2i;
70\endcode
71
Benoit Jacobe078bb22010-06-26 14:00:00 -040072\section TutorialMatrixDynamic The special value Dynamic
Benoit Jacob43388342010-06-25 10:04:35 -040073
74Of course, Eigen is not limited to matrices whose dimensions are known at compile time.
Jitse Niesen3428d802010-07-06 10:48:25 +010075The \c RowsAtCompileTime and \c ColsAtCompileTime template parameters can take the special
Benoit Jacob43388342010-06-25 10:04:35 -040076value \c Dynamic which indicates that the size is unknown at compile time, so must
Jitse Niesen3428d802010-07-06 10:48:25 +010077be handled as a run-time variable. In Eigen terminology, such a size is referred to as a
Benoit Jacob43388342010-06-25 10:04:35 -040078\em dynamic \em size; while a size that is known at compile time is called a
79\em fixed \em size. For example, the convenience typedef \c MatrixXd, meaning
80a matrix of doubles with dynamic size, is defined as follows:
81\code
Jitse Niesen3428d802010-07-06 10:48:25 +010082typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
Benoit Jacob43388342010-06-25 10:04:35 -040083\endcode
84And similarly, we define a self-explanatory typedef \c VectorXi as follows:
85\code
Jitse Niesen3428d802010-07-06 10:48:25 +010086typedef Matrix<int, Dynamic, 1> VectorXi;
Benoit Jacob43388342010-06-25 10:04:35 -040087\endcode
88You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
89\code
90Matrix<float, 3, Dynamic>
91\endcode
92
Benoit Jacobe078bb22010-06-26 14:00:00 -040093\section TutorialMatrixConstructors Constructors
Benoit Jacob43388342010-06-25 10:04:35 -040094
Benoit Jacob26129222010-10-15 09:44:43 -040095A 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 -040096\code
97Matrix3f a;
98MatrixXf b;
99\endcode
100Here,
101\li \c a is a 3x3 matrix, with a static float[9] array of uninitialized coefficients,
102\li \c b is a dynamic-size matrix whose size is currently 0x0, and whose array of
103coefficients hasn't yet been allocated at all.
104
105Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
106For vectors, just pass the vector size. They allocate the array of coefficients
107with the given size, but don't initialize the coefficients themselves:
108\code
109MatrixXf a(10,15);
110VectorXf b(30);
111\endcode
112Here,
113\li \c a is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients.
114\li \c b is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.
115
116In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
117constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
118\code
119Matrix3f a(3,3);
120\endcode
121and is a no-operation.
122
123Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4:
124\code
125Vector2d a(5.0, 6.0);
126Vector3d b(5.0, 6.0, 7.0);
127Vector4d c(5.0, 6.0, 7.0, 8.0);
128\endcode
129
Benoit Jacobe078bb22010-06-26 14:00:00 -0400130\section TutorialMatrixCoeffAccessors Coefficient accessors
Benoit Jacob43388342010-06-25 10:04:35 -0400131
132The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators.
133For matrices, the row index is always passed first. For vectors, just pass one index.
134The numbering starts at 0. This example is self-explanatory:
Benoit Jacob43388342010-06-25 10:04:35 -0400135
Jitse Niesen8e776c92010-07-12 12:02:31 +0100136<table class="tutorial_code"><tr><td>
137Example: \include tut_matrix_coefficient_accessors.cpp
138</td>
139<td>
140Output: \verbinclude tut_matrix_coefficient_accessors.out
141</td></tr></table>
142
143Note that the syntax <tt> m(index) </tt>
Benoit Jacob43388342010-06-25 10:04:35 -0400144is not restricted to vectors, it is also available for general matrices, meaning index-based access
145in 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 -0400146column-major storage order, but this can be changed to row-major, see \ref TopicStorageOrders "Storage orders".
Benoit Jacob43388342010-06-25 10:04:35 -0400147
148The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to
149take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language
150would make matrix[i,j] compile to the same thing as matrix[j] !
151
Gael Guennebaudaae59942010-06-28 00:22:47 +0200152\section TutorialMatrixCommaInitializer Comma-initialization
153
Jitse Niesen8e776c92010-07-12 12:02:31 +0100154%Matrix and vector coefficients can be conveniently set using the so-called \em comma-initializer syntax.
Gael Guennebaudaae59942010-06-28 00:22:47 +0200155For now, it is enough to know this example:
Jitse Niesen8e776c92010-07-12 12:02:31 +0100156
157<table class="tutorial_code"><tr><td>
158Example: \include Tutorial_commainit_01.cpp
159</td>
160<td>
Gael Guennebaudaae59942010-06-28 00:22:47 +0200161Output: \verbinclude Tutorial_commainit_01.out
Jitse Niesen8e776c92010-07-12 12:02:31 +0100162</td></tr></table>
163
164
Jitse Niesen3428d802010-07-06 10:48:25 +0100165The right-hand side can also contain matrix expressions as discussed in \ref TutorialAdvancedInitialization "this page".
Gael Guennebaudaae59942010-06-28 00:22:47 +0200166
Benoit Jacobe078bb22010-06-26 14:00:00 -0400167\section TutorialMatrixSizesResizing Resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400168
Jitse Niesen3428d802010-07-06 10:48:25 +0100169The 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 DenseStorageBase::resize(Index,Index) resize() \endlink method.
Jitse Niesen8e776c92010-07-12 12:02:31 +0100170
171<table class="tutorial_code"><tr><td>
172Example: \include tut_matrix_resize.cpp
173</td>
174<td>
Benoit Jacob43388342010-06-25 10:04:35 -0400175Output: \verbinclude tut_matrix_resize.out
Jitse Niesen8e776c92010-07-12 12:02:31 +0100176</td></tr></table>
Benoit Jacob43388342010-06-25 10:04:35 -0400177
Jitse Niesen3428d802010-07-06 10:48:25 +0100178The 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.
179If you want a conservative variant of resize() which does not change the coefficients, use \link DenseStorageBase::conservativeResize() conservativeResize()\endlink, see \ref TopicResizing "this page" for more details.
Benoit Jacob43388342010-06-25 10:04:35 -0400180
181All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
182resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
183but the following code is legal:
Jitse Niesen8e776c92010-07-12 12:02:31 +0100184
185<table class="tutorial_code"><tr><td>
186Example: \include tut_matrix_resize_fixed_size.cpp
187</td>
188<td>
Benoit Jacob43388342010-06-25 10:04:35 -0400189Output: \verbinclude tut_matrix_resize_fixed_size.out
Jitse Niesen8e776c92010-07-12 12:02:31 +0100190</td></tr></table>
191
Benoit Jacob43388342010-06-25 10:04:35 -0400192
Benoit Jacobe078bb22010-06-26 14:00:00 -0400193\section TutorialMatrixAssignment Assignment and resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400194
Jitse Niesen3428d802010-07-06 10:48:25 +0100195Assignment 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 +0100196
197<table class="tutorial_code"><tr><td>
198Example: \include tut_matrix_assignment_resizing.cpp
199</td>
200<td>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400201Output: \verbinclude tut_matrix_assignment_resizing.out
Jitse Niesen8e776c92010-07-12 12:02:31 +0100202</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400203
Jitse Niesen3428d802010-07-06 10:48:25 +0100204Of course, if the left-hand side is of fixed size, resizing it is not allowed.
Benoit Jacobe078bb22010-06-26 14:00:00 -0400205
206If you do not want this automatic resizing to happen (for example for debugging purposes), you can disable it, see
207\ref TopicResizing "this page".
208
209
210\section TutorialMatrixFixedVsDynamic Fixed vs. Dynamic size
211
212When 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 -0400213The simple answer is: use fixed
214sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes,
215especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial
216to performance, as it allows Eigen to avoid dynamic memory allocation and to unroll
217loops. Internally, a fixed-size Eigen matrix is just a plain static array, i.e. doing
218\code Matrix4f mymatrix; \endcode
219really amounts to just doing
220\code float mymatrix[16]; \endcode
Jitse Niesen30701642010-06-29 11:42:51 +0100221so this really has zero runtime cost. By contrast, the array of a dynamic-size matrix
Benoit Jacob43388342010-06-25 10:04:35 -0400222is always allocated on the heap, so doing
223\code MatrixXf mymatrix(rows,columns); \endcode
224amounts to doing
225\code float *mymatrix = new float[rows*columns]; \endcode
226and in addition to that, the MatrixXf object stores its number of rows and columns as
227member variables.
228
229The limitation of using fixed sizes, of course, is that this is only possible
230when you know the sizes at compile time. Also, for large enough sizes, say for sizes
231greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible.
232Worse, trying to create a very large matrix using fixed sizes could result in a stack overflow,
233since Eigen will try to allocate the array as a static array, which by default goes on the stack.
234Finally, depending on circumstances, Eigen can also be more aggressive trying to vectorize
Benoit Jacobe078bb22010-06-26 14:00:00 -0400235(use SIMD instructions) when dynamic sizes are used, see \ref TopicVectorization "Vectorization".
Benoit Jacob43388342010-06-25 10:04:35 -0400236
Benoit Jacobe078bb22010-06-26 14:00:00 -0400237\section TutorialMatrixOptTemplParams Optional template parameters
Benoit Jacob43388342010-06-25 10:04:35 -0400238
Jitse Niesen3428d802010-07-06 10:48:25 +0100239We mentioned at the beginning of this page that the Matrix class takes six template parameters,
240but so far we only discussed the first three. The remaining three parameters are optional. Here is
Benoit Jacob43388342010-06-25 10:04:35 -0400241the complete list of template parameters:
242\code
243Matrix<typename Scalar,
244 int RowsAtCompileTime,
245 int ColsAtCompileTime,
246 int Options = 0,
247 int MaxRowsAtCompileTime = RowsAtCompileTime,
248 int MaxColsAtCompileTime = ColsAtCompileTime>
249\endcode
Jitse Niesen3428d802010-07-06 10:48:25 +0100250\li \c Options is a bit field. Here, we discuss only one bit: \c RowMajor. It specifies that the matrices
251 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 -0400252 \ref TopicStorageOrders "storage orders". For example, this type means row-major 3x3 matrices:
Benoit Jacob43388342010-06-25 10:04:35 -0400253 \code
Jitse Niesen3428d802010-07-06 10:48:25 +0100254 Matrix<float, 3, 3, RowMajor>
Benoit Jacob43388342010-06-25 10:04:35 -0400255 \endcode
256\li \c MaxRowsAtCompileTime and \c MaxColsAtCompileTime are useful when you want to specify that, even though
Jitse Niesen3428d802010-07-06 10:48:25 +0100257 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 -0400258 compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation.
259 For example the following matrix type uses a static array of 12 floats, without dynamic memory allocation:
260 \code
Jitse Niesen3428d802010-07-06 10:48:25 +0100261 Matrix<float, Dynamic, Dynamic, 0, 3, 4>
Benoit Jacob43388342010-06-25 10:04:35 -0400262 \endcode
263
Benoit Jacobe078bb22010-06-26 14:00:00 -0400264\section TutorialMatrixTypedefs Convenience typedefs
Benoit Jacob43388342010-06-25 10:04:35 -0400265
266Eigen defines the following Matrix typedefs:
Benoit Jacob08c17c42010-07-01 20:29:13 -0400267\li MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
268\li VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
269\li RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.
Benoit Jacob43388342010-06-25 10:04:35 -0400270
271Where:
Jitse Niesen3428d802010-07-06 10:48:25 +0100272\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 -0400273\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 -0400274 \c cf (meaning complex<float>), or \c cd (meaning complex<double>). The fact that typedefs are only
Jitse Niesen3428d802010-07-06 10:48:25 +0100275 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 -0400276 all standard integer types are supported, see \ref TopicScalarTypes "Scalar types".
277
278\li \b Next: \ref TutorialMatrixArithmetic
Benoit Jacob43388342010-06-25 10:04:35 -0400279
280*/
281
Jitse Niesen30701642010-06-29 11:42:51 +0100282}