blob: c73bbf5ccd7c31b4c0e9f7fe8f152cb7b0e98822 [file] [log] [blame]
Benoit Jacob43388342010-06-25 10:04:35 -04001namespace Eigen {
2
3o /** \page tut_matrix Tutorial page 1 - the Matrix class
4
5\ingroup Tutorial
6
7We assume that you have already read the \ref GettingStarted "quick \"getting started\" tutorial.
8This page is the first one in a much longer multi-page tutorial.
9
10In Eigen, all matrices and vectors are object of the Matrix class.
11Vectors are just a special case of matrices, with either 1 row or 1 column.
12
13\section tut_matrix_class The Matrix class
14
15The Matrix class takes 6 template parameters, but for now it's enough to
16learn about the 3 first parameters. The 3 remaining parameters have default
17values, which for now we will leave untouched, and which we
18\ref tut_matrix_3_last_template_params "discuss below".
19
20The 3 mandatory template parameters of Matrix are:
21\code
22Matrix<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
31We offer a lot of convenience typedefs to cover the usual cases. For example, \c Matrix4f is
32a 4x4 matrix of floats. Here is how it is defined by Eigen:
33\code
34typedef Matrix<float,4,4> Matrix4f;
35\endcode
36We discuss \ref tut_matrix_typedefs "below" these convenience typedefs.
37
38\section tut_matrix_vectors Vectors
39
40As mentioned above, in Eigen, vectors are just a special case of
41matrices, with either 1 row or 1 column. The case where they have 1 column is the most common;
42such vectors are called column-vectors, often abbreviated as just vectors. In the other case
43where they have 1 row, they are called row-vectors.
44
45For example, the convenience typedef \c Vector3f is defined as follows by Eigen:
46\code
47typedef Matrix<float, 3, 1> Vector3f;
48\endcode
49and we also offer convenience typedefs for row-vectors, for example:
50\code
51typedef Matrix<int, 1, 2> RowVector2i;
52\endcode
53
54\section tut_matrix_dynamic The special value Dynamic
55
56Of course, Eigen is not limited to matrices whose dimensions are known at compile time.
57The above-discussed \c RowsAtCompileTime and \c ColsAtCompileTime can take the special
58value \c Dynamic which indicates that the size is unknown at compile time, so must
59be 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
62a matrix of doubles with dynamic size, is defined as follows:
63\code
64typedef Matrix<double,Dynamic,Dynamic> MatrixXd;
65\endcode
66And similarly, we define a self-explanatory typedef \c VectorXi as follows:
67\code
68typedef Matrix<int,Dynamic,1> VectorXi;
69\endcode
70You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in:
71\code
72Matrix<float, 3, Dynamic>
73\endcode
74
75\section tut_matrix_constructors Constructors
76
77A default constructor is always available, and always has zero runtime cost. You can do:
78\code
79Matrix3f a;
80MatrixXf b;
81\endcode
82Here,
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
85coefficients hasn't yet been allocated at all.
86
87Constructors taking sizes are also available. For matrices, the number of rows is always passed first.
88For vectors, just pass the vector size. They allocate the array of coefficients
89with the given size, but don't initialize the coefficients themselves:
90\code
91MatrixXf a(10,15);
92VectorXf b(30);
93\endcode
94Here,
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
98In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these
99constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal:
100\code
101Matrix3f a(3,3);
102\endcode
103and is a no-operation.
104
105Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4:
106\code
107Vector2d a(5.0, 6.0);
108Vector3d b(5.0, 6.0, 7.0);
109Vector4d c(5.0, 6.0, 7.0, 8.0);
110\endcode
111
112\section tut_matrix_coefficient_accessors Coefficient accessors
113
114The primary coefficient accessors and mutators in Eigen are the overloaded parenthesis operators.
115For matrices, the row index is always passed first. For vectors, just pass one index.
116The numbering starts at 0. This example is self-explanatory:
117\include tut_matrix_coefficient_accessors.cpp
118Output: \verbinclude tut_matrix_coefficient_accessors.out
119
120Note that the syntax
121\code
122m(index)
123\endcode
124is not restricted to vectors, it is also available for general matrices, meaning index-based access
125in the array of coefficients. This however depends on the matrix's storage order. All Eigen matrices default to
126column-major storage order, but this can be changed to row-major, see \ref topic_storage_orders "Storage orders".
127
128The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to
129take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language
130would make matrix[i,j] compile to the same thing as matrix[j] !
131
132\section tut_matrix_sizes_and_resizing Resizing
133
134The current sizes can be retrieved by rows(), cols() and size(). Resizing a dynamic-size matrix is done by the resize() method.
135For example: \include tut_matrix_resize.cpp
136Output: \verbinclude tut_matrix_resize.out
137
138The resize() method is a no-operation if the actual array size doesn't change; otherwise it is destructive.
139If you want a conservative variant of resize(), use conservativeResize().
140
141All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually
142resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure;
143but the following code is legal:
144\include tut_matrix_resize_fixed_size.cpp
145Output: \verbinclude tut_matrix_resize_fixed_size.out
146
147\section tut_matrix_fixed_vs_dynamic Fixed vs. Dynamic size
148
149When should one use fixed sizes (e.g. \c Matrix4f), and when should one prefer dynamic sizes (e.g. \c MatrixXf) ?
150The simple answer is: use fixed
151sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes,
152especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial
153to performance, as it allows Eigen to avoid dynamic memory allocation and to unroll
154loops. Internally, a fixed-size Eigen matrix is just a plain static array, i.e. doing
155\code Matrix4f mymatrix; \endcode
156really amounts to just doing
157\code float mymatrix[16]; \endcode
158so this really has zero runtime cost. By constrast, the array of a dynamic-size matrix
159is always allocated on the heap, so doing
160\code MatrixXf mymatrix(rows,columns); \endcode
161amounts to doing
162\code float *mymatrix = new float[rows*columns]; \endcode
163and in addition to that, the MatrixXf object stores its number of rows and columns as
164member variables.
165
166The limitation of using fixed sizes, of course, is that this is only possible
167when you know the sizes at compile time. Also, for large enough sizes, say for sizes
168greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible.
169Worse, trying to create a very large matrix using fixed sizes could result in a stack overflow,
170since Eigen will try to allocate the array as a static array, which by default goes on the stack.
171Finally, 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
176We mentioned at the beginning of this page that the Matrix class takes 6 template parameters,
177but so far we only discussed the first 3. The remaining 3 parameters are optional. Here is
178the complete list of template parameters:
179\code
180Matrix<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
203Eigen 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
208Where:
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}