<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>RandomGenerators.h Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <div align="left"><a href="http://www.mrpt.org/">Main MRPT website</a> > <b>C++ reference</b> </div> <div align="right"> <a href="index.html"><img border="0" src="mrpt_logo.png" alt="MRPT logo"></a> </div> <!-- Generated by Doxygen 1.7.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <div class="left"> <form id="FSearchBox" action="search.php" method="get"> <img id="MSearchSelect" src="search/mag.png" alt=""/> <input type="text" id="MSearchField" name="query" value="Search" size="20" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)"/> </form> </div><div class="right"></div> </div> </li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <div class="title">RandomGenerators.h</div> </div> </div> <div class="contents"> <a href="_random_generators_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* +---------------------------------------------------------------------------+</span> <a name="l00002"></a>00002 <span class="comment"> | The Mobile Robot Programming Toolkit (MRPT) C++ library |</span> <a name="l00003"></a>00003 <span class="comment"> | |</span> <a name="l00004"></a>00004 <span class="comment"> | http://www.mrpt.org/ |</span> <a name="l00005"></a>00005 <span class="comment"> | |</span> <a name="l00006"></a>00006 <span class="comment"> | Copyright (C) 2005-2011 University of Malaga |</span> <a name="l00007"></a>00007 <span class="comment"> | |</span> <a name="l00008"></a>00008 <span class="comment"> | This software was written by the Machine Perception and Intelligent |</span> <a name="l00009"></a>00009 <span class="comment"> | Robotics Lab, University of Malaga (Spain). |</span> <a name="l00010"></a>00010 <span class="comment"> | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |</span> <a name="l00011"></a>00011 <span class="comment"> | |</span> <a name="l00012"></a>00012 <span class="comment"> | This file is part of the MRPT project. |</span> <a name="l00013"></a>00013 <span class="comment"> | |</span> <a name="l00014"></a>00014 <span class="comment"> | MRPT is free software: you can redistribute it and/or modify |</span> <a name="l00015"></a>00015 <span class="comment"> | it under the terms of the GNU General Public License as published by |</span> <a name="l00016"></a>00016 <span class="comment"> | the Free Software Foundation, either version 3 of the License, or |</span> <a name="l00017"></a>00017 <span class="comment"> | (at your option) any later version. |</span> <a name="l00018"></a>00018 <span class="comment"> | |</span> <a name="l00019"></a>00019 <span class="comment"> | MRPT is distributed in the hope that it will be useful, |</span> <a name="l00020"></a>00020 <span class="comment"> | but WITHOUT ANY WARRANTY; without even the implied warranty of |</span> <a name="l00021"></a>00021 <span class="comment"> | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |</span> <a name="l00022"></a>00022 <span class="comment"> | GNU General Public License for more details. |</span> <a name="l00023"></a>00023 <span class="comment"> | |</span> <a name="l00024"></a>00024 <span class="comment"> | You should have received a copy of the GNU General Public License |</span> <a name="l00025"></a>00025 <span class="comment"> | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |</span> <a name="l00026"></a>00026 <span class="comment"> | |</span> <a name="l00027"></a>00027 <span class="comment"> +---------------------------------------------------------------------------+ */</span> <a name="l00028"></a>00028 <span class="preprocessor">#ifndef RandomGenerator_H</span> <a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#define RandomGenerator_H</span> <a name="l00030"></a>00030 <span class="preprocessor"></span> <a name="l00031"></a>00031 <span class="preprocessor">#include <<a class="code" href="utils__defs_8h.html">mrpt/utils/utils_defs.h</a>></span> <a name="l00032"></a>00032 <span class="preprocessor">#include <<a class="code" href="_c_matrix_template_numeric_8h.html">mrpt/math/CMatrixTemplateNumeric.h</a>></span> <a name="l00033"></a>00033 <span class="preprocessor">#include <<a class="code" href="ops__matrices_8h.html" title="This file implements miscelaneous matrix and matrix/vector operations, plus internal functions in mrp...">mrpt/math/ops_matrices.h</a>></span> <a name="l00034"></a>00034 <a name="l00035"></a>00035 <span class="keyword">namespace </span>mrpt <a name="l00036"></a>00036 {<span class="comment"></span> <a name="l00037"></a>00037 <span class="comment"> /** A namespace of pseudo-random numbers genrators of diferent distributions. The central class in this namespace is mrpt::random::CRandomGenerator</span> <a name="l00038"></a>00038 <span class="comment"> * \ingroup mrpt_base_grp</span> <a name="l00039"></a>00039 <span class="comment"> */</span> <a name="l00040"></a><a class="code" href="namespacemrpt_1_1random.html">00040</a> <span class="keyword">namespace </span>random <a name="l00041"></a>00041 { <a name="l00042"></a>00042 <span class="keyword">using namespace </span>mrpt::utils; <a name="l00043"></a>00043 <span class="keyword">using namespace </span>mrpt::math; <a name="l00044"></a>00044 <span class="comment"></span> <a name="l00045"></a>00045 <span class="comment"> /** A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator.</span> <a name="l00046"></a>00046 <span class="comment"> * The base algorithm for randomness is platform-independent. See http://en.wikipedia.org/wiki/Mersenne_twister</span> <a name="l00047"></a>00047 <span class="comment"> *</span> <a name="l00048"></a>00048 <span class="comment"> * For real thread-safety, each thread must create and use its own instance of this class.</span> <a name="l00049"></a>00049 <span class="comment"> *</span> <a name="l00050"></a>00050 <span class="comment"> * Single-thread programs can use the static object mrpt::random::randomGenerator</span> <a name="l00051"></a>00051 <span class="comment"> * \ingroup mrpt_base_grp</span> <a name="l00052"></a>00052 <span class="comment"> */</span> <a name="l00053"></a>00053 <span class="keyword">class </span><a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> CRandomGenerator <a name="l00054"></a>00054 { <a name="l00055"></a>00055 <span class="keyword">protected</span>:<span class="comment"></span> <a name="l00056"></a>00056 <span class="comment"> /** Data used internally by the MT19937 PRNG algorithm. */</span> <a name="l00057"></a>00057 <span class="keyword">struct </span>TMT19937_data <a name="l00058"></a>00058 { <a name="l00059"></a><a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#a914cc6a74e13b5fd5917a77e03d67104">00059</a> <a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html" title="Data used internally by the MT19937 PRNG algorithm.">TMT19937_data</a>() : index(0), seed_initialized(false) <a name="l00060"></a>00060 {} <a name="l00061"></a><a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#a5414db67ced3de26d91bb867517c808e">00061</a> uint32_t MT[624]; <a name="l00062"></a><a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#a96dd2e919bf936c4ba6bf623f9b281cb">00062</a> uint32_t <a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#a96dd2e919bf936c4ba6bf623f9b281cb">index</a>; <a name="l00063"></a><a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#af223bf8d0249639feb8992ef5088238b">00063</a> <span class="keywordtype">bool</span> <a class="code" href="structmrpt_1_1random_1_1_c_random_generator_1_1_t_m_t19937__data.html#af223bf8d0249639feb8992ef5088238b">seed_initialized</a>; <a name="l00064"></a>00064 } m_MT19937_data; <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordtype">void</span> MT19937_generateNumbers(); <a name="l00067"></a>00067 <span class="keywordtype">void</span> MT19937_initializeGenerator(<span class="keyword">const</span> uint32_t &seed); <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="keyword">public</span>: <a name="l00070"></a>00070 <span class="comment"></span> <a name="l00071"></a>00071 <span class="comment"> /** @name Initialization</span> <a name="l00072"></a>00072 <span class="comment"> @{ */</span> <a name="l00073"></a>00073 <span class="comment"></span> <a name="l00074"></a>00074 <span class="comment"> /** Default constructor: initialize random seed based on current time */</span> <a name="l00075"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a9dd3aa1b262e296450419370123866d1">00075</a> <a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a9dd3aa1b262e296450419370123866d1" title="Default constructor: initialize random seed based on current time.">CRandomGenerator</a>() : m_MT19937_data() { randomize(); } <a name="l00076"></a>00076 <span class="comment"></span> <a name="l00077"></a>00077 <span class="comment"> /** Constructor for providing a custom random seed to initialize the PRNG */</span> <a name="l00078"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a5134b7ac823d79b9f0f555ffdba7bd86">00078</a> <a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a5134b7ac823d79b9f0f555ffdba7bd86" title="Constructor for providing a custom random seed to initialize the PRNG.">CRandomGenerator</a>(<span class="keyword">const</span> uint32_t seed) : m_MT19937_data() { randomize(seed); } <a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="keywordtype">void</span> randomize(<span class="keyword">const</span> uint32_t seed); <span class="comment">//!< Initialize the PRNG from the given random seed</span> <a name="l00081"></a>00081 <span class="comment"></span> <span class="keywordtype">void</span> randomize(); <span class="comment">//!< Randomize the generators, based on current time</span> <a name="l00082"></a>00082 <span class="comment"></span><span class="comment"></span> <a name="l00083"></a>00083 <span class="comment"> /** @} */</span> <a name="l00084"></a>00084 <span class="comment"></span> <a name="l00085"></a>00085 <span class="comment"> /** @name Uniform pdf</span> <a name="l00086"></a>00086 <span class="comment"> @{ */</span> <a name="l00087"></a>00087 <span class="comment"></span> <a name="l00088"></a>00088 <span class="comment"> /** Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.</span> <a name="l00089"></a>00089 <span class="comment"> * See: http://en.wikipedia.org/wiki/Mersenne_twister */</span> <a name="l00090"></a>00090 uint32_t drawUniform32bit(); <a name="l00091"></a>00091 <span class="comment"></span> <a name="l00092"></a>00092 <span class="comment"> /** Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range. */</span> <a name="l00093"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7a4bbaa961f106cd88a872986737a2fb">00093</a> <span class="keywordtype">double</span> <a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7a4bbaa961f106cd88a872986737a2fb" title="Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.">drawUniform</a>( <span class="keyword">const</span> <span class="keywordtype">double</span> Min, <span class="keyword">const</span> <span class="keywordtype">double</span> Max) { <a name="l00094"></a>00094 <span class="keywordflow">return</span> Min + (Max-Min)* drawUniform32bit() * 2.3283064370807973754314699618685e-10; <span class="comment">// 0xFFFFFFFF ^ -1</span> <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <span class="comment"></span> <a name="l00097"></a>00097 <span class="comment"> /** Fills the given matrix with independent, uniformly distributed samples.</span> <a name="l00098"></a>00098 <span class="comment"> * Matrix classes can be CMatrixTemplateNumeric or CMatrixFixedNumeric</span> <a name="l00099"></a>00099 <span class="comment"> * \sa drawUniform</span> <a name="l00100"></a>00100 <span class="comment"> */</span> <a name="l00101"></a>00101 <span class="keyword">template</span> <<span class="keyword">class</span> MAT> <a name="l00102"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a2378ba5fdabf270b2c48726dbcb627cc">00102</a> <span class="keywordtype">void</span> drawUniformMatrix( <a name="l00103"></a>00103 MAT &matrix, <a name="l00104"></a>00104 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_min = 0, <a name="l00105"></a>00105 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_max = 1 ) <a name="l00106"></a>00106 { <a name="l00107"></a>00107 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<matrix.getRowCount();r++) <a name="l00108"></a>00108 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<matrix.getColCount();c++) <a name="l00109"></a>00109 matrix.get_unsafe(r,c) = <span class="keyword">static_cast<</span>typename <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">MAT::value_type</a><span class="keyword">></span>( drawUniform(unif_min,unif_max) ); <a name="l00110"></a>00110 } <a name="l00111"></a>00111 <span class="comment"></span> <a name="l00112"></a>00112 <span class="comment"> /** Fills the given vector with independent, uniformly distributed samples.</span> <a name="l00113"></a>00113 <span class="comment"> * \sa drawUniform</span> <a name="l00114"></a>00114 <span class="comment"> */</span> <a name="l00115"></a>00115 <span class="keyword">template</span> <<span class="keyword">class</span> VEC> <a name="l00116"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a4ef658fc363148b999c6654c07e8c504">00116</a> <span class="keywordtype">void</span> drawUniformVector( <a name="l00117"></a>00117 VEC & v, <a name="l00118"></a>00118 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_min = 0, <a name="l00119"></a>00119 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_max = 1 ) <a name="l00120"></a>00120 { <a name="l00121"></a>00121 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = v.size(); <a name="l00122"></a>00122 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<N;c++) <a name="l00123"></a>00123 v[c] = static_cast<typename VEC::value_type>( drawUniform(unif_min,unif_max) ); <a name="l00124"></a>00124 } <a name="l00125"></a>00125 <span class="comment"></span> <a name="l00126"></a>00126 <span class="comment"> /** @} */</span> <a name="l00127"></a>00127 <span class="comment"></span> <a name="l00128"></a>00128 <span class="comment"> /** @name Normal/Gaussian pdf</span> <a name="l00129"></a>00129 <span class="comment"> @{ */</span> <a name="l00130"></a>00130 <span class="comment"></span> <a name="l00131"></a>00131 <span class="comment"> /** Generate a normalized (mean=0, std=1) normally distributed sample.</span> <a name="l00132"></a>00132 <span class="comment"> * \param likelihood If desired, pass a pointer to a double which will receive the likelihood of the given sample to have been obtained, that is, the value of the normal pdf at the sample value.</span> <a name="l00133"></a>00133 <span class="comment"> */</span> <a name="l00134"></a>00134 <span class="keywordtype">double</span> drawGaussian1D_normalized( <span class="keywordtype">double</span> *likelihood = NULL); <a name="l00135"></a>00135 <span class="comment"></span> <a name="l00136"></a>00136 <span class="comment"> /** Generate a normally distributed pseudo-random number.</span> <a name="l00137"></a>00137 <span class="comment"> * \param mean The mean value of desired normal distribution</span> <a name="l00138"></a>00138 <span class="comment"> * \param std The standard deviation value of desired normal distribution</span> <a name="l00139"></a>00139 <span class="comment"> */</span> <a name="l00140"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a3eab704e20a481c7ebf9884b06726cec">00140</a> <span class="keywordtype">double</span> <a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a3eab704e20a481c7ebf9884b06726cec" title="Generate a normally distributed pseudo-random number.">drawGaussian1D</a>( <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>, <span class="keyword">const</span> <span class="keywordtype">double</span> std ) { <a name="l00141"></a>00141 <span class="keywordflow">return</span> mean+std*drawGaussian1D_normalized(); <a name="l00142"></a>00142 } <a name="l00143"></a>00143 <span class="comment"></span> <a name="l00144"></a>00144 <span class="comment"> /** Fills the given matrix with independent, 1D-normally distributed samples.</span> <a name="l00145"></a>00145 <span class="comment"> * Matrix classes can be CMatrixTemplateNumeric or CMatrixFixedNumeric</span> <a name="l00146"></a>00146 <span class="comment"> * \sa drawGaussian1D</span> <a name="l00147"></a>00147 <span class="comment"> */</span> <a name="l00148"></a>00148 <span class="keyword">template</span> <<span class="keyword">class</span> MAT> <a name="l00149"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a63ed9d57fc7fe778bd50842f49077dda">00149</a> <span class="keywordtype">void</span> drawGaussian1DMatrix( <a name="l00150"></a>00150 MAT &matrix, <a name="l00151"></a>00151 <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = 0, <a name="l00152"></a>00152 <span class="keyword">const</span> <span class="keywordtype">double</span> std = 1 ) <a name="l00153"></a>00153 { <a name="l00154"></a>00154 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<matrix.getRowCount();r++) <a name="l00155"></a>00155 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<matrix.getColCount();c++) <a name="l00156"></a>00156 matrix.get_unsafe(r,c) = <span class="keyword">static_cast<</span>typename <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">MAT::value_type</a><span class="keyword">></span>( drawGaussian1D(<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>,std) ); <a name="l00157"></a>00157 } <a name="l00158"></a>00158 <span class="comment"></span> <a name="l00159"></a>00159 <span class="comment"> /** Generates a random definite-positive matrix of the given size, using the formula C = v*v^t + epsilon*I, with "v" being a vector of gaussian random samples.</span> <a name="l00160"></a>00160 <span class="comment"> */</span> <a name="l00161"></a>00161 <a class="code" href="classmrpt_1_1math_1_1_c_matrix_template_numeric.html" title="A matrix of dynamic size.">CMatrixDouble</a> drawDefinitePositiveMatrix(<span class="keyword">const</span> <span class="keywordtype">size_t</span> dim, <span class="keyword">const</span> <span class="keywordtype">double</span> std_scale = 1.0, <span class="keyword">const</span> <span class="keywordtype">double</span> diagonal_epsilon = 1e-8); <a name="l00162"></a>00162 <span class="comment"></span> <a name="l00163"></a>00163 <span class="comment"> /** Fills the given vector with independent, 1D-normally distributed samples.</span> <a name="l00164"></a>00164 <span class="comment"> * \sa drawGaussian1D</span> <a name="l00165"></a>00165 <span class="comment"> */</span> <a name="l00166"></a>00166 <span class="keyword">template</span> <<span class="keyword">class</span> VEC> <a name="l00167"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#acbe63466320842a1ceb738bae1a062e4">00167</a> <span class="keywordtype">void</span> drawGaussian1DVector( <a name="l00168"></a>00168 VEC & v, <a name="l00169"></a>00169 <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = 0, <a name="l00170"></a>00170 <span class="keyword">const</span> <span class="keywordtype">double</span> std = 1 ) <a name="l00171"></a>00171 { <a name="l00172"></a>00172 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = v.size(); <a name="l00173"></a>00173 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<N;c++) <a name="l00174"></a>00174 v[c] = static_cast<typename VEC::value_type>( drawGaussian1D(<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>,std) ); <a name="l00175"></a>00175 } <a name="l00176"></a>00176 <span class="comment"></span> <a name="l00177"></a>00177 <span class="comment"> /** Generate multidimensional random samples according to a given covariance matrix.</span> <a name="l00178"></a>00178 <span class="comment"> * Mean is assumed to be zero if mean==NULL.</span> <a name="l00179"></a>00179 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00180"></a>00180 <span class="comment"> * \sa drawGaussianMultivariateMany</span> <a name="l00181"></a>00181 <span class="comment"> */</span> <a name="l00182"></a>00182 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00183"></a>00183 <span class="keywordtype">void</span> drawGaussianMultivariate( <a name="l00184"></a>00184 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &out_result, <a name="l00185"></a>00185 <span class="keyword">const</span> <a class="code" href="classmrpt_1_1math_1_1_c_matrix_template_numeric.html" title="A matrix of dynamic size.">CMatrixTemplateNumeric<T></a> &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00186"></a>00186 <span class="keyword">const</span> <a class="code" href="classstd_1_1vector.html">std::vector<T></a>* <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = NULL <a name="l00187"></a>00187 ); <a name="l00188"></a>00188 <a name="l00189"></a>00189 <span class="comment"></span> <a name="l00190"></a>00190 <span class="comment"> /** Generate multidimensional random samples according to a given covariance matrix.</span> <a name="l00191"></a>00191 <span class="comment"> * Mean is assumed to be zero if mean==NULL.</span> <a name="l00192"></a>00192 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00193"></a>00193 <span class="comment"> * \sa drawGaussianMultivariateMany</span> <a name="l00194"></a>00194 <span class="comment"> */</span> <a name="l00195"></a>00195 <span class="keyword">template</span> <<span class="keyword">class</span> VECTORLIKE,<span class="keyword">class</span> COVMATRIX> <a name="l00196"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#ae8d65bbc56b2f33acfcfaae4343b79b9">00196</a> <span class="keywordtype">void</span> drawGaussianMultivariate( <a name="l00197"></a>00197 VECTORLIKE &out_result, <a name="l00198"></a>00198 <span class="keyword">const</span> COVMATRIX &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00199"></a>00199 <span class="keyword">const</span> VECTORLIKE* <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = NULL <a name="l00200"></a>00200 ) <a name="l00201"></a>00201 { <a name="l00202"></a>00202 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = cov.rows(); <a name="l00203"></a>00203 <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(cov.rows()==cov.cols()) <a name="l00204"></a>00204 <span class="keywordflow">if</span> (<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>) <a class="code" href="mrpt__macros_8h.html#a02c6e78d47a0bae723824559846cc673">ASSERT_EQUAL_</a>(<span class="keywordtype">size_t</span>(<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>->size()),N) <a name="l00205"></a>00205 <a name="l00206"></a>00206 <span class="comment">// Compute eigenvalues/eigenvectors of cov:</span> <a name="l00207"></a>00207 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject> eigensolver(cov); <a name="l00208"></a>00208 <a name="l00209"></a>00209 <span class="keyword">typename</span> Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::MatrixType eigVecs = eigensolver.<a class="code" href="class_eigen_1_1_self_adjoint_eigen_solver.html#a64ec384f6d84f4591e86c117a95d79dd" title="Returns the eigenvectors of given matrix.">eigenvectors</a>(); <a name="l00210"></a>00210 <span class="keyword">typename</span> Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::RealVectorType eigVals = eigensolver.<a class="code" href="class_eigen_1_1_self_adjoint_eigen_solver.html#a57d8b8b0cc16f36ea536c135d432d9c6" title="Returns the eigenvalues of given matrix.">eigenvalues</a>(); <a name="l00211"></a>00211 <a name="l00212"></a>00212 <span class="comment">// Scale eigenvectors with eigenvalues:</span> <a name="l00213"></a>00213 <span class="comment">// D.Sqrt(); Z = Z * D; (for each column)</span> <a name="l00214"></a>00214 eigVals = eigVals.array().sqrt(); <a name="l00215"></a>00215 <span class="keywordflow">for</span> (<span class="keyword">typename</span> COVMATRIX::Index i=0;i<eigVecs.cols();i++) <a name="l00216"></a>00216 eigVecs.col(i) *= eigVals[i]; <a name="l00217"></a>00217 <a name="l00218"></a>00218 <span class="comment">// Set size of output vector:</span> <a name="l00219"></a>00219 out_result.assign(N,0); <a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i=0;i<N;i++) <a name="l00222"></a>00222 { <a name="l00223"></a>00223 <span class="keyword">typename</span> COVMATRIX::Scalar rnd = drawGaussian1D_normalized(); <a name="l00224"></a>00224 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> d=0;d<N;d++) <a name="l00225"></a>00225 out_result[d]+= eigVecs.coeff(d,i) * rnd; <a name="l00226"></a>00226 } <a name="l00227"></a>00227 <span class="keywordflow">if</span> (<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>) <a name="l00228"></a>00228 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> d=0;d<N;d++) <a name="l00229"></a>00229 out_result[d]+= (*<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>)[d]; <a name="l00230"></a>00230 } <a name="l00231"></a>00231 <span class="comment"></span> <a name="l00232"></a>00232 <span class="comment"> /** Generate a given number of multidimensional random samples according to a given covariance matrix.</span> <a name="l00233"></a>00233 <span class="comment"> * \param cov The covariance matrix where to draw the samples from.</span> <a name="l00234"></a>00234 <span class="comment"> * \param desiredSamples The number of samples to generate.</span> <a name="l00235"></a>00235 <span class="comment"> * \param ret The output list of samples</span> <a name="l00236"></a>00236 <span class="comment"> * \param mean The mean, or zeros if mean==NULL.</span> <a name="l00237"></a>00237 <span class="comment"> */</span> <a name="l00238"></a>00238 <span class="keyword">template</span> <<span class="keyword">typename</span> VECTOR_OF_VECTORS,<span class="keyword">typename</span> COVMATRIX> <a name="l00239"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a02ebde6aa19bc11b17960e88e2a9e58b">00239</a> <span class="keywordtype">void</span> drawGaussianMultivariateMany( <a name="l00240"></a>00240 VECTOR_OF_VECTORS &ret, <a name="l00241"></a>00241 <span class="keywordtype">size_t</span> desiredSamples, <a name="l00242"></a>00242 <span class="keyword">const</span> COVMATRIX &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00243"></a>00243 <span class="keyword">const</span> <span class="keyword">typename</span> <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">VECTOR_OF_VECTORS::value_type</a> *<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = NULL ) <a name="l00244"></a>00244 { <a name="l00245"></a>00245 <a class="code" href="mrpt__macros_8h.html#a02c6e78d47a0bae723824559846cc673">ASSERT_EQUAL_</a>(cov.cols(),cov.rows()) <a name="l00246"></a>00246 <span class="keywordflow">if</span> (<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>) <a class="code" href="mrpt__macros_8h.html#a02c6e78d47a0bae723824559846cc673">ASSERT_EQUAL_</a>(<span class="keywordtype">size_t</span>(<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>->size()),<span class="keywordtype">size_t</span>(cov.cols())) <a name="l00247"></a>00247 <a name="l00248"></a>00248 <span class="comment">// Compute eigenvalues/eigenvectors of cov:</span> <a name="l00249"></a>00249 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject> eigensolver(cov); <a name="l00250"></a>00250 <a name="l00251"></a>00251 <span class="keyword">typename</span> Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::MatrixType eigVecs = eigensolver.<a class="code" href="class_eigen_1_1_self_adjoint_eigen_solver.html#a64ec384f6d84f4591e86c117a95d79dd" title="Returns the eigenvectors of given matrix.">eigenvectors</a>(); <a name="l00252"></a>00252 <span class="keyword">typename</span> Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>::RealVectorType eigVals = eigensolver.<a class="code" href="class_eigen_1_1_self_adjoint_eigen_solver.html#a57d8b8b0cc16f36ea536c135d432d9c6" title="Returns the eigenvalues of given matrix.">eigenvalues</a>(); <a name="l00253"></a>00253 <a name="l00254"></a>00254 <span class="comment">// Scale eigenvectors with eigenvalues:</span> <a name="l00255"></a>00255 <span class="comment">// D.Sqrt(); Z = Z * D; (for each column)</span> <a name="l00256"></a>00256 eigVals = eigVals.array().sqrt(); <a name="l00257"></a>00257 <span class="keywordflow">for</span> (<span class="keyword">typename</span> COVMATRIX::Index i=0;i<eigVecs.cols();i++) <a name="l00258"></a>00258 eigVecs.col(i) *= eigVals[i]; <a name="l00259"></a>00259 <a name="l00260"></a>00260 <span class="comment">// Set size of output vector:</span> <a name="l00261"></a>00261 ret.resize(desiredSamples); <a name="l00262"></a>00262 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = cov.cols(); <a name="l00263"></a>00263 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> k=0;k<desiredSamples;k++) <a name="l00264"></a>00264 { <a name="l00265"></a>00265 ret[k].assign(N,0); <a name="l00266"></a>00266 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i=0;i<N;i++) <a name="l00267"></a>00267 { <a name="l00268"></a>00268 <span class="keyword">typename</span> COVMATRIX::Scalar rnd = drawGaussian1D_normalized(); <a name="l00269"></a>00269 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> d=0;d<N;d++) <a name="l00270"></a>00270 ret[k][d]+= eigVecs.coeff(d,i) * rnd; <a name="l00271"></a>00271 } <a name="l00272"></a>00272 <span class="keywordflow">if</span> (<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>) <a name="l00273"></a>00273 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> d=0;d<N;d++) <a name="l00274"></a>00274 ret[k][d]+= (*<a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a>)[d]; <a name="l00275"></a>00275 } <a name="l00276"></a>00276 } <a name="l00277"></a>00277 <a name="l00278"></a>00278 <span class="comment"></span> <a name="l00279"></a>00279 <span class="comment"> /** @} */</span> <a name="l00280"></a>00280 <a name="l00281"></a>00281 <span class="comment"></span> <a name="l00282"></a>00282 <span class="comment"> /** @name Miscellaneous</span> <a name="l00283"></a>00283 <span class="comment"> @{ */</span> <a name="l00284"></a>00284 <span class="comment"></span> <a name="l00285"></a>00285 <span class="comment"> /** Returns a random permutation of a vector: all the elements of the input vector are in the output but at random positions.</span> <a name="l00286"></a>00286 <span class="comment"> */</span> <a name="l00287"></a>00287 <span class="keyword">template</span> <<span class="keyword">class</span> VEC> <a name="l00288"></a><a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#ac0f4a363fb29bf340e64ab9af6231d7b">00288</a> <span class="keywordtype">void</span> permuteVector(<span class="keyword">const</span> VEC &in_vector, VEC &out_result) <a name="l00289"></a>00289 { <a name="l00290"></a>00290 out_result = in_vector; <a name="l00291"></a>00291 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = out_result.size(); <a name="l00292"></a>00292 <span class="keywordflow">if</span> (N>1) <a name="l00293"></a>00293 std::random_shuffle( &out_result[0],&out_result[N-1] ); <a name="l00294"></a>00294 } <a name="l00295"></a>00295 <span class="comment"></span> <a name="l00296"></a>00296 <span class="comment"> /** @} */</span> <a name="l00297"></a>00297 <a name="l00298"></a>00298 }; <span class="comment">// end of CRandomGenerator --------------------------------------------------------------</span> <a name="l00299"></a>00299 <a name="l00300"></a>00300 <span class="comment"></span> <a name="l00301"></a>00301 <span class="comment"> /** A static instance of a CRandomGenerator class, for use in single-thread applications */</span> <a name="l00302"></a>00302 <span class="keyword">extern</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> CRandomGenerator <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>; <a name="l00303"></a>00303 <a name="l00304"></a>00304 <span class="comment"></span> <a name="l00305"></a>00305 <span class="comment"> /** A random number generator for usage in STL algorithms expecting a function like this (eg, random_shuffle):</span> <a name="l00306"></a>00306 <span class="comment"> */</span> <a name="l00307"></a><a class="code" href="namespacemrpt_1_1random.html#a4b8c2d5323417f5760fb2ed586b07d0c">00307</a> <span class="keyword">inline</span> ptrdiff_t <a class="code" href="namespacemrpt_1_1random.html#a4b8c2d5323417f5760fb2ed586b07d0c" title="A random number generator for usage in STL algorithms expecting a function like this (eg...">random_generator_for_STL</a>(ptrdiff_t i) <a name="l00308"></a>00308 { <a name="l00309"></a>00309 <span class="keywordflow">return</span> <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#aede40e77053b8b4350fd786f1683e260" title="Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.">drawUniform32bit</a>() % i; <a name="l00310"></a>00310 } <a name="l00311"></a>00311 <span class="comment"></span> <a name="l00312"></a>00312 <span class="comment"> /** Fills the given matrix with independent, uniformly distributed samples.</span> <a name="l00313"></a>00313 <span class="comment"> * Matrix classes can be CMatrixTemplateNumeric or CMatrixFixedNumeric</span> <a name="l00314"></a>00314 <span class="comment"> * \sa matrixRandomNormal</span> <a name="l00315"></a>00315 <span class="comment"> */</span> <a name="l00316"></a>00316 <span class="keyword">template</span> <<span class="keyword">class</span> MAT> <a name="l00317"></a><a class="code" href="namespacemrpt_1_1random.html#ad7c4e3d36decc3132685f45933086b3a">00317</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#ad7c4e3d36decc3132685f45933086b3a" title="Fills the given matrix with independent, uniformly distributed samples.">matrixRandomUni</a>( <a name="l00318"></a>00318 MAT &matrix, <a name="l00319"></a>00319 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_min = 0, <a name="l00320"></a>00320 <span class="keyword">const</span> <span class="keywordtype">double</span> unif_max = 1 ) <a name="l00321"></a>00321 { <a name="l00322"></a>00322 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<matrix.getRowCount();r++) <a name="l00323"></a>00323 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<matrix.getColCount();c++) <a name="l00324"></a>00324 matrix.get_unsafe(r,c) = <span class="keyword">static_cast<</span>typename <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">MAT::value_type</a><span class="keyword">></span>( <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7a4bbaa961f106cd88a872986737a2fb" title="Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.">drawUniform</a>(unif_min,unif_max) ); <a name="l00325"></a>00325 } <a name="l00326"></a>00326 <span class="comment"></span> <a name="l00327"></a>00327 <span class="comment"> /** Fills the given matrix with independent, uniformly distributed samples.</span> <a name="l00328"></a>00328 <span class="comment"> * \sa vectorRandomNormal</span> <a name="l00329"></a>00329 <span class="comment"> */</span> <a name="l00330"></a>00330 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00331"></a><a class="code" href="namespacemrpt_1_1random.html#a62e82d56289ffa599233d5cd504699c3">00331</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a62e82d56289ffa599233d5cd504699c3" title="Fills the given matrix with independent, uniformly distributed samples.">vectorRandomUni</a>( <a name="l00332"></a>00332 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &v_out, <a name="l00333"></a>00333 <span class="keyword">const</span> T& unif_min = 0, <a name="l00334"></a>00334 <span class="keyword">const</span> T& unif_max = 1 ) <a name="l00335"></a>00335 { <a name="l00336"></a>00336 <span class="keywordtype">size_t</span> n = v_out.size(); <a name="l00337"></a>00337 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<n;r++) <a name="l00338"></a>00338 v_out[r] = <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7a4bbaa961f106cd88a872986737a2fb" title="Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.">drawUniform</a>(unif_min,unif_max); <a name="l00339"></a>00339 } <a name="l00340"></a>00340 <span class="comment"></span> <a name="l00341"></a>00341 <span class="comment"> /** Fills the given matrix with independent, normally distributed samples.</span> <a name="l00342"></a>00342 <span class="comment"> * Matrix classes can be CMatrixTemplateNumeric or CMatrixFixedNumeric</span> <a name="l00343"></a>00343 <span class="comment"> * \sa matrixRandomUni</span> <a name="l00344"></a>00344 <span class="comment"> */</span> <a name="l00345"></a>00345 <span class="keyword">template</span> <<span class="keyword">class</span> MAT> <a name="l00346"></a><a class="code" href="namespacemrpt_1_1random.html#a6d3e634a522443866723c1a5585a35f1">00346</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a6d3e634a522443866723c1a5585a35f1" title="Fills the given matrix with independent, normally distributed samples.">matrixRandomNormal</a>( <a name="l00347"></a>00347 MAT &matrix, <a name="l00348"></a>00348 <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = 0, <a name="l00349"></a>00349 <span class="keyword">const</span> <span class="keywordtype">double</span> std = 1 ) <a name="l00350"></a>00350 { <a name="l00351"></a>00351 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<matrix.getRowCount();r++) <a name="l00352"></a>00352 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0;c<matrix.getColCount();c++) <a name="l00353"></a>00353 matrix.get_unsafe(r,c) = <span class="keyword">static_cast<</span>typename <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">MAT::value_type</a><span class="keyword">></span>( <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> + std*<a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a174c76f98b4213166fb87b577fdf2aaa" title="Generate a normalized (mean=0, std=1) normally distributed sample.">drawGaussian1D_normalized</a>() ); <a name="l00354"></a>00354 } <a name="l00355"></a>00355 <span class="comment"></span> <a name="l00356"></a>00356 <span class="comment"> /** Generates a random vector with independent, normally distributed samples.</span> <a name="l00357"></a>00357 <span class="comment"> * \sa matrixRandomUni</span> <a name="l00358"></a>00358 <span class="comment"> */</span> <a name="l00359"></a>00359 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00360"></a><a class="code" href="namespacemrpt_1_1random.html#a60f07d06c183298a085dec3534c50338">00360</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a60f07d06c183298a085dec3534c50338" title="Generates a random vector with independent, normally distributed samples.">vectorRandomNormal</a>( <a name="l00361"></a>00361 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &v_out, <a name="l00362"></a>00362 <span class="keyword">const</span> T& <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> = 0, <a name="l00363"></a>00363 <span class="keyword">const</span> T& std = 1 ) <a name="l00364"></a>00364 { <a name="l00365"></a>00365 <span class="keywordtype">size_t</span> n = v_out.size(); <a name="l00366"></a>00366 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0;r<n;r++) <a name="l00367"></a>00367 v_out[r] = <a class="code" href="eigen__plugins_8h.html#a378ef7ee1218e4aa29b595c6e0f8ee4a" title="Computes the mean of the entire matrix.">mean</a> + std*<a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a174c76f98b4213166fb87b577fdf2aaa" title="Generate a normalized (mean=0, std=1) normally distributed sample.">drawGaussian1D_normalized</a>(); <a name="l00368"></a>00368 } <a name="l00369"></a>00369 <span class="comment"></span> <a name="l00370"></a>00370 <span class="comment"> /** Randomize the generators.</span> <a name="l00371"></a>00371 <span class="comment"> * A seed can be providen, or a current-time based seed can be used (default)</span> <a name="l00372"></a>00372 <span class="comment"> */</span> <a name="l00373"></a><a class="code" href="namespacemrpt_1_1random.html#a596e1d3fa80d38575ae5b4e97da13094">00373</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a596e1d3fa80d38575ae5b4e97da13094" title="Randomize the generators.">Randomize</a>(<span class="keyword">const</span> uint32_t seed) { <a name="l00374"></a>00374 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a231ef5bd9387714debad4914e5da6985" title="Initialize the PRNG from the given random seed.">randomize</a>(seed); <a name="l00375"></a>00375 } <a name="l00376"></a><a class="code" href="namespacemrpt_1_1random.html#a1e4696dacdce6473e9b5439de94dc97f">00376</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a596e1d3fa80d38575ae5b4e97da13094" title="Randomize the generators.">Randomize</a>() { <a name="l00377"></a>00377 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a231ef5bd9387714debad4914e5da6985" title="Initialize the PRNG from the given random seed.">randomize</a>(); <a name="l00378"></a>00378 } <a name="l00379"></a>00379 <span class="comment"></span> <a name="l00380"></a>00380 <span class="comment"> /** Returns a random permutation of a vector: all the elements of the input vector are in the output but at random positions.</span> <a name="l00381"></a>00381 <span class="comment"> */</span> <a name="l00382"></a>00382 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00383"></a><a class="code" href="namespacemrpt_1_1random.html#ae505099440683685a98a33761d8b1527">00383</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#ae505099440683685a98a33761d8b1527" title="Returns a random permutation of a vector: all the elements of the input vector are in the output but ...">randomPermutation</a>( <a name="l00384"></a>00384 <span class="keyword">const</span> <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &in_vector, <a name="l00385"></a>00385 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &out_result) <a name="l00386"></a>00386 { <a name="l00387"></a>00387 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#ac0f4a363fb29bf340e64ab9af6231d7b" title="Returns a random permutation of a vector: all the elements of the input vector are in the output but ...">permuteVector</a>(in_vector,out_result); <a name="l00388"></a>00388 } <a name="l00389"></a>00389 <a name="l00390"></a>00390 <span class="comment"></span> <a name="l00391"></a>00391 <span class="comment"> /** Generate multidimensional random samples according to a given covariance matrix.</span> <a name="l00392"></a>00392 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00393"></a>00393 <span class="comment"> * \sa randomNormalMultiDimensionalMany</span> <a name="l00394"></a>00394 <span class="comment"> */</span> <a name="l00395"></a>00395 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00396"></a><a class="code" href="namespacemrpt_1_1random.html#afb45a0dc6818892974defd932c504028">00396</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#afb45a0dc6818892974defd932c504028" title="Generate multidimensional random samples according to a given covariance matrix.">randomNormalMultiDimensional</a>( <a name="l00397"></a>00397 <span class="keyword">const</span> <a class="code" href="classmrpt_1_1math_1_1_c_matrix_template_numeric.html" title="A matrix of dynamic size.">CMatrixTemplateNumeric<T></a> &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00398"></a>00398 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &out_result) <a name="l00399"></a>00399 { <a name="l00400"></a>00400 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7e74a5bba69f5a49b40ed38f32499c9d" title="Generate multidimensional random samples according to a given covariance matrix.">drawGaussianMultivariate</a>(out_result,cov); <a name="l00401"></a>00401 } <a name="l00402"></a>00402 <span class="comment"></span> <a name="l00403"></a>00403 <span class="comment"> /** Generate a given number of multidimensional random samples according to a given covariance matrix.</span> <a name="l00404"></a>00404 <span class="comment"> * \param cov The covariance matrix where to draw the samples from.</span> <a name="l00405"></a>00405 <span class="comment"> * \param desiredSamples The number of samples to generate.</span> <a name="l00406"></a>00406 <span class="comment"> * \param samplesLikelihoods If desired, set to a valid pointer to a vector, where it will be stored the likelihoods of having obtained each sample: the product of the gaussian-pdf for each independent variable.</span> <a name="l00407"></a>00407 <span class="comment"> * \param ret The output list of samples</span> <a name="l00408"></a>00408 <span class="comment"> *</span> <a name="l00409"></a>00409 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00410"></a>00410 <span class="comment"> *</span> <a name="l00411"></a>00411 <span class="comment"> * \sa randomNormalMultiDimensional</span> <a name="l00412"></a>00412 <span class="comment"> */</span> <a name="l00413"></a>00413 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00414"></a><a class="code" href="namespacemrpt_1_1random.html#a5b1d5e7ef7ee69828efa52b408648608">00414</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a5b1d5e7ef7ee69828efa52b408648608" title="Generate a given number of multidimensional random samples according to a given covariance matrix...">randomNormalMultiDimensionalMany</a>( <a name="l00415"></a>00415 <span class="keyword">const</span> <a class="code" href="classmrpt_1_1math_1_1_c_matrix_template_numeric.html" title="A matrix of dynamic size.">CMatrixTemplateNumeric<T></a> &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00416"></a>00416 <span class="keywordtype">size_t</span> desiredSamples, <a name="l00417"></a>00417 <a class="code" href="classstd_1_1vector.html" title="STL class.">std::vector</a>< <a class="code" href="classstd_1_1vector.html">std::vector<T></a> > &ret, <a name="l00418"></a>00418 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> *samplesLikelihoods = NULL) <a name="l00419"></a>00419 { <a name="l00420"></a>00420 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a02ebde6aa19bc11b17960e88e2a9e58b" title="Generate a given number of multidimensional random samples according to a given covariance matrix...">drawGaussianMultivariateMany</a>(ret,desiredSamples,cov,<span class="keyword">static_cast<</span><span class="keyword">const </span><a class="code" href="classstd_1_1vector.html" title="STL class.">std::vector</a><T<span class="keyword">></span>*>(NULL),samplesLikelihoods); <a name="l00421"></a>00421 } <a name="l00422"></a>00422 <span class="comment"></span> <a name="l00423"></a>00423 <span class="comment"> /** Generate multidimensional random samples according to a given covariance matrix.</span> <a name="l00424"></a>00424 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00425"></a>00425 <span class="comment"> * \sa randomNormalMultiDimensional</span> <a name="l00426"></a>00426 <span class="comment"> */</span> <a name="l00427"></a>00427 <span class="keyword">template</span> <<span class="keyword">typename</span> T,<span class="keywordtype">size_t</span> N> <a name="l00428"></a><a class="code" href="namespacemrpt_1_1random.html#a7b1e3e134cd17409849dd7fb62aee489">00428</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#a5b1d5e7ef7ee69828efa52b408648608" title="Generate a given number of multidimensional random samples according to a given covariance matrix...">randomNormalMultiDimensionalMany</a>( <a name="l00429"></a>00429 <span class="keyword">const</span> <a class="code" href="classmrpt_1_1math_1_1_c_matrix_fixed_numeric.html" title="A numeric matrix of compile-time fixed size.">CMatrixFixedNumeric<T,N,N></a> &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00430"></a>00430 <span class="keywordtype">size_t</span> desiredSamples, <a name="l00431"></a>00431 <a class="code" href="classstd_1_1vector.html" title="STL class.">std::vector</a>< <a class="code" href="classstd_1_1vector.html">std::vector<T></a> > &ret ) <a name="l00432"></a>00432 { <a name="l00433"></a>00433 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a02ebde6aa19bc11b17960e88e2a9e58b" title="Generate a given number of multidimensional random samples according to a given covariance matrix...">drawGaussianMultivariateMany</a>(ret,desiredSamples,cov); <a name="l00434"></a>00434 } <a name="l00435"></a>00435 <span class="comment"></span> <a name="l00436"></a>00436 <span class="comment"> /** Generate multidimensional random samples according to a given covariance matrix.</span> <a name="l00437"></a>00437 <span class="comment"> * \exception std::exception On invalid covariance matrix</span> <a name="l00438"></a>00438 <span class="comment"> * \sa randomNormalMultiDimensionalMany</span> <a name="l00439"></a>00439 <span class="comment"> */</span> <a name="l00440"></a>00440 <span class="keyword">template</span> <<span class="keyword">typename</span> T,<span class="keywordtype">size_t</span> N> <a name="l00441"></a><a class="code" href="namespacemrpt_1_1random.html#ae07f23edde6d47ab9c2dba434e4b1d33">00441</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1random.html#afb45a0dc6818892974defd932c504028" title="Generate multidimensional random samples according to a given covariance matrix.">randomNormalMultiDimensional</a>( <a name="l00442"></a>00442 <span class="keyword">const</span> <a class="code" href="classmrpt_1_1math_1_1_c_matrix_fixed_numeric.html" title="A numeric matrix of compile-time fixed size.">CMatrixFixedNumeric<T,N,N></a> &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00443"></a>00443 <a class="code" href="classstd_1_1vector.html">std::vector<T></a> &out_result) <a name="l00444"></a>00444 { <a name="l00445"></a>00445 <a class="code" href="namespacemrpt_1_1random.html#a4743bfa8fcb282b6f5d66395ccabaa73" title="A static instance of a CRandomGenerator class, for use in single-thread applications.">randomGenerator</a>.<a class="code" href="classmrpt_1_1random_1_1_c_random_generator.html#a7e74a5bba69f5a49b40ed38f32499c9d" title="Generate multidimensional random samples according to a given covariance matrix.">drawGaussianMultivariate</a>(out_result,cov); <a name="l00446"></a>00446 } <a name="l00447"></a>00447 <a name="l00448"></a>00448 <a name="l00449"></a>00449 }<span class="comment">// End of namespace</span> <a name="l00450"></a>00450 <a name="l00451"></a>00451 } <span class="comment">// End of namespace</span> <a name="l00452"></a>00452 <a name="l00453"></a>00453 <span class="preprocessor">#endif</span> </pre></div></div> </div> <br><hr><br> <table border="0" width="100%"> <tr> <td> Page generated by <a href="http://www.doxygen.org" target="_blank">Doxygen 1.7.5</a> for MRPT 0.9.5 SVN: at Sun Sep 25 17:20:18 UTC 2011</td><td></td> <td width="100"> </td> <td width="150"> </td></tr> </table> </body></html>