Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > 99cb5ede6a5329071fbeecc8218deb35 > files > 1094

eigen3-doc-3.2-3.mga4.noarch.rpm

<!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: The Matrix 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>
   &#160;<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__TutorialMatrixClass.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">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</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">The Matrix 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>In <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a>, all matrices and vectors are objects of the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors. ">Matrix</a> template class. Vectors are just a special case of matrices, with either 1 row or 1 column.</p>
<h1><a class="anchor" id="TutorialMatrixFirst3Params"></a>
The first three template parameters of Matrix</h1>
<p>The <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors. ">Matrix</a> class takes six template parameters, but for now it's enough to learn about the first three first parameters. The three remaining parameters have default values, which for now we will leave untouched, and which we <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixOptTemplParams">discuss below</a>.</p>
<p>The three mandatory template parameters of <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors. ">Matrix</a> are: </p>
<div class="fragment"><div class="line">Matrix&lt;typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime&gt;</div>
</div><!-- fragment --> <ul>
<li><code>Scalar</code> is the scalar type, i.e. the type of the coefficients. That is, if you want a matrix of floats, choose <code>float</code> here. See <a class="el" href="TopicScalarTypes.html">Scalar types</a> for a list of all supported scalar types and for how to extend support to new types. </li>
<li><code>RowsAtCompileTime</code> and <code>ColsAtCompileTime</code> are the number of rows and columns of the matrix as known at compile time (see <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixDynamic">below</a> for what to do if the number is not known at compile time).</li>
</ul>
<p>We offer a lot of convenience typedefs to cover the usual cases. For example, <code>Matrix4f</code> is a 4x4 matrix of floats. Here is how it is defined by <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a>: </p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;float, 4, 4&gt; <a class="code" href="group__matrixtypedefs.html#ga548963d9c2934037af5f8e5b3df87e45">Matrix4f</a>;</div>
</div><!-- fragment --><p> We discuss <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixTypedefs">below</a> these convenience typedefs.</p>
<h1><a class="anchor" id="TutorialMatrixVectors"></a>
Vectors</h1>
<p>As mentioned above, in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a>, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; such vectors are called column-vectors, often abbreviated as just vectors. In the other case where they have 1 row, they are called row-vectors.</p>
<p>For example, the convenience typedef <code>Vector3f</code> is a (column) vector of 3 floats. It is defined as follows by <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a>: </p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;float, 3, 1&gt; <a class="code" href="group__matrixtypedefs.html#ga5ac9fb0df0c4858477890cce1f998096">Vector3f</a>;</div>
</div><!-- fragment --><p> We also offer convenience typedefs for row-vectors, for example: </p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;int, 1, 2&gt; <a class="code" href="group__matrixtypedefs.html#ga721fa43db56fff83d1f96b2e370653c4">RowVector2i</a>;</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixDynamic"></a>
The special value Dynamic</h1>
<p>Of course, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> is not limited to matrices whose dimensions are known at compile time. The <code>RowsAtCompileTime</code> and <code>ColsAtCompileTime</code> template parameters can take the special value <code>Dynamic</code> which indicates that the size is unknown at compile time, so must be handled as a run-time variable. In <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> terminology, such a size is referred to as a <em>dynamic</em> <em>size</em>; while a size that is known at compile time is called a <em>fixed</em> <em>size</em>. For example, the convenience typedef <code>MatrixXd</code>, meaning a matrix of doubles with dynamic size, is defined as follows: </p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;double, Dynamic, Dynamic&gt; <a class="code" href="group__matrixtypedefs.html#ga0750af9a6b82761985a15fe77256de87">MatrixXd</a>;</div>
</div><!-- fragment --><p> And similarly, we define a self-explanatory typedef <code>VectorXi</code> as follows: </p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;int, Dynamic, 1&gt; <a class="code" href="group__matrixtypedefs.html#ga0312eb421909d32007ae4a55b9f22abb">VectorXi</a>;</div>
</div><!-- fragment --><p> You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in: </p>
<div class="fragment"><div class="line">Matrix&lt;float, 3, Dynamic&gt;</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixConstructors"></a>
Constructors</h1>
<p>A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients. You can do: </p>
<div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga2b787393a7fc870d99aa634f60b2510c">Matrix3f</a> a;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gabab09c32e96cfa9829a88400627af162">MatrixXf</a> b;</div>
</div><!-- fragment --><p> Here, </p>
<ul>
<li><code>a</code> is a 3-by-3 matrix, with a plain float[9] array of uninitialized coefficients, </li>
<li><code>b</code> is a dynamic-size matrix whose size is currently 0-by-0, and whose array of coefficients hasn't yet been allocated at all.</li>
</ul>
<p>Constructors taking sizes are also available. For matrices, the number of rows is always passed first. For vectors, just pass the vector size. They allocate the array of coefficients with the given size, but don't initialize the coefficients themselves: </p>
<div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gabab09c32e96cfa9829a88400627af162">MatrixXf</a> a(10,15);</div>
<div class="line">VectorXf b(30);</div>
</div><!-- fragment --><p> Here, </p>
<ul>
<li><code>a</code> is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients. </li>
<li><code>b</code> is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.</li>
</ul>
<p>In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal: </p>
<div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga2b787393a7fc870d99aa634f60b2510c">Matrix3f</a> a(3,3);</div>
</div><!-- fragment --><p> and is a no-operation.</p>
<p>Finally, we also offer some constructors to initialize the coefficients of small fixed-size vectors up to size 4: </p>
<div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Vector2d</a> a(5.0, 6.0);</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga2006332f6989f501762673e21f5128f5">Vector3d</a> b(5.0, 6.0, 7.0);</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga9bdb6b1010218b700f4bc0d23c7d2ee8">Vector4d</a> c(5.0, 6.0, 7.0, 8.0);</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixCoeffAccessors"></a>
Coefficient accessors</h1>
<p>The primary coefficient accessors and mutators in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> are the overloaded parenthesis operators. For matrices, the row index is always passed first. For vectors, just pass one index. The numbering starts at 0. This example is self-explanatory:</p>
<table  class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>Eigen;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  MatrixXd m(2,2);</div>
<div class="line">  m(0,0) = 3;</div>
<div class="line">  m(1,0) = 2.5;</div>
<div class="line">  m(0,1) = -1;</div>
<div class="line">  m(1,1) = m(1,0) + m(0,1);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the matrix m:\n&quot;</span> &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">VectorXd</a> v(2);</div>
<div class="line">  v(0) = 4;</div>
<div class="line">  v(1) = v(0) - 1;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the vector v:\n&quot;</span> &lt;&lt; v &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the matrix m:
  3  -1
2.5 1.5
Here is the vector v:
4
3
</pre> </td></tr>
</table>
<p>Note that the syntax <code> m(index) </code> is not restricted to vectors, it is also available for general matrices, meaning index-based access in the array of coefficients. This however depends on the matrix's storage order. All <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> matrices default to column-major storage order, but this can be changed to row-major, see <a class="el" href="group__TopicStorageOrders.html">Storage orders</a>.</p>
<p>The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to take more than one argument. We restrict operator[] to vectors, because an awkwardness in the C++ language would make matrix[i,j] compile to the same thing as matrix[j] !</p>
<h1><a class="anchor" id="TutorialMatrixCommaInitializer"></a>
Comma-initialization</h1>
<p>Matrix and vector coefficients can be conveniently set using the so-called <em>comma-initializer</em> syntax. For now, it is enough to know this example:</p>
<table  class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga2b787393a7fc870d99aa634f60b2510c">Matrix3f</a> m;</div>
<div class="line">m &lt;&lt; 1, 2, 3,</div>
<div class="line">     4, 5, 6,</div>
<div class="line">     7, 8, 9;</div>
<div class="line">std::cout &lt;&lt; m;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">1 2 3
4 5 6
7 8 9</pre>  </td></tr>
</table>
<p>The right-hand side can also contain matrix expressions as discussed in <a class="el" href="group__TutorialAdvancedInitialization.html">this page</a>.</p>
<h1><a class="anchor" id="TutorialMatrixSizesResizing"></a>
Resizing</h1>
<p>The current size of a matrix can be retrieved by <a class="el" href="structEigen_1_1EigenBase.html#a5552abd83dbd03c85cea6d61fd8875a5">rows()</a>, <a class="el" href="structEigen_1_1EigenBase.html#aaca1908a5ec508a25ff0a8bca803e5f3">cols() </a> and <a class="el" href="structEigen_1_1EigenBase.html#a76f5bc8a03ec105ab4be1f2b91b7b5d5">size()</a>. These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the <a class="el" href="classEigen_1_1PlainObjectBase.html#afbbb33d14fe7fb9683019a39ce1c659d">resize() </a> method.</p>
<table  class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>Eigen;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  MatrixXd m(2,5);</div>
<div class="line">  m.resize(4,3);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The matrix m is of size &quot;</span></div>
<div class="line">            &lt;&lt; m.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; m.cols() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;It has &quot;</span> &lt;&lt; m.size() &lt;&lt; <span class="stringliteral">&quot; coefficients&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">VectorXd</a> v(2);</div>
<div class="line">  v.resize(5);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The vector v is of size &quot;</span> &lt;&lt; v.size() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;As a matrix, v is of size &quot;</span></div>
<div class="line">            &lt;&lt; v.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; v.cols() &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">The matrix m is of size 4x3
It has 12 coefficients
The vector v is of size 5
As a matrix, v is of size 5x1
</pre>  </td></tr>
</table>
<p>The resize() method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change. If you want a conservative variant of resize() which does not change the coefficients, use <a class="el" href="classEigen_1_1PlainObjectBase.html#a4ac24a195e3e35ce2d8946cee5873e51">conservativeResize()</a>, see <a class="el" href="TopicResizing.html">this page</a> for more details.</p>
<p>All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure; but the following code is legal:</p>
<table  class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>Eigen;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Matrix4d</a> m;</div>
<div class="line">  m.<a class="code" href="classEigen_1_1PlainObjectBase.html#afbbb33d14fe7fb9683019a39ce1c659d">resize</a>(4,4); <span class="comment">// no operation</span></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The matrix m is of size &quot;</span></div>
<div class="line">            &lt;&lt; m.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; m.cols() &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">The matrix m is of size 4x4
</pre>  </td></tr>
</table>
<h1><a class="anchor" id="TutorialMatrixAssignment"></a>
Assignment and resizing</h1>
<p>Assignment is the action of copying a matrix into another, using <code>operator=</code>. <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> resizes the matrix on the left-hand side automatically so that it matches the size of the matrix on the right-hand size. For example:</p>
<table  class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gabab09c32e96cfa9829a88400627af162">MatrixXf</a> a(2,2);</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;a is of size &quot;</span> &lt;&lt; a.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; a.cols() &lt;&lt; std::endl;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gabab09c32e96cfa9829a88400627af162">MatrixXf</a> b(3,3);</div>
<div class="line">a = b;</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;a is now of size &quot;</span> &lt;&lt; a.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; a.cols() &lt;&lt; std::endl;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">a is of size 2x2
a is now of size 3x3
</pre>  </td></tr>
</table>
<p>Of course, if the left-hand side is of fixed size, resizing it is not allowed.</p>
<p>If you do not want this automatic resizing to happen (for example for debugging purposes), you can disable it, see <a class="el" href="TopicResizing.html">this page</a>.</p>
<h1><a class="anchor" id="TutorialMatrixFixedVsDynamic"></a>
Fixed vs. Dynamic size</h1>
<p>When should one use fixed sizes (e.g. <code>Matrix4f</code>), and when should one prefer dynamic sizes (e.g. <code>MatrixXf</code>)? The simple answer is: use fixed sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes, especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial to performance, as it allows <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> to avoid dynamic memory allocation and to unroll loops. Internally, a fixed-size <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> matrix is just a plain array, i.e. doing </p>
<div class="fragment"><div class="line">Matrix4f mymatrix; </div>
</div><!-- fragment --><p> really amounts to just doing </p>
<div class="fragment"><div class="line"><span class="keywordtype">float</span> mymatrix[16]; </div>
</div><!-- fragment --><p> so this really has zero runtime cost. By contrast, the array of a dynamic-size matrix is always allocated on the heap, so doing </p>
<div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gabab09c32e96cfa9829a88400627af162">MatrixXf</a> mymatrix(rows,columns); </div>
</div><!-- fragment --><p> amounts to doing </p>
<div class="fragment"><div class="line"><span class="keywordtype">float</span> *mymatrix = <span class="keyword">new</span> <span class="keywordtype">float</span>[rows*columns]; </div>
</div><!-- fragment --><p> and in addition to that, the MatrixXf object stores its number of rows and columns as member variables.</p>
<p>The limitation of using fixed sizes, of course, is that this is only possible when you know the sizes at compile time. Also, for large enough sizes, say for sizes greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible. Worse, trying to create a very large matrix using fixed sizes inside a function could result in a stack overflow, since <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> will try to allocate the array automatically as a local variable, and this is normally done on the stack. Finally, depending on circumstances, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> can also be more aggressive trying to vectorize (use SIMD instructions) when dynamic sizes are used, see <a class="el" href="TopicVectorization.html">Vectorization</a>.</p>
<h1><a class="anchor" id="TutorialMatrixOptTemplParams"></a>
Optional template parameters</h1>
<p>We mentioned at the beginning of this page that the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors. ">Matrix</a> class takes six template parameters, but so far we only discussed the first three. The remaining three parameters are optional. Here is the complete list of template parameters: </p>
<div class="fragment"><div class="line">Matrix&lt;<span class="keyword">typename</span> Scalar,</div>
<div class="line">       <span class="keywordtype">int</span> RowsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> ColsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> Options = 0,</div>
<div class="line">       <span class="keywordtype">int</span> MaxRowsAtCompileTime = RowsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> MaxColsAtCompileTime = ColsAtCompileTime&gt;</div>
</div><!-- fragment --> <ul>
<li><code>Options</code> is a bit field. Here, we discuss only one bit: <code>RowMajor</code>. It specifies that the matrices of this type use row-major storage order; by default, the storage order is column-major. See the page on <a class="el" href="group__TopicStorageOrders.html">storage orders</a>. For example, this type means row-major 3x3 matrices: <div class="fragment"><div class="line">Matrix&lt;float, 3, 3, RowMajor&gt;</div>
</div><!-- fragment --> </li>
<li><code>MaxRowsAtCompileTime</code> and <code>MaxColsAtCompileTime</code> are useful when you want to specify that, even though the exact sizes of your matrices are not known at compile time, a fixed upper bound is known at compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation. For example the following matrix type uses a plain array of 12 floats, without dynamic memory allocation: <div class="fragment"><div class="line">Matrix&lt;float, Dynamic, Dynamic, 0, 3, 4&gt;</div>
</div><!-- fragment --></li>
</ul>
<h1><a class="anchor" id="TutorialMatrixTypedefs"></a>
Convenience typedefs</h1>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> defines the following <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors. ">Matrix</a> typedefs: </p>
<ul>
<li>MatrixNt for Matrix&lt;type, N, N&gt;. For example, MatrixXi for Matrix&lt;int, Dynamic, Dynamic&gt;. </li>
<li>VectorNt for Matrix&lt;type, N, 1&gt;. For example, Vector2f for Matrix&lt;float, 2, 1&gt;. </li>
<li>RowVectorNt for Matrix&lt;type, 1, N&gt;. For example, RowVector3d for Matrix&lt;double, 1, 3&gt;.</li>
</ul>
<p>Where: </p>
<ul>
<li>N can be any one of <code>2</code>, <code>3</code>, <code>4</code>, or <code>X</code> (meaning <code>Dynamic</code>). </li>
<li>t can be any one of <code>i</code> (meaning int), <code>f</code> (meaning float), <code>d</code> (meaning double), <code>cf</code> (meaning complex&lt;float&gt;), or <code>cd</code> (meaning complex&lt;double&gt;). The fact that typedefs are only defined for these five types doesn't mean that they are the only supported scalar types. For example, all standard integer types are supported, see <a class="el" href="TopicScalarTypes.html">Scalar types</a>. </li>
</ul>
</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>