TheAlgorithms-C/d0/d46/kohonen__som__trace_8c.html

1085 lines
84 KiB
HTML
Raw Normal View History

<!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">#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="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:affe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(a, b)&#160;&#160;&#160;(((a) &gt; (b)) ? (a) : (b))</td></tr>
<tr class="separator:affe776513b24d84b39af8ab0930fef7f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac6afabdc09a49a433ee19d8a9486056d"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a>(a, b)&#160;&#160;&#160;(((a) &lt; (b)) ? (a) : (b))</td></tr>
<tr class="separator:ac6afabdc09a49a433ee19d8a9486056d"><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:af5ce14f026d6d231bef29161bac2b485"><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#af5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
<tr class="separator:af5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7b84b14e60f47812b581d1f93057c85a"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
<tr class="separator:a7b84b14e60f47812b581d1f93057c85a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0a292ebd954c568934b1fd06666d27e6"><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#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a> (double const *X, int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, double *val, int *idx)</td></tr>
<tr class="separator:a0a292ebd954c568934b1fd06666d27e6"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a9c98203f55242ff7a8f45a6370840e66"><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#a9c98203f55242ff7a8f45a6370840e66">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="separator:a9c98203f55242ff7a8f45a6370840e66"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aeaeffbff2be4d5d15b0d4f10f846abde"><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#aeaeffbff2be4d5d15b0d4f10f846abde">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="separator:aeaeffbff2be4d5d15b0d4f10f846abde"><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 <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</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="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 <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</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="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 <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</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="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="separator:a2256c10b16edba377b64a44b6c656908"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">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="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>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a></dd></dl>
<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 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">Macro Definition Documentation</h2>
<a id="a525335710b53cb064ca56b936120431e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a525335710b53cb064ca56b936120431e">&#9670;&nbsp;</a></span>_USE_MATH_DEFINES</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define _USE_MATH_DEFINES</td>
</tr>
</table>
</div><div class="memdoc">
<p>required for MS Visual C </p>
</div>
</div>
<a id="affe776513b24d84b39af8ab0930fef7f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#affe776513b24d84b39af8ab0930fef7f">&#9670;&nbsp;</a></span>max</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define max</td>
<td>(</td>
<td class="paramtype">&#160;</td>
<td class="paramname">a, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">&#160;</td>
<td class="paramname">b&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td>&#160;&#160;&#160;(((a) &gt; (b)) ? (a) : (b))</td>
</tr>
</table>
</div><div class="memdoc">
<p>shorthand for maximum value \ </p>
</div>
</div>
<a id="ac6afabdc09a49a433ee19d8a9486056d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac6afabdc09a49a433ee19d8a9486056d">&#9670;&nbsp;</a></span>min</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define min</td>
<td>(</td>
<td class="paramtype">&#160;</td>
<td class="paramname">a, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">&#160;</td>
<td class="paramname">b&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td>&#160;&#160;&#160;(((a) &lt; (b)) ? (a) : (b))</td>
</tr>
</table>
</div><div class="memdoc">
<p>shorthand for minimum value \ </p>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="af5ce14f026d6d231bef29161bac2b485"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af5ce14f026d6d231bef29161bac2b485">&#9670;&nbsp;</a></span>_random()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double _random </td>
<td>(</td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>b</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Helper function to generate a random number in a given interval. <br />
Steps:</p><ol type="1">
<li><code>r1 = rand() % 100</code> gets a random number between 0 and 99</li>
<li><code>r2 = r1 / 100</code> converts random number to be between 0 and 0.99</li>
<li>scale and offset the random number to given range of \([a,b)\) <p class="formulaDsp">
\[ y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \; \text{mod}\; 100}{100} + a \]
</p>
</li>
</ol>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>lower limit </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">b</td><td>upper limit </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>random number in the range \([a,b)\) </dd></dl>
<div class="fragment"><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160;{</div>
</div><!-- fragment -->
</div>
</div>
<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="l00507"></a><span class="lineno"> 507</span>&#160;{</div>
</div><!-- fragment -->
</div>
</div>
<a id="a0a292ebd954c568934b1fd06666d27e6"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0a292ebd954c568934b1fd06666d27e6">&#9670;&nbsp;</a></span>get_min_1d()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void get_min_1d </td>
<td>(</td>
<td class="paramtype">double const *&#160;</td>
<td class="paramname"><em>X</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>val</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>idx</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Get minimum value and index of the value in a vector </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>vector to search </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points in the vector </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">val</td><td>minimum value found </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">idx</td><td>index where minimum value was found </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; {</div>
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160; <span class="keywordflow">if</span> (X[i] &lt; val[0]) <span class="comment">// if a lower value is found</span></div>
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160; { <span class="comment">// save the value and its index</span></div>
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; idx[0] = i;</div>
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; val[0] = X[i];</div>
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160; }</div>
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; }</div>
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160;}</div>
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160;<span class="comment">/**</span></div>
</div><!-- fragment -->
</div>
</div>
<a id="aeaeffbff2be4d5d15b0d4f10f846abde"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aeaeffbff2be4d5d15b0d4f10f846abde">&#9670;&nbsp;</a></span>kohonen_som_tracer()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void kohonen_som_tracer </td>
<td>(</td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>X</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *const *&#160;</td>
<td class="paramname"><em>W</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_samples</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_features</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_out</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>alpha_min</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>data set </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">D</td><td>temporary vector to store distances </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_samples</td><td>number of output points </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">alpha_min</td><td>terminal value of alpha </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160;{</div>
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160; <span class="keywordtype">int</span> R = num_out &gt;&gt; 2, iter = 0;</div>
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160; <span class="keywordtype">double</span> alpha = 1.f;</div>
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160; <span class="keywordtype">double</span> *D = (<span class="keywordtype">double</span> *)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>&#160; </div>
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>&#160; <span class="comment">// Loop alpha from 1 to slpha_min</span></div>
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>&#160; <span class="keywordflow">for</span> (; alpha &gt; alpha_min; alpha -= 0.01, iter++)</div>
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>&#160; {</div>
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>&#160; <span class="comment">// Loop for each sample pattern in the data set</span></div>
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample &lt; num_samples; sample++)</div>
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>&#160; {</div>
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>&#160; <span class="keyword">const</span> <span class="keywordtype">double</span> *x = X[sample];</div>
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>&#160; <span class="comment">// update weights for the current input pattern sample</span></div>
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">update_weights</a>(x, W, D, num_out, num_features, alpha, R);</div>
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>&#160; }</div>
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>&#160; </div>
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>&#160; <span class="comment">// every 10th iteration, reduce the neighborhood range</span></div>
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>&#160; <span class="keywordflow">if</span> (iter % 10 == 0 &amp;&amp; R &gt; 1)</div>
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>&#160; R--;</div>
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>&#160; }</div>
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>&#160; </div>
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>&#160; free(D);</div>
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>&#160;}</div>
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>&#160;<span class="comment">/** Creates a random set of points distributed *near* the circumference</span></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_aeaeffbff2be4d5d15b0d4f10f846abde_cgraph.svg" width="452" 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="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">&#9670;&nbsp;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char **&#160;</td>
<td class="paramname"><em>argv</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function </p>
<div class="fragment"><div class="line"><a name="l00529"></a><span class="lineno"> 529</span>&#160; : Calculated times include: creating test sets, training <span class="stringliteral">&quot;</span></div>
<div class="line"><a name="l00530"></a><span class="lineno"> 530</span>&#160;<span class="stringliteral"> &quot;</span>model and writing files to disk.)\n\n<span class="stringliteral">&quot;);</span></div>
<div class="line"><a name="l00531"></a><span class="lineno"> 531</span>&#160;<span class="stringliteral"> return 0;</span></div>
<div class="line"><a name="l00532"></a><span class="lineno"> 532</span>&#160;<span class="stringliteral">}</span></div>
</div><!-- fragment -->
</div>
</div>
<a id="a7b84b14e60f47812b581d1f93057c85a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7b84b14e60f47812b581d1f93057c85a">&#9670;&nbsp;</a></span>save_nd_data()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int save_nd_data </td>
<td>(</td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>fname</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>X</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_points</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_features</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Save a given n-dimensional data martix to file.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">fname</td><td>filename to save in (gets overwriten without confirmation) </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>matrix to save </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_points</td><td>rows in the matrix = number of points </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>columns in the matrix = dimensions of points </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 if all ok </dd>
<dd>
-1 if file creation failed </dd></dl>
<div class="fragment"><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160;{</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; FILE *fp = fopen(fname, <span class="stringliteral">&quot;wt&quot;</span>);</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; {</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="keywordtype">char</span> msg[120];</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; sprintf(msg, <span class="stringliteral">&quot;File error (%s): &quot;</span>, fname);</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; perror(msg);</div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; }</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; </div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; num_points; i++) <span class="comment">// for each point in the array</span></div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; {</div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; num_features; j++) <span class="comment">// for each feature in the array</span></div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; {</div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; fprintf(fp, <span class="stringliteral">&quot;%.4g&quot;</span>, X[i][j]); <span class="comment">// print the feature value</span></div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keywordflow">if</span> (j &lt; num_features - 1) <span class="comment">// if not the last feature</span></div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; fprintf(fp, <span class="stringliteral">&quot;,&quot;</span>); <span class="comment">// suffix comma</span></div>
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; }</div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordflow">if</span> (i &lt; num_points - 1) <span class="comment">// if not the last row</span></div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; fprintf(fp, <span class="stringliteral">&quot;\n&quot;</span>); <span class="comment">// start a new line</span></div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; }</div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; fclose(fp);</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;}</div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;<span class="comment">/**</span></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. 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="l00263"></a><span class="lineno"> 263</span>&#160; {</div>
<div class="line"><a name="l00264"></a><span class="lineno"> 264</span>&#160; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00265"></a><span class="lineno"> 265</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="l00266"></a><span class="lineno"> 266</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="l00267"></a><span class="lineno"> 267</span>&#160; {</div>
<div class="line"><a name="l00268"></a><span class="lineno"> 268</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="l00269"></a><span class="lineno"> 269</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00270"></a><span class="lineno"> 270</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00271"></a><span class="lineno"> 271</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00272"></a><span class="lineno"> 272</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</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; }</div>
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span>&#160; </div>
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around circumference of a circle</span></div>
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test1.csv&quot;</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w11.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.1); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w12.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>&#160; </div>
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</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; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>&#160; }</div>
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span>&#160;}</div>
<div class="line"><a name="l00293"></a><span class="lineno"> 293</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00294"></a><span class="lineno"> 294</span>&#160;<span class="comment">/** Creates a random set of points distributed *near* the locus</span></div>
</div><!-- fragment -->
</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. 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="l00355"></a><span class="lineno"> 355</span>&#160; {</div>
<div class="line"><a name="l00356"></a><span class="lineno"> 356</span>&#160; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00357"></a><span class="lineno"> 357</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="l00358"></a><span class="lineno"> 358</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="l00359"></a><span class="lineno"> 359</span>&#160; {</div>
<div class="line"><a name="l00360"></a><span class="lineno"> 360</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="l00361"></a><span class="lineno"> 361</span>&#160; </div>
<div class="line"><a name="l00362"></a><span class="lineno"> 362</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00363"></a><span class="lineno"> 363</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00364"></a><span class="lineno"> 364</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00365"></a><span class="lineno"> 365</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00366"></a><span class="lineno"> 366</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
<div class="line"><a name="l00367"></a><span class="lineno"> 367</span>&#160; }</div>
<div class="line"><a name="l00368"></a><span class="lineno"> 368</span>&#160; }</div>
<div class="line"><a name="l00369"></a><span class="lineno"> 369</span>&#160; </div>
<div class="line"><a name="l00370"></a><span class="lineno"> 370</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around the lamniscate</span></div>
<div class="line"><a name="l00371"></a><span class="lineno"> 371</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test2.csv&quot;</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00372"></a><span class="lineno"> 372</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w21.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00373"></a><span class="lineno"> 373</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00374"></a><span class="lineno"> 374</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00375"></a><span class="lineno"> 375</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w22.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00376"></a><span class="lineno"> 376</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00377"></a><span class="lineno"> 377</span>&#160; </div>
<div class="line"><a name="l00378"></a><span class="lineno"> 378</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</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; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
<div class="line"><a name="l00381"></a><span class="lineno"> 381</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00382"></a><span class="lineno"> 382</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00383"></a><span class="lineno"> 383</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00384"></a><span class="lineno"> 384</span>&#160; }</div>
<div class="line"><a name="l00385"></a><span class="lineno"> 385</span>&#160; free(X);</div>
<div class="line"><a name="l00386"></a><span class="lineno"> 386</span>&#160; free(W);</div>
<div class="line"><a name="l00387"></a><span class="lineno"> 387</span>&#160;}</div>
<div class="line"><a name="l00388"></a><span class="lineno"> 388</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00389"></a><span class="lineno"> 389</span>&#160;<span class="comment">/** Creates a random set of points distributed in four clusters in</span></div>
</div><!-- fragment -->
</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. 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="l00459"></a><span class="lineno"> 459</span>&#160; {</div>
<div class="line"><a name="l00460"></a><span class="lineno"> 460</span>&#160; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><a name="l00461"></a><span class="lineno"> 461</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="l00462"></a><span class="lineno"> 462</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="l00463"></a><span class="lineno"> 463</span>&#160; {</div>
<div class="line"><a name="l00464"></a><span class="lineno"> 464</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="l00465"></a><span class="lineno"> 465</span>&#160; </div>
<div class="line"><a name="l00466"></a><span class="lineno"> 466</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00467"></a><span class="lineno"> 467</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00468"></a><span class="lineno"> 468</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00469"></a><span class="lineno"> 469</span>&#160; <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><a name="l00470"></a><span class="lineno"> 470</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
<div class="line"><a name="l00471"></a><span class="lineno"> 471</span>&#160; }</div>
<div class="line"><a name="l00472"></a><span class="lineno"> 472</span>&#160; }</div>
<div class="line"><a name="l00473"></a><span class="lineno"> 473</span>&#160; </div>
<div class="line"><a name="l00474"></a><span class="lineno"> 474</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around the lamniscate</span></div>
<div class="line"><a name="l00475"></a><span class="lineno"> 475</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;test3.csv&quot;</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
<div class="line"><a name="l00476"></a><span class="lineno"> 476</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w31.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00477"></a><span class="lineno"> 477</span>&#160; features); <span class="comment">// save initial random weights</span></div>
<div class="line"><a name="l00478"></a><span class="lineno"> 478</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
<div class="line"><a name="l00479"></a><span class="lineno"> 479</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">&quot;w32.csv&quot;</span>, W, num_out,</div>
<div class="line"><a name="l00480"></a><span class="lineno"> 480</span>&#160; features); <span class="comment">// save the resultant weights</span></div>
<div class="line"><a name="l00481"></a><span class="lineno"> 481</span>&#160; </div>
<div class="line"><a name="l00482"></a><span class="lineno"> 482</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</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; <span class="keywordflow">if</span> (i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
<div class="line"><a name="l00485"></a><span class="lineno"> 485</span>&#160; free(X[i]);</div>
<div class="line"><a name="l00486"></a><span class="lineno"> 486</span>&#160; <span class="keywordflow">if</span> (i &lt; num_out)</div>
<div class="line"><a name="l00487"></a><span class="lineno"> 487</span>&#160; free(W[i]);</div>
<div class="line"><a name="l00488"></a><span class="lineno"> 488</span>&#160; }</div>
<div class="line"><a name="l00489"></a><span class="lineno"> 489</span>&#160; free(X);</div>
<div class="line"><a name="l00490"></a><span class="lineno"> 490</span>&#160; free(W);</div>
<div class="line"><a name="l00491"></a><span class="lineno"> 491</span>&#160;}</div>
<div class="line"><a name="l00492"></a><span class="lineno"> 492</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00493"></a><span class="lineno"> 493</span>&#160;<span class="comment">/**</span></div>
</div><!-- fragment -->
</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="l00404"></a><span class="lineno"> 404</span>&#160; {</div>
<div class="line"><a name="l00405"></a><span class="lineno"> 405</span>&#160; <span class="comment">// centres of each class cluster</span></div>
<div class="line"><a name="l00406"></a><span class="lineno"> 406</span>&#160; {.5, .5, .5}, <span class="comment">// centre of class 1</span></div>
<div class="line"><a name="l00407"></a><span class="lineno"> 407</span>&#160; {.5, -.5, -.5}, <span class="comment">// centre of class 2</span></div>
<div class="line"><a name="l00408"></a><span class="lineno"> 408</span>&#160; {-.5, .5, .5}, <span class="comment">// centre of class 3</span></div>
<div class="line"><a name="l00409"></a><span class="lineno"> 409</span>&#160; {-.5, -.5 - .5} <span class="comment">// centre of class 4</span></div>
<div class="line"><a name="l00410"></a><span class="lineno"> 410</span>&#160; };</div>
<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="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00413"></a><span class="lineno"> 413</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00414"></a><span class="lineno"> 414</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00415"></a><span class="lineno"> 415</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
<div class="line"><a name="l00416"></a><span class="lineno"> 416</span>&#160; {</div>
<div class="line"><a name="l00417"></a><span class="lineno"> 417</span>&#160; <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
<div class="line"><a name="l00418"></a><span class="lineno"> 418</span>&#160; rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
<div class="line"><a name="l00419"></a><span class="lineno"> 419</span>&#160; </div>
<div class="line"><a name="l00420"></a><span class="lineno"> 420</span>&#160; <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<div class="line"><a name="l00421"></a><span class="lineno"> 421</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
<div class="line"><a name="l00422"></a><span class="lineno"> 422</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
<div class="line"><a name="l00423"></a><span class="lineno"> 423</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][2] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][2] - R, centres[<span class="keyword">class</span>][2] + R);</div>
<div class="line"><a name="l00424"></a><span class="lineno"> 424</span>&#160; </div>
<div class="line"><a name="l00425"></a><span class="lineno"> 425</span>&#160; <span class="comment">/* The follosing can also be used</span></div>
<div class="line"><a name="l00426"></a><span class="lineno"> 426</span>&#160;<span class="comment"> for (int j = 0; j &lt; 3; j++)</span></div>
<div class="line"><a name="l00427"></a><span class="lineno"> 427</span>&#160;<span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
<div class="line"><a name="l00428"></a><span class="lineno"> 428</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00429"></a><span class="lineno"> 429</span>&#160; }</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"></span> </div>
<div class="line"><a name="l00432"></a><span class="lineno"> 432</span>&#160;<span class="comment">/** Test that creates a random set of points distributed in six clusters in</span></div>
</div><!-- fragment -->
</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. 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="l00223"></a><span class="lineno"> 223</span>&#160; {</div>
<div class="line"><a name="l00224"></a><span class="lineno"> 224</span>&#160; <span class="keywordtype">double</span> r = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(a_r, b_r); <span class="comment">// random radius</span></div>
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>&#160; <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(a_t, b_t); <span class="comment">// random theta</span></div>
<div class="line"><a name="l00226"></a><span class="lineno"> 226</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="l00227"></a><span class="lineno"> 227</span>&#160; <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = r * sin(theta);</div>
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>&#160; }</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="comment"></span> </div>
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>&#160;<span class="comment">/** Test that creates a random set of points distributed *near* the</span></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="l00317"></a><span class="lineno"> 317</span>&#160; {</div>
<div class="line"><a name="l00318"></a><span class="lineno"> 318</span>&#160; <span class="keywordtype">double</span> dx = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in x</span></div>
<div class="line"><a name="l00319"></a><span class="lineno"> 319</span>&#160; <span class="keywordtype">double</span> dy = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in y</span></div>
<div class="line"><a name="l00320"></a><span class="lineno"> 320</span>&#160; <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(0, M_PI); <span class="comment">// random theta</span></div>
<div class="line"><a name="l00321"></a><span class="lineno"> 321</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="l00322"></a><span class="lineno"> 322</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="l00323"></a><span class="lineno"> 323</span>&#160; }</div>
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span>&#160;}</div>
<div class="line"><a name="l00325"></a><span class="lineno"> 325</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00326"></a><span class="lineno"> 326</span>&#160;<span class="comment">/** Test that creates a random set of points distributed *near* the locus</span></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>
<a id="a9c98203f55242ff7a8f45a6370840e66"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a9c98203f55242ff7a8f45a6370840e66">&#9670;&nbsp;</a></span>update_weights()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void update_weights </td>
<td>(</td>
<td class="paramtype">double const *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *const *&#160;</td>
<td class="paramname"><em>W</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>D</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_out</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_features</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>alpha</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>R</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Update weights of the SOM using Kohonen algorithm</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>data point </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">D</td><td>temporary vector to store distances </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">alpha</td><td>learning rate \(0&lt;\alpha\le1\) </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">R</td><td>neighborhood range </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160;{</div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; <span class="keywordtype">int</span> j, k;</div>
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; </div>
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; <span class="comment">// step 1: for each output point</span></div>
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; num_out; j++)</div>
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160; {</div>
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160; D[j] = 0.f;</div>
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160; <span class="comment">// compute Euclidian distance of each output</span></div>
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>&#160; <span class="comment">// point from the current sample</span></div>
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160; <span class="keywordflow">for</span> (k = 0; k &lt; num_features; k++)</div>
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160; D[j] += (W[j][k] - x[k]) * (W[j][k] - x[k]);</div>
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; }</div>
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160; </div>
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160; <span class="comment">// step 2: get closest node i.e., node with snallest Euclidian distance to</span></div>
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160; <span class="comment">// the current pattern</span></div>
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; <span class="keywordtype">int</span> d_min_idx;</div>
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160; <span class="keywordtype">double</span> d_min;</div>
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a>(D, num_out, &amp;d_min, &amp;d_min_idx);</div>
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; </div>
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; <span class="comment">// step 3a: get the neighborhood range</span></div>
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; <span class="keywordtype">int</span> from_node = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_idx - R);</div>
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160; <span class="keywordtype">int</span> to_node = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_idx + R + 1);</div>
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; </div>
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; <span class="comment">// step 3b: update the weights of nodes in the</span></div>
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160; <span class="comment">// neighborhood</span></div>
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; <span class="keywordflow">for</span> (j = from_node; j &lt; to_node; j++)</div>
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; <span class="keywordflow">for</span> (k = 0; k &lt; num_features; k++)</div>
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; <span class="comment">// update weights of nodes in the neighborhood</span></div>
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; W[j][k] += alpha * (x[k] - W[j][k]);</div>
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160;}</div>
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160;<span class="comment">/**</span></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_a9c98203f55242ff7a8f45a6370840e66_cgraph.svg" width="262" 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_a7b84b14e60f47812b581d1f93057c85a"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">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="ttdef"><b>Definition:</b> kohonen_som_trace.c:65</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_affe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a></div><div class="ttdeci">#define max(a, b)</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:24</div></div>
<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="ttdef"><b>Definition:</b> kohonen_som_trace.c:214</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="akohonen__som__trace_8c_html_aeaeffbff2be4d5d15b0d4f10f846abde"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">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="ttdef"><b>Definition:</b> kohonen_som_trace.c:175</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_a9c98203f55242ff7a8f45a6370840e66"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">update_weights</a></div><div class="ttdeci">void update_weights(double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:124</div></div>
<div class="ttc" id="aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4"><div class="ttname"><a href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a></div><div class="ttdeci">#define N</div><div class="ttdef"><b>Definition:</b> sol1.c:109</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_a0a292ebd954c568934b1fd06666d27e6"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a></div><div class="ttdeci">void get_min_1d(double const *X, int N, double *val, int *idx)</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:99</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_af5ce14f026d6d231bef29161bac2b485"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a></div><div class="ttdeci">double _random(double a, double b)</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:50</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="ttdef"><b>Definition:</b> kohonen_som_trace.c:310</div></div>
<div class="ttc" id="akohonen__som__trace_8c_html_ac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a></div><div class="ttdeci">#define min(a, b)</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:30</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="ttdef"><b>Definition:</b> kohonen_som_trace.c:401</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>