<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.5"/> <title>Eigen: Interfacing with raw buffers: the Map class</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> $(document).ready(initResizable); $(window).load(resizeHeight); </script> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/search.js"></script> <script type="text/javascript"> $(document).ready(function() { searchBox.OnSelectItem(0); }); </script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="eigendoxy.css" rel="stylesheet" type="text/css"> <!-- --> <script type="text/javascript" src="eigen_navtree_hacks.js"></script> <!-- <script type="text/javascript"> --> <!-- </script> --> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <!-- <a name="top"></a> --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td> <td style="padding-left: 0.5em;"> <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>  <span id="projectnumber">3.2.0</span> </div> </td> <td> <div id="MSearchBox" class="MSearchBoxInactive"> <span class="left"> <img id="MSearchSelect" src="search/mag_sel.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> </span><span class="right"> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> $(document).ready(function(){initNavTree('group__TutorialMapClass.html','');}); </script> <div id="doc-content"> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Pages</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div class="header"> <div class="headertitle"> <div class="title">Interfacing with raw buffers: the Map class<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div> </div> </div><!--header--> <div class="contents"> <p>This page explains how to work with "raw" C/C++ arrays. This can be useful in a variety of contexts, particularly when "importing" vectors and matrices from other libraries into Eigen.</p> <h1><a class="anchor" id="TutorialMapIntroduction"></a> Introduction</h1> <p>Occasionally 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 <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> class.</p> <h1><a class="anchor" id="TutorialMapTypes"></a> Map types and declaring Map variables</h1> <p>A <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> object has a type defined by its Eigen equivalent: </p> <div class="fragment"><div class="line">Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> ></div> </div><!-- fragment --><p> Note that, in this default case, a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> requires just a single template parameter.</p> <p>To construct a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> 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 <code>float</code> with sizes determined at compile time, you might do the following: </p> <div class="fragment"><div class="line">Map<MatrixXf> mf(pf,rows,columns);</div> </div><!-- fragment --><p> where <code>pf</code> is a <code>float</code> <code>*</code> pointing to the array of memory. A fixed-size read-only vector of integers might be declared as </p> <div class="fragment"><div class="line">Map<const Vector4i> mi(pi);</div> </div><!-- fragment --><p> where <code>pi</code> is an <code>int</code> <code>*</code>. In this case the size does not have to be passed to the constructor, because it is already specified by the Matrix/Array type.</p> <p>Note that <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> does not have a default constructor; you <em>must</em> pass a pointer to intialize the object. However, you can work around this requirement (see <a class="el" href="group__TutorialMapClass.html#TutorialMapPlacementNew">Changing the mapped array</a>).</p> <p><a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> is flexible enough to accomodate a variety of different data representations. There are two other (optional) template parameters: </p> <div class="fragment"><div class="line">Map<<span class="keyword">typename</span> MatrixType,</div> <div class="line"> <span class="keywordtype">int</span> MapOptions,</div> <div class="line"> <span class="keyword">typename</span> StrideType></div> </div><!-- fragment --> <ul> <li><code>MapOptions</code> specifies whether the pointer is <code><a class="el" href="group__enums.html#gga456ac33d49271d3e2c371351cd1d6371ad5380ca00f3d74b38593adf8a0d06d3e">Aligned</a></code>, or <code><a class="el" href="group__enums.html#gga456ac33d49271d3e2c371351cd1d6371afeaf599f3da3693d2ab4a7cc48a19437">Unaligned</a></code>. The default is <code><a class="el" href="group__enums.html#gga456ac33d49271d3e2c371351cd1d6371afeaf599f3da3693d2ab4a7cc48a19437">Unaligned</a></code>. </li> <li><code>StrideType</code> allows you to specify a custom layout for the memory array, using the <a class="el" href="classEigen_1_1Stride.html" title="Holds strides information for Map. ">Stride</a> class. One example would be to specify that the data array is organized in row-major format: <table class="example"> <tr> <th>Example:</th><th>Output: </th></tr> <tr> <td><div class="fragment"><div class="line"><span class="keywordtype">int</span> array[8];</div> <div class="line"><span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i < 8; ++i) array[i] = i;</div> <div class="line">cout << <span class="stringliteral">"Column-major:\n"</span> << Map<Matrix<int,2,4> >(array) << endl;</div> <div class="line">cout << <span class="stringliteral">"Row-major:\n"</span> << Map<Matrix<int,2,4,RowMajor> >(array) << endl;</div> <div class="line">cout << <span class="stringliteral">"Row-major using stride:\n"</span> <<</div> <div class="line"> Map<Matrix<int,2,4>, <a class="code" href="group__enums.html#gga456ac33d49271d3e2c371351cd1d6371afeaf599f3da3693d2ab4a7cc48a19437">Unaligned</a>, Stride<1,4> >(array) << endl;</div> <div class="line"></div> </div><!-- fragment --> </td><td><pre class="fragment">Column-major: 0 2 4 6 1 3 5 7 Row-major: 0 1 2 3 4 5 6 7 Row-major using stride: 0 1 2 3 4 5 6 7 </pre> </td></tr> </table> However, <a class="el" href="classEigen_1_1Stride.html" title="Holds strides information for Map. ">Stride</a> is even more flexible than this; for details, see the documentation for the <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> and <a class="el" href="classEigen_1_1Stride.html" title="Holds strides information for Map. ">Stride</a> classes.</li> </ul> <h1><a class="anchor" id="TutorialMapUsing"></a> Using Map variables</h1> <p>You can use a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> object just like any other Eigen type: </p> <table class="example"> <tr> <th>Example:</th><th>Output: </th></tr> <tr> <td><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix<float,1,Dynamic> MatrixType;</div> <div class="line"><span class="keyword">typedef</span> Map<MatrixType> MapType;</div> <div class="line"><span class="keyword">typedef</span> Map<const MatrixType> MapTypeConst; <span class="comment">// a read-only map</span></div> <div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> n_dims = 5;</div> <div class="line"> </div> <div class="line">MatrixType m1(n_dims), m2(n_dims);</div> <div class="line">m1.setRandom();</div> <div class="line">m2.setRandom();</div> <div class="line"><span class="keywordtype">float</span> *p = &m2(0); <span class="comment">// get the address storing the data for m2</span></div> <div class="line">MapType m2map(p,m2.size()); <span class="comment">// m2map shares data with m2</span></div> <div class="line">MapTypeConst m2mapconst(p,m2.size()); <span class="comment">// a read-only accessor for m2</span></div> <div class="line"></div> <div class="line">cout << <span class="stringliteral">"m1: "</span> << m1 << endl;</div> <div class="line">cout << <span class="stringliteral">"m2: "</span> << m2 << endl;</div> <div class="line">cout << <span class="stringliteral">"Squared euclidean distance: "</span> << (m1-m2).squaredNorm() << endl;</div> <div class="line">cout << <span class="stringliteral">"Squared euclidean distance, using map: "</span> <<</div> <div class="line"> (m1-m2map).squaredNorm() << endl;</div> <div class="line">m2map(3) = 7; <span class="comment">// this will change m2, since they share the same array</span></div> <div class="line">cout << <span class="stringliteral">"Updated m2: "</span> << m2 << endl;</div> <div class="line">cout << <span class="stringliteral">"m2 coefficient 2, constant accessor: "</span> << m2mapconst(2) << endl;</div> <div class="line"><span class="comment">/* m2mapconst(2) = 5; */</span> <span class="comment">// this yields a compile-time error</span></div> </div><!-- fragment --> </td><td><pre class="fragment">m1: 0.68 -0.211 0.566 0.597 0.823 m2: -0.605 -0.33 0.536 -0.444 0.108 Squared euclidean distance: 3.26 Squared euclidean distance, using map: 3.26 Updated m2: -0.605 -0.33 0.536 7 0.108 m2 coefficient 2, constant accessor: 0.536 </pre> </td></tr> </table> <p>All Eigen functions are written to accept <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> objects just like other Eigen types. However, when writing your own functions taking Eigen types, this does <em>not</em> happen automatically: a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> type is not identical to its <a class="el" href="structEigen_1_1Dense.html">Dense</a> equivalent. See <a class="el" href="TopicFunctionTakingEigenTypes.html">Writing Functions Taking Eigen Types as Parameters</a> for details.</p> <h1><a class="anchor" id="TutorialMapPlacementNew"></a> Changing the mapped array</h1> <p>It is possible to change the array of a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> object after declaration, using the C++ "placement new" syntax: </p> <table class="example"> <tr> <th>Example:</th><th>Output: </th></tr> <tr> <td><div class="fragment"><div class="line"><span class="keywordtype">int</span> data[] = {1,2,3,4,5,6,7,8,9};</div> <div class="line">Map<RowVectorXi> v(data,4);</div> <div class="line">cout << <span class="stringliteral">"The mapped vector v is: "</span> << v << <span class="stringliteral">"\n"</span>;</div> <div class="line"><span class="keyword">new</span> (&v) Map<RowVectorXi>(data+4,5);</div> <div class="line">cout << <span class="stringliteral">"Now v is: "</span> << v << <span class="stringliteral">"\n"</span>;</div> </div><!-- fragment --> </td><td><pre class="fragment">The mapped vector v is: 1 2 3 4 Now v is: 5 6 7 8 9 </pre> </td></tr> </table> <p>Despite appearances, this does not invoke the memory allocator, because the syntax specifies the location for storing the result.</p> <p>This syntax makes it possible to declare a <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data. ">Map</a> object without first knowing the mapped array's location in memory: </p> <div class="fragment"><div class="line">Map<Matrix3f> A(NULL); <span class="comment">// don't try to use this matrix yet!</span></div> <div class="line">VectorXf b(n_matrices);</div> <div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < n_matrices; i++)</div> <div class="line">{</div> <div class="line"> <span class="keyword">new</span> (&A) Map<Matrix3f>(get_matrix_pointer(i));</div> <div class="line"> b(i) = A.<a class="code" href="classEigen_1_1MatrixBase.html#a71696dd0adbf4731561fd60e55c3a96e">trace</a>();</div> <div class="line">}</div> </div><!-- fragment --> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="footer">Generated on Mon Oct 28 2013 11:04:27 for Eigen by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li> </ul> </div> <!-- Piwik --> <!-- <script type="text/javascript"> var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.sylphide-consulting.com/piwik/" : "http://stats.sylphide-consulting.com/piwik/"); document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); </script><script type="text/javascript"> try { var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 20); piwikTracker.trackPageView(); piwikTracker.enableLinkTracking(); } catch( err ) {} </script><noscript><p><img src="http://stats.sylphide-consulting.com/piwik/piwik.php?idsite=20" style="border:0" alt="" /></p></noscript> --> <!-- End Piwik Tracking Code --> </body> </html>