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