blob: e07b404d1f48328a5ec959086a40b8bb46116358 [file] [log] [blame]
Tim Holy44b19b42012-02-08 22:11:12 +01001namespace Eigen {
2
3/** \page TutorialMapClass Tutorial page 10 - Interfacing with C/C++ arrays and external libraries: the %Map class
4
5\ingroup Tutorial
6
7\li \b Previous: \ref TutorialSparse
Gael Guennebaudf41d96d2012-12-24 13:33:22 +01008\li \b Next:
Tim Holy44b19b42012-02-08 22:11:12 +01009
10This tutorial page explains how to work with "raw" C++ arrays. This can be useful in a variety of contexts, particularly when "importing" vectors and matrices from other libraries into Eigen.
11
Gael Guennebaud2ea1e492012-12-28 18:58:07 +010012\tableofcontents
Tim Holy44b19b42012-02-08 22:11:12 +010013
14\section TutorialMapIntroduction Introduction
15
16Occasionally you may have a pre-defined array of numbers that you want to use within Eigen as a vector or matrix. While one option is to make a copy of the data, most commonly you probably want to re-use this memory as an Eigen type. Fortunately, this is very easy with the Map class.
17
18\section TutorialMapTypes Map types and declaring Map variables
19
20A Map object has a type defined by its Eigen equivalent:
21\code
22Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >
23\endcode
24Note that, in this default case, a Map requires just a single template parameter.
25
26To construct a Map variable, you need two other pieces of information: a pointer to the region of memory defining the array of coefficients, and the desired shape of the matrix or vector. For example, to define a matrix of \c float with sizes determined at compile time, you might do the following:
27\code
28Map<MatrixXf> mf(pf,rows,columns);
29\endcode
30where \c pf is a \c float \c * pointing to the array of memory. A fixed-size read-only vector of integers might be declared as
31\code
32Map<const Vector4i> mi(pi);
33\endcode
34where \c pi is an \c int \c *. In this case the size does not have to be passed to the constructor, because it is already specified by the Matrix/Array type.
35
36Note that Map does not have a default constructor; you \em must pass a pointer to intialize the object. However, you can work around this requirement (see \ref TutorialMapPlacementNew).
37
38Map is flexible enough to accomodate a variety of different data representations. There are two other (optional) template parameters:
39\code
40Map<typename MatrixType,
41 int MapOptions,
42 typename StrideType>
43\endcode
44\li \c MapOptions specifies whether the pointer is \c #Aligned, or \c #Unaligned. The default is \c #Unaligned.
45\li \c StrideType allows you to specify a custom layout for the memory array, using the Stride class. One example would be to specify that the data array is organized in row-major format:
46<table class="example">
47<tr><th>Example:</th><th>Output:</th></tr>
48<tr>
49<td>\include Tutorial_Map_rowmajor.cpp </td>
50<td>\verbinclude Tutorial_Map_rowmajor.out </td>
51</table>
52However, Stride is even more flexible than this; for details, see the documentation for the Map and Stride classes.
53
54\section TutorialMapUsing Using Map variables
55
56You can use a Map object just like any other Eigen type:
57<table class="example">
58<tr><th>Example:</th><th>Output:</th></tr>
59<tr>
60<td>\include Tutorial_Map_using.cpp </td>
61<td>\verbinclude Tutorial_Map_using.out </td>
62</table>
63
64However, when writing functions taking Eigen types, it is important to realize that a Map type is \em not identical to its Dense equivalent. See \ref TopicFunctionTakingEigenTypesMultiarguments for details.
65
66\section TutorialMapPlacementNew Changing the mapped array
67
68It is possible to change the array of a Map object after declaration, using the C++ "placement new" syntax:
69<table class="example">
70<tr><th>Example:</th><th>Output:</th></tr>
71<tr>
72<td>\include Map_placement_new.cpp </td>
73<td>\verbinclude Map_placement_new.out </td>
74</table>
75Despite appearances, this does not invoke the memory allocator, because the syntax specifies the location for storing the result.
76
77This syntax makes it possible to declare a Map object without first knowing the mapped array's location in memory:
78\code
79Map<Matrix3f> A(NULL); // don't try to use this matrix yet!
80VectorXf b(n_matrices);
81for (int i = 0; i < n_matrices; i++)
82{
83 new (&A) Map<Matrix3f>(get_matrix_pointer(i));
84 b(i) = A.trace();
85}
86\endcode
87
Gael Guennebaudf41d96d2012-12-24 13:33:22 +010088\li \b Next:
Tim Holy44b19b42012-02-08 22:11:12 +010089
90*/
91
92}