TheAlgorithms-C/d0/d46/kohonen__som__trace_8c.html
2020-07-16 20:36:47 +00:00

674 lines
65 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: machine_learning/kohonen_som_trace.c File Reference</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="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</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">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('d0/d46/kohonen__som__trace_8c.html','../../'); initResizable(); });
/* @license-end */
</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)">
</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="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">kohonen_som_trace.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Self-organizing_map">Kohonen self organizing map</a> (data tracing)
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;math.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for kohonen_som_trace.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/dfd/kohonen__som__trace_8c__incl.svg" width="331" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a525335710b53cb064ca56b936120431e">_USE_MATH_DEFINES</a></td></tr>
<tr class="memdesc:a525335710b53cb064ca56b936120431e"><td class="mdescLeft">&#160;</td><td class="mdescRight">required for MS Visual C <br /></td></tr>
<tr class="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaffe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top">
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(a, b)&#160;&#160;&#160;(((a) &gt; (b)) ? (a) : (b))</td></tr>
<tr class="memdesc:gaffe776513b24d84b39af8ab0930fef7f"><td class="mdescLeft">&#160;</td><td class="mdescRight">shorthand for maximum value <br /></td></tr>
<tr class="separator:gaffe776513b24d84b39af8ab0930fef7f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gac6afabdc09a49a433ee19d8a9486056d"><td class="memItemLeft" align="right" valign="top">
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(a, b)&#160;&#160;&#160;(((a) &lt; (b)) ? (a) : (b))</td></tr>
<tr class="memdesc:gac6afabdc09a49a433ee19d8a9486056d"><td class="mdescLeft">&#160;</td><td class="mdescRight">shorthand for minimum value <br /></td></tr>
<tr class="separator:gac6afabdc09a49a433ee19d8a9486056d"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gaf5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
<tr class="memdesc:gaf5ce14f026d6d231bef29161bac2b485"><td class="mdescLeft">&#160;</td><td class="mdescRight">Helper function to generate a random number in a given interval. <a href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">More...</a><br /></td></tr>
<tr class="separator:gaf5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga7b84b14e60f47812b581d1f93057c85a"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
<tr class="memdesc:ga7b84b14e60f47812b581d1f93057c85a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Save a given n-dimensional data martix to file. <a href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">More...</a><br /></td></tr>
<tr class="separator:ga7b84b14e60f47812b581d1f93057c85a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga4a57a413a3cef286a7da6d4666575586"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">kohonen_get_min_1d</a> (double const *X, int N, double *val, int *idx)</td></tr>
<tr class="memdesc:ga4a57a413a3cef286a7da6d4666575586"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get minimum value and index of the value in a vector. <a href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">More...</a><br /></td></tr>
<tr class="separator:ga4a57a413a3cef286a7da6d4666575586"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gae334493a0917a24736fe5ba82aa6f81f"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">kohonen_update_weights</a> (double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</td></tr>
<tr class="memdesc:gae334493a0917a24736fe5ba82aa6f81f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Update weights of the SOM using Kohonen algorithm. <a href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">More...</a><br /></td></tr>
<tr class="separator:gae334493a0917a24736fe5ba82aa6f81f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a> (double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
<tr class="memdesc:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="mdescLeft">&#160;</td><td class="mdescRight">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. <a href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">More...</a><br /></td></tr>
<tr class="separator:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a107f00650b8041f77767927073ddddb8"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:a107f00650b8041f77767927073ddddb8"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">More...</a><br /></td></tr>
<tr class="separator:a107f00650b8041f77767927073ddddb8"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
<tr class="memdesc:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">More...</a><br /></td></tr>
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa2246f940155472084ee461f3685d614"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:aa2246f940155472084ee461f3685d614"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a>. <a href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">More...</a><br /></td></tr>
<tr class="separator:aa2246f940155472084ee461f3685d614"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0283886819c7c140a023582b7269e2d0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
<tr class="memdesc:a0283886819c7c140a023582b7269e2d0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a> and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a0283886819c7c140a023582b7269e2d0">More...</a><br /></td></tr>
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a41ae16442e3e5b891a58d2e5932a2cd0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:a41ae16442e3e5b891a58d2e5932a2cd0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <a href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">More...</a><br /></td></tr>
<tr class="separator:a41ae16442e3e5b891a58d2e5932a2cd0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a6d0455dd5c30adda100e95f0423c786e"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a6d0455dd5c30adda100e95f0423c786e">test3</a> ()</td></tr>
<tr class="memdesc:a6d0455dd5c30adda100e95f0423c786e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed in six clusters in 3D space. <a href="../../d0/d46/kohonen__som__trace_8c.html#a6d0455dd5c30adda100e95f0423c786e">More...</a><br /></td></tr>
<tr class="separator:a6d0455dd5c30adda100e95f0423c786e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2256c10b16edba377b64a44b6c656908"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a> (clock_t start_t, clock_t end_t)</td></tr>
<tr class="memdesc:a2256c10b16edba377b64a44b6c656908"><td class="mdescLeft">&#160;</td><td class="mdescRight">Convert clock cycle difference to time in seconds. <a href="../../d0/d46/kohonen__som__trace_8c.html#a2256c10b16edba377b64a44b6c656908">More...</a><br /></td></tr>
<tr class="separator:a2256c10b16edba377b64a44b6c656908"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top"><a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Self-organizing_map">Kohonen self organizing map</a> (data tracing) </p>
<p>This example implements a powerful self organizing map algorithm. The algorithm creates a connected network of weights that closely follows the given data points. This this creates a chain of nodes that resembles the given input shape. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a> </dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html" title="Kohonen self organizing map (topological map)">kohonen_som_topology.c</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a2256c10b16edba377b64a44b6c656908"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a2256c10b16edba377b64a44b6c656908">&#9670;&nbsp;</a></span>get_clock_diff()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double get_clock_diff </td>
<td>(</td>
<td class="paramtype">clock_t&#160;</td>
<td class="paramname"><em>start_t</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">clock_t&#160;</td>
<td class="paramname"><em>end_t</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Convert clock cycle difference to time in seconds. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">start_t</td><td>start clock </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">end_t</td><td>end clock </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>time difference in seconds </dd></dl>
<div class="fragment"><div class="line"><a name="l00512"></a><span class="lineno"> 512</span>&#160;{</div>
<div class="line"><a name="l00513"></a><span class="lineno"> 513</span>&#160; <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)(end_t - start_t) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC;</div>
<div class="line"><a name="l00514"></a><span class="lineno"> 514</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a1440a7779ac56f47a3f355ce4a8c7da0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1440a7779ac56f47a3f355ce4a8c7da0">&#9670;&nbsp;</a></span>test1()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test1 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Test that creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. </p>
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> files are created to validate the execution:</p><ul>
<li><code>test1.csv</code>: random test samples points with a circular pattern</li>
<li><code>w11.csv</code>: initial random map</li>
<li><code>w12.csv</code>: trained SOM map</li>
</ul>
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator &#39;,&#39;</div>
<div class="line">plot &quot;test1.csv&quot; title &quot;original&quot;, \</div>
<div class="line"> &quot;w11.csv&quot; title &quot;w1&quot;, \</div>
<div class="line"> &quot;w12.csv&quot; title &quot;w2&quot;</div>
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test1.svg" alt="Sample execution
output" style="pointer-events: none;" class="inline"/> </p>
<div class="fragment"><div class="line"><a name="l00262"></a><span class="lineno"> 262</span>&#160;{</div>
<div class="line"><a name="l00263"></a><span class="lineno"> 263</span>&#160; <span class="keywordtype">int</span> j, N = 500;</div>
<div class="line"><a name="l00264"></a><span class="lineno"> 264</span>&#160; <span class="keywordtype">int</span> features = 2;</div>
<div class="line"><a name="l00265"></a><span class="lineno"> 265</span>&#160; <span class="keywordtype">int</span> num_out = 50;</div>
<div class="line"><a name="l00266"></a><span class="lineno"> 266</span>&#160; </div>
<div class="line"><a name="l00267"></a><span class="lineno"> 267</span>&#160; <span class="comment">// 2D space, hence size = number of rows * 2</span></div>
<div class="line"><a name="l00268"></a><span class="lineno"> 268</span>&#160; <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00269"></a><span class="lineno"> 269</span>&#160; </div>
<div class="line"><a name="l00270"></a><span class="lineno"> 270</span>&#160; <span class="comment">// number of clusters nodes * 2</span></div>
<div class="line"><a name="l00271"></a><span class="lineno"> 271</span>&#160; <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00272"></a><span class="lineno"> 272</span>&#160; </div>
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++) <span class="comment">// loop till max(N, num_out)</span></div>
<div class="line"><a name="l00274"></a><span class="lineno"> 274</span>&#160; {</div>
<div class="line"><a name="l00275"></a><span class="lineno"> 275</span>&#160; <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span>&#160; X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out) <span class="comment">// only add new arrays if i &lt; num_out</span></div>
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>&#160; {</div>
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>&#160; W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>&#160; }</div>
<div class="line"><a name="l00286"></a><span class="lineno"> 286</span>&#160; }</div>
<div class="line"><a name="l00287"></a><span class="lineno"> 287</span>&#160; </div>
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a>(X, N); <span class="comment">// create test data around circumference of a circle</span></div>
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test1.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w11.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, N, features, num_out, 0.1); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00293"></a><span class="lineno"> 293</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w12.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00294"></a><span class="lineno"> 294</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00295"></a><span class="lineno"> 295</span>&#160; </div>
<div class="line"><a name="l00296"></a><span class="lineno"> 296</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++)</div>
<div class="line"><a name="l00297"></a><span class="lineno"> 297</span>&#160; {</div>
<div class="line"><a name="l00298"></a><span class="lineno"> 298</span>&#160; <span class="keywordflow">if</span> (i &lt; N)</div>
<div class="line"><a name="l00299"></a><span class="lineno"> 299</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00300"></a><span class="lineno"> 300</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00301"></a><span class="lineno"> 301</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00302"></a><span class="lineno"> 302</span>&#160; }</div>
<div class="line"><a name="l00303"></a><span class="lineno"> 303</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg" width="663" height="190"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0283886819c7c140a023582b7269e2d0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0283886819c7c140a023582b7269e2d0">&#9670;&nbsp;</a></span>test2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test2 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Test that creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a> and trains an SOM that finds that circular pattern. </p>
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> files are created to validate the execution:</p><ul>
<li><code>test2.csv</code>: random test samples points with a lamniscate pattern</li>
<li><code>w21.csv</code>: initial random map</li>
<li><code>w22.csv</code>: trained SOM map</li>
</ul>
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator &#39;,&#39;</div>
<div class="line">plot &quot;test2.csv&quot; title &quot;original&quot;, \</div>
<div class="line"> &quot;w21.csv&quot; title &quot;w1&quot;, \</div>
<div class="line"> &quot;w22.csv&quot; title &quot;w2&quot;</div>
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test2.svg" alt="Sample execution
output" style="pointer-events: none;" class="inline"/> </p>
<div class="fragment"><div class="line"><a name="l00359"></a><span class="lineno"> 359</span>&#160;{</div>
<div class="line"><a name="l00360"></a><span class="lineno"> 360</span>&#160; <span class="keywordtype">int</span> j, N = 500;</div>
<div class="line"><a name="l00361"></a><span class="lineno"> 361</span>&#160; <span class="keywordtype">int</span> features = 2;</div>
<div class="line"><a name="l00362"></a><span class="lineno"> 362</span>&#160; <span class="keywordtype">int</span> num_out = 20;</div>
<div class="line"><a name="l00363"></a><span class="lineno"> 363</span>&#160; <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00364"></a><span class="lineno"> 364</span>&#160; <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00365"></a><span class="lineno"> 365</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++)</div>
<div class="line"><a name="l00366"></a><span class="lineno"> 366</span>&#160; {</div>
<div class="line"><a name="l00367"></a><span class="lineno"> 367</span>&#160; <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00368"></a><span class="lineno"> 368</span>&#160; X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00369"></a><span class="lineno"> 369</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out) <span class="comment">// only add new arrays if i &lt; num_out</span></div>
<div class="line"><a name="l00370"></a><span class="lineno"> 370</span>&#160; {</div>
<div class="line"><a name="l00371"></a><span class="lineno"> 371</span>&#160; W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00372"></a><span class="lineno"> 372</span>&#160; </div>
<div class="line"><a name="l00373"></a><span class="lineno"> 373</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00374"></a><span class="lineno"> 374</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00375"></a><span class="lineno"> 375</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00376"></a><span class="lineno"> 376</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00377"></a><span class="lineno"> 377</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
<div class="line"><a name="l00378"></a><span class="lineno"> 378</span>&#160; }</div>
<div class="line"><a name="l00379"></a><span class="lineno"> 379</span>&#160; }</div>
<div class="line"><a name="l00380"></a><span class="lineno"> 380</span>&#160; </div>
<div class="line"><a name="l00381"></a><span class="lineno"> 381</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a>(X, N); <span class="comment">// create test data around the lamniscate</span></div>
<div class="line"><a name="l00382"></a><span class="lineno"> 382</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test2.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00383"></a><span class="lineno"> 383</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w21.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00384"></a><span class="lineno"> 384</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00385"></a><span class="lineno"> 385</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, N, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00386"></a><span class="lineno"> 386</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w22.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00387"></a><span class="lineno"> 387</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00388"></a><span class="lineno"> 388</span>&#160; </div>
<div class="line"><a name="l00389"></a><span class="lineno"> 389</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++)</div>
<div class="line"><a name="l00390"></a><span class="lineno"> 390</span>&#160; {</div>
<div class="line"><a name="l00391"></a><span class="lineno"> 391</span>&#160; <span class="keywordflow">if</span> (i &lt; N)</div>
<div class="line"><a name="l00392"></a><span class="lineno"> 392</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00393"></a><span class="lineno"> 393</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00394"></a><span class="lineno"> 394</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00395"></a><span class="lineno"> 395</span>&#160; }</div>
<div class="line"><a name="l00396"></a><span class="lineno"> 396</span>&#160; free(X);</div>
<div class="line"><a name="l00397"></a><span class="lineno"> 397</span>&#160; free(W);</div>
<div class="line"><a name="l00398"></a><span class="lineno"> 398</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="663" height="190"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a6d0455dd5c30adda100e95f0423c786e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6d0455dd5c30adda100e95f0423c786e">&#9670;&nbsp;</a></span>test3()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test3 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Test that creates a random set of points distributed in six clusters in 3D space. </p>
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> files are created to validate the execution:</p><ul>
<li><code>test3.csv</code>: random test samples points with a circular pattern</li>
<li><code>w31.csv</code>: initial random map</li>
<li><code>w32.csv</code>: trained SOM map</li>
</ul>
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator &#39;,&#39;</div>
<div class="line">plot &quot;test3.csv&quot; title &quot;original&quot;, \</div>
<div class="line"> &quot;w31.csv&quot; title &quot;w1&quot;, \</div>
<div class="line"> &quot;w32.csv&quot; title &quot;w2&quot;</div>
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test3.svg" alt="Sample execution
output" style="pointer-events: none;" class="inline"/> </p>
<div class="fragment"><div class="line"><a name="l00463"></a><span class="lineno"> 463</span>&#160;{</div>
<div class="line"><a name="l00464"></a><span class="lineno"> 464</span>&#160; <span class="keywordtype">int</span> j, N = 200;</div>
<div class="line"><a name="l00465"></a><span class="lineno"> 465</span>&#160; <span class="keywordtype">int</span> features = 3;</div>
<div class="line"><a name="l00466"></a><span class="lineno"> 466</span>&#160; <span class="keywordtype">int</span> num_out = 20;</div>
<div class="line"><a name="l00467"></a><span class="lineno"> 467</span>&#160; <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00468"></a><span class="lineno"> 468</span>&#160; <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00469"></a><span class="lineno"> 469</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++)</div>
<div class="line"><a name="l00470"></a><span class="lineno"> 470</span>&#160; {</div>
<div class="line"><a name="l00471"></a><span class="lineno"> 471</span>&#160; <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00472"></a><span class="lineno"> 472</span>&#160; X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00473"></a><span class="lineno"> 473</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out) <span class="comment">// only add new arrays if i &lt; num_out</span></div>
<div class="line"><a name="l00474"></a><span class="lineno"> 474</span>&#160; {</div>
<div class="line"><a name="l00475"></a><span class="lineno"> 475</span>&#160; W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00476"></a><span class="lineno"> 476</span>&#160; </div>
<div class="line"><a name="l00477"></a><span class="lineno"> 477</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00478"></a><span class="lineno"> 478</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00479"></a><span class="lineno"> 479</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00480"></a><span class="lineno"> 480</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00481"></a><span class="lineno"> 481</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
<div class="line"><a name="l00482"></a><span class="lineno"> 482</span>&#160; }</div>
<div class="line"><a name="l00483"></a><span class="lineno"> 483</span>&#160; }</div>
<div class="line"><a name="l00484"></a><span class="lineno"> 484</span>&#160; </div>
<div class="line"><a name="l00485"></a><span class="lineno"> 485</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a>(X, N); <span class="comment">// create test data around the lamniscate</span></div>
<div class="line"><a name="l00486"></a><span class="lineno"> 486</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test3.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00487"></a><span class="lineno"> 487</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w31.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00488"></a><span class="lineno"> 488</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00489"></a><span class="lineno"> 489</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, N, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00490"></a><span class="lineno"> 490</span>&#160; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w32.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00491"></a><span class="lineno"> 491</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00492"></a><span class="lineno"> 492</span>&#160; </div>
<div class="line"><a name="l00493"></a><span class="lineno"> 493</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++)</div>
<div class="line"><a name="l00494"></a><span class="lineno"> 494</span>&#160; {</div>
<div class="line"><a name="l00495"></a><span class="lineno"> 495</span>&#160; <span class="keywordflow">if</span> (i &lt; N)</div>
<div class="line"><a name="l00496"></a><span class="lineno"> 496</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00497"></a><span class="lineno"> 497</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00498"></a><span class="lineno"> 498</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00499"></a><span class="lineno"> 499</span>&#160; }</div>
<div class="line"><a name="l00500"></a><span class="lineno"> 500</span>&#160; free(X);</div>
<div class="line"><a name="l00501"></a><span class="lineno"> 501</span>&#160; free(W);</div>
<div class="line"><a name="l00502"></a><span class="lineno"> 502</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a6d0455dd5c30adda100e95f0423c786e_cgraph.svg" width="663" height="190"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a41ae16442e3e5b891a58d2e5932a2cd0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a41ae16442e3e5b891a58d2e5932a2cd0">&#9670;&nbsp;</a></span>test_3d_classes()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_3d_classes </td>
<td>(</td>
<td class="paramtype">double *const *&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Creates a random set of points distributed in four clusters in 3D space with centroids at the points. </p>
<ul>
<li>\((0,5, 0.5, 0.5)\)</li>
<li>\((0,5,-0.5, -0.5)\)</li>
<li>\((-0,5, 0.5, 0.5)\)</li>
<li>\((-0,5,-0.5, -0.5)\)</li>
</ul>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00411"></a><span class="lineno"> 411</span>&#160;{</div>
<div class="line"><a name="l00412"></a><span class="lineno"> 412</span>&#160; <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.1; <span class="comment">// radius of cluster</span></div>
<div class="line"><a name="l00413"></a><span class="lineno"> 413</span>&#160; <span class="keywordtype">int</span> i;</div>
<div class="line"><a name="l00414"></a><span class="lineno"> 414</span>&#160; <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
<div class="line"><a name="l00415"></a><span class="lineno"> 415</span>&#160; <span class="keyword">const</span> <span class="keywordtype">double</span> centres[][3] = {</div>
<div class="line"><a name="l00416"></a><span class="lineno"> 416</span>&#160; <span class="comment">// centres of each class cluster</span></div>
<div class="line"><a name="l00417"></a><span class="lineno"> 417</span>&#160; {.5, .5, .5}, <span class="comment">// centre of class 1</span></div>
<div class="line"><a name="l00418"></a><span class="lineno"> 418</span>&#160; {.5, -.5, -.5}, <span class="comment">// centre of class 2</span></div>
<div class="line"><a name="l00419"></a><span class="lineno"> 419</span>&#160; {-.5, .5, .5}, <span class="comment">// centre of class 3</span></div>
<div class="line"><a name="l00420"></a><span class="lineno"> 420</span>&#160; {-.5, -.5 - .5} <span class="comment">// centre of class 4</span></div>
<div class="line"><a name="l00421"></a><span class="lineno"> 421</span>&#160; };</div>
<div class="line"><a name="l00422"></a><span class="lineno"> 422</span>&#160; </div>
<div class="line"><a name="l00423"></a><span class="lineno"> 423</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00424"></a><span class="lineno"> 424</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00425"></a><span class="lineno"> 425</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00426"></a><span class="lineno"> 426</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><a name="l00427"></a><span class="lineno"> 427</span>&#160; {</div>
<div class="line"><a name="l00428"></a><span class="lineno"> 428</span>&#160; <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
<div class="line"><a name="l00429"></a><span class="lineno"> 429</span>&#160; rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
<div class="line"><a name="l00430"></a><span class="lineno"> 430</span>&#160; </div>
<div class="line"><a name="l00431"></a><span class="lineno"> 431</span>&#160; <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<div class="line"><a name="l00432"></a><span class="lineno"> 432</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
<div class="line"><a name="l00433"></a><span class="lineno"> 433</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
<div class="line"><a name="l00434"></a><span class="lineno"> 434</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][2] = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][2] - R, centres[<span class="keyword">class</span>][2] + R);</div>
<div class="line"><a name="l00435"></a><span class="lineno"> 435</span>&#160; </div>
<div class="line"><a name="l00436"></a><span class="lineno"> 436</span>&#160; <span class="comment">/* The follosing can also be used</span></div>
<div class="line"><a name="l00437"></a><span class="lineno"> 437</span>&#160;<span class="comment"> for (int j = 0; j &lt; 3; j++)</span></div>
<div class="line"><a name="l00438"></a><span class="lineno"> 438</span>&#160;<span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
<div class="line"><a name="l00439"></a><span class="lineno"> 439</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00440"></a><span class="lineno"> 440</span>&#160; }</div>
<div class="line"><a name="l00441"></a><span class="lineno"> 441</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a41ae16442e3e5b891a58d2e5932a2cd0_cgraph.svg" width="248" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a107f00650b8041f77767927073ddddb8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a107f00650b8041f77767927073ddddb8">&#9670;&nbsp;</a></span>test_circle()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_circle </td>
<td>(</td>
<td class="paramtype">double *const *&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. </p>
<p>The generating function is </p><p class="formulaDsp">
\begin{eqnarray*} r &amp;\in&amp; [1-\delta r, 1+\delta r)\\ \theta &amp;\in&amp; [0, 2\pi)\\ x &amp;=&amp; r\cos\theta\\ y &amp;=&amp; r\sin\theta \end{eqnarray*}
</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00224"></a><span class="lineno"> 224</span>&#160;{</div>
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>&#160; <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.75, dr = 0.3;</div>
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>&#160; <span class="keywordtype">double</span> a_t = 0., b_t = 2.f * M_PI; <span class="comment">// theta random between 0 and 2*pi</span></div>
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>&#160; <span class="keywordtype">double</span> a_r = R - dr, b_r = R + dr; <span class="comment">// radius random between R-dr and R+dr</span></div>
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>&#160; <span class="keywordtype">int</span> i;</div>
<div class="line"><a name="l00229"></a><span class="lineno"> 229</span>&#160; </div>
<div class="line"><a name="l00230"></a><span class="lineno"> 230</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00232"></a><span class="lineno"> 232</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00233"></a><span class="lineno"> 233</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><a name="l00234"></a><span class="lineno"> 234</span>&#160; {</div>
<div class="line"><a name="l00235"></a><span class="lineno"> 235</span>&#160; <span class="keywordtype">double</span> r = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(a_r, b_r); <span class="comment">// random radius</span></div>
<div class="line"><a name="l00236"></a><span class="lineno"> 236</span>&#160; <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(a_t, b_t); <span class="comment">// random theta</span></div>
<div class="line"><a name="l00237"></a><span class="lineno"> 237</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = r * cos(theta); <span class="comment">// convert from polar to cartesian</span></div>
<div class="line"><a name="l00238"></a><span class="lineno"> 238</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = r * sin(theta);</div>
<div class="line"><a name="l00239"></a><span class="lineno"> 239</span>&#160; }</div>
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a107f00650b8041f77767927073ddddb8_cgraph.svg" width="214" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="aa2246f940155472084ee461f3685d614"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa2246f940155472084ee461f3685d614">&#9670;&nbsp;</a></span>test_lamniscate()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_lamniscate </td>
<td>(</td>
<td class="paramtype">double *const *&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a>. </p>
<p class="formulaDsp">
\begin{eqnarray*} \delta r &amp;=&amp; 0.2\\ \delta x &amp;\in&amp; [-\delta r, \delta r)\\ \delta y &amp;\in&amp; [-\delta r, \delta r)\\ \theta &amp;\in&amp; [0, \pi)\\ x &amp;=&amp; \delta x + \cos\theta\\ y &amp;=&amp; \delta y + \frac{\sin(2\theta)}{2} \end{eqnarray*}
</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00320"></a><span class="lineno"> 320</span>&#160;{</div>
<div class="line"><a name="l00321"></a><span class="lineno"> 321</span>&#160; <span class="keyword">const</span> <span class="keywordtype">double</span> dr = 0.2;</div>
<div class="line"><a name="l00322"></a><span class="lineno"> 322</span>&#160; <span class="keywordtype">int</span> i;</div>
<div class="line"><a name="l00323"></a><span class="lineno"> 323</span>&#160; </div>
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00325"></a><span class="lineno"> 325</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00326"></a><span class="lineno"> 326</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00327"></a><span class="lineno"> 327</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><a name="l00328"></a><span class="lineno"> 328</span>&#160; {</div>
<div class="line"><a name="l00329"></a><span class="lineno"> 329</span>&#160; <span class="keywordtype">double</span> dx = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in x</span></div>
<div class="line"><a name="l00330"></a><span class="lineno"> 330</span>&#160; <span class="keywordtype">double</span> dy = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in y</span></div>
<div class="line"><a name="l00331"></a><span class="lineno"> 331</span>&#160; <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(0, M_PI); <span class="comment">// random theta</span></div>
<div class="line"><a name="l00332"></a><span class="lineno"> 332</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = dx + cos(theta); <span class="comment">// convert from polar to cartesian</span></div>
<div class="line"><a name="l00333"></a><span class="lineno"> 333</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = dy + sin(2. * theta) / 2.f;</div>
<div class="line"><a name="l00334"></a><span class="lineno"> 334</span>&#160; }</div>
<div class="line"><a name="l00335"></a><span class="lineno"> 335</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_aa2246f940155472084ee461f3685d614_cgraph.svg" width="246" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="akohonen__som__trace_8c_html_a107f00650b8041f77767927073ddddb8"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a></div><div class="ttdeci">void test_circle(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed near the circumference of a circle and trains an SOM that ...</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:223</div></div>
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition:</b> prime_factoriziation.c:25</div></div>
<div class="ttc" id="agroup__kohonen__1d_html_ga7b84b14e60f47812b581d1f93057c85a"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a></div><div class="ttdeci">int save_nd_data(const char *fname, double **X, int num_points, int num_features)</div><div class="ttdoc">Save a given n-dimensional data martix to file.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:70</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_aa2246f940155472084ee461f3685d614"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a></div><div class="ttdeci">void test_lamniscate(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed near the locus of the Lamniscate of Gerono.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:319</div></div>
<div class="ttc" id="agroup__kohonen__1d_html_gaeaeffbff2be4d5d15b0d4f10f846abde"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a></div><div class="ttdeci">void kohonen_som_tracer(double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)</div><div class="ttdoc">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:179</div></div>
<div class="ttc" id="agroup__kohonen__1d_html_gaffe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a></div><div class="ttdeci">#define max(a, b)</div><div class="ttdoc">shorthand for maximum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:32</div></div>
<div class="ttc" id="agroup__kohonen__1d_html_gaf5ce14f026d6d231bef29161bac2b485"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a></div><div class="ttdeci">double _random(double a, double b)</div><div class="ttdoc">Helper function to generate a random number in a given interval.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:54</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_a41ae16442e3e5b891a58d2e5932a2cd0"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a></div><div class="ttdeci">void test_3d_classes(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:410</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html">kohonen_som_trace.c</a></li>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>