blob: 2d84cd72cc84c4f06fedf62185fe1888e7397935 [file] [log] [blame]
Benoit Jacob43388342010-06-25 10:04:35 -04001namespace Eigen {
2
Benoit Jacobe078bb22010-06-26 14:00:00 -04003/** \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
Benoit Jacob43388342010-06-25 10:04:35 -040026In Eigen, all matrices and vectors are object of the Matrix class.
27Vectors are just a special case of matrices, with either 1 row or 1 column.
28
Benoit Jacobe078bb22010-06-26 14:00:00 -040029\section TutorialMatrixFirst3Params The first 3 template parameters of Matrix
Benoit Jacob43388342010-06-25 10:04:35 -040030
31The Matrix class takes 6 template parameters, but for now it's enough to
32learn about the 3 first parameters. The 3 remaining parameters have default
33values, 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
36The 3 mandatory template parameters of Matrix are:
37\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
45 and columns of the matrix as known at compile-time.
46
47We offer a lot of convenience typedefs to cover the usual cases. For example, \c Matrix4f is
48a 4x4 matrix of floats. Here is how it is defined by Eigen:
49\code
50typedef Matrix<float,4,4> Matrix4f;
51\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -040052We discuss \ref TutorialMatrixTypedefs "below" these convenience typedefs.
Benoit Jacob43388342010-06-25 10:04:35 -040053
Benoit Jacobe078bb22010-06-26 14:00:00 -040054\section TutorialMatrixVectors Vectors
Benoit Jacob43388342010-06-25 10:04:35 -040055
56As mentioned above, in Eigen, vectors are just a special case of
57matrices, with either 1 row or 1 column. The case where they have 1 column is the most common;
58such vectors are called column-vectors, often abbreviated as just vectors. In the other case
59where they have 1 row, they are called row-vectors.
60
61For example, the convenience typedef \c Vector3f is defined as follows by Eigen:
62\code
63typedef Matrix<float, 3, 1> Vector3f;
64\endcode
65and we also offer convenience typedefs for row-vectors, for example:
66\code
67typedef Matrix<int, 1, 2> RowVector2i;
68\endcode
69
Benoit Jacobe078bb22010-06-26 14:00:00 -040070\section TutorialMatrixDynamic The special value Dynamic
Benoit Jacob43388342010-06-25 10:04:35 -040071
72Of course, Eigen is not limited to matrices whose dimensions are known at compile time.
73The above-discussed \c RowsAtCompileTime and \c ColsAtCompileTime can take the special
74value \c Dynamic which indicates that the size is unknown at compile time, so must
75be 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
78a matrix of doubles with dynamic size, is defined as follows:
79\code
80typedef Matrix<double,Dynamic,Dynamic> MatrixXd;
81\endcode
82And similarly, we define a self-explanatory typedef \c VectorXi as follows:
83\code
84typedef Matrix<int,Dynamic,1> VectorXi;
85\endcode
86You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
87\code
88Matrix<float, 3, Dynamic>
89\endcode
90
Benoit Jacobe078bb22010-06-26 14:00:00 -040091\section TutorialMatrixConstructors Constructors
Benoit Jacob43388342010-06-25 10:04:35 -040092
93A default constructor is always available, and always has zero runtime cost. You can do:
94\code
95Matrix3f a;
96MatrixXf b;
97\endcode
98Here,
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
101coefficients hasn't yet been allocated at all.
102
103Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
104For vectors, just pass the vector size. They allocate the array of coefficients
105with the given size, but don't initialize the coefficients themselves:
106\code
107MatrixXf a(10,15);
108VectorXf b(30);
109\endcode
110Here,
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
114In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
115constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
116\code
117Matrix3f a(3,3);
118\endcode
119and is a no-operation.
120
121Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4:
122\code
123Vector2d a(5.0, 6.0);
124Vector3d b(5.0, 6.0, 7.0);
125Vector4d c(5.0, 6.0, 7.0, 8.0);
126\endcode
127
Benoit Jacobe078bb22010-06-26 14:00:00 -0400128\section TutorialMatrixCoeffAccessors Coefficient accessors
Benoit Jacob43388342010-06-25 10:04:35 -0400129
130The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators.
131For matrices, the row index is always passed first. For vectors, just pass one index.
132The numbering starts at 0. This example is self-explanatory:
133\include tut_matrix_coefficient_accessors.cpp
134Output: \verbinclude tut_matrix_coefficient_accessors.out
135
136Note that the syntax
137\code
138m(index)
139\endcode
140is not restricted to vectors, it is also available for general matrices, meaning index-based access
141in 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 -0400142column-major storage order, but this can be changed to row-major, see \ref TopicStorageOrders "Storage orders".
Benoit Jacob43388342010-06-25 10:04:35 -0400143
144The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to
145take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language
146would make matrix[i,j] compile to the same thing as matrix[j] !
147
Gael Guennebaudaae59942010-06-28 00:22:47 +0200148\section TutorialMatrixCommaInitializer Comma-initialization
149
150Matrix and vector coefficients can be conveniently set using the so-called \em comma-initializer syntax.
151For now, it is enough to know this example:
152\include Tutorial_commainit_01.cpp
153Output: \verbinclude Tutorial_commainit_01.out
154The right hand side can also contains matrix expressions as discussed in \ref TutorialAdvancedInitialization "this page".
155
Benoit Jacobe078bb22010-06-26 14:00:00 -0400156\section TutorialMatrixSizesResizing Resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400157
158The current sizes can be retrieved by rows(), cols() and size(). Resizing a dynamic-size matrix is done by the resize() method.
159For example: \include tut_matrix_resize.cpp
160Output: \verbinclude tut_matrix_resize.out
161
162The resize() method is a no-operation if the actual array size doesn't change; otherwise it is destructive.
Benoit Jacobe078bb22010-06-26 14:00:00 -0400163If you want a conservative variant of resize(), use conservativeResize(), see \ref TopicResizing "this page" for more details.
Benoit Jacob43388342010-06-25 10:04:35 -0400164
165All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
166resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
167but the following code is legal:
168\include tut_matrix_resize_fixed_size.cpp
169Output: \verbinclude tut_matrix_resize_fixed_size.out
170
Benoit Jacobe078bb22010-06-26 14:00:00 -0400171\section TutorialMatrixAssignment Assignment and resizing
Benoit Jacob43388342010-06-25 10:04:35 -0400172
Benoit Jacobe078bb22010-06-26 14:00:00 -0400173Assignment is the action of copying a matrix into another, using \c operator=. The only non-obvious thing to know here, is that
174Eigen 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
176Output: \verbinclude tut_matrix_assignment_resizing.out
177
178Of course, if the left hand side is of fixed size, resizing it is not allowed.
179
180If 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
186When 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 -0400187The simple answer is: use fixed
188sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes,
189especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial
190to performance, as it allows Eigen to avoid dynamic memory allocation and to unroll
191loops. Internally, a fixed-size Eigen matrix is just a plain static array, i.e. doing
192\code Matrix4f mymatrix; \endcode
193really amounts to just doing
194\code float mymatrix[16]; \endcode
195so this really has zero runtime cost. By constrast, the array of a dynamic-size matrix
196is always allocated on the heap, so doing
197\code MatrixXf mymatrix(rows,columns); \endcode
198amounts to doing
199\code float *mymatrix = new float[rows*columns]; \endcode
200and in addition to that, the MatrixXf object stores its number of rows and columns as
201member variables.
202
203The limitation of using fixed sizes, of course, is that this is only possible
204when you know the sizes at compile time. Also, for large enough sizes, say for sizes
205greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible.
206Worse, trying to create a very large matrix using fixed sizes could result in a stack overflow,
207since Eigen will try to allocate the array as a static array, which by default goes on the stack.
208Finally, depending on circumstances, Eigen can also be more aggressive trying to vectorize
Benoit Jacobe078bb22010-06-26 14:00:00 -0400209(use SIMD instructions) when dynamic sizes are used, see \ref TopicVectorization "Vectorization".
Benoit Jacob43388342010-06-25 10:04:35 -0400210
Benoit Jacobe078bb22010-06-26 14:00:00 -0400211\section TutorialMatrixOptTemplParams Optional template parameters
Benoit Jacob43388342010-06-25 10:04:35 -0400212
213We mentioned at the beginning of this page that the Matrix class takes 6 template parameters,
214but so far we only discussed the first 3. The remaining 3 parameters are optional. Here is
215the complete list of template parameters:
216\code
217Matrix<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 Jacobe078bb22010-06-26 14:00:00 -0400226 \ref TopicStorageOrders "storage orders". For example, this type means row-major 3x3 matrices:
Benoit Jacob43388342010-06-25 10:04:35 -0400227 \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 Jacobe078bb22010-06-26 14:00:00 -0400238\section TutorialMatrixTypedefs Convenience typedefs
Benoit Jacob43388342010-06-25 10:04:35 -0400239
240Eigen 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
245Where:
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 Jacobe078bb22010-06-26 14:00:00 -0400250 all standard integer types are supported, see \ref TopicScalarTypes "Scalar types".
251
252\li \b Next: \ref TutorialMatrixArithmetic
Benoit Jacob43388342010-06-25 10:04:35 -0400253
254*/
255
256}