TheAlgorithms-C/d1/d6b/group__kohonen__2d.html
2021-12-09 17:57:52 +00:00

782 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=11"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: Kohonen SOM topology algorithm</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<span id="projectnumber">&#160;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.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d1/d6b/group__kohonen__2d.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="#nested-classes">Data Structures</a> &#124;
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">Kohonen SOM topology algorithm<div class="ingroups"><a class="el" href="../../d9/d66/group__machine__learning.html">Machine learning algorithms</a></div></div></div>
</div><!--header-->
<div class="contents">
<div class="dynheader">
Collaboration diagram for Kohonen SOM topology algorithm:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/group__kohonen__2d.svg" width="403" height="51"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">to store info regarding 3D arrays <a href="../../d8/db8/structkohonen__array__3d.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:gaffe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top"><a id="gaffe776513b24d84b39af8ab0930fef7f" name="gaffe776513b24d84b39af8ab0930fef7f"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>max</b>(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"><a id="gac6afabdc09a49a433ee19d8a9486056d" name="gac6afabdc09a49a433ee19d8a9486056d"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>min</b>(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 id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gac78022076d889bf2ebfb2c41d4527ceb"><td class="memItemLeft" align="right" valign="top">double *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a> (const struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *arr, int x, int y, int z)</td></tr>
<tr class="memdesc:gac78022076d889bf2ebfb2c41d4527ceb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: <a href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">More...</a><br /></td></tr>
<tr class="separator:gac78022076d889bf2ebfb2c41d4527ceb"><td class="memSeparator" colspan="2">&#160;</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="../../d1/d6b/group__kohonen__2d.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="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">More...</a><br /></td></tr>
<tr class="separator:gaf5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
<tr class="memdesc:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Save a given n-dimensional data martix to file. <a href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">More...</a><br /></td></tr>
<tr class="separator:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a> (const char *fname, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W)</td></tr>
<tr class="memdesc:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="mdescLeft">&#160;</td><td class="mdescRight">Create the distance matrix or <a href="https://en.wikipedia.org/wiki/U-matrix" target="_blank">U-matrix</a> from the trained weights and save to disk. <a href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">More...</a><br /></td></tr>
<tr class="separator:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a> (double **X, int N, double *val, int *x_idx, int *y_idx)</td></tr>
<tr class="memdesc:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get minimum value and index of the value in a matrix. <a href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">More...</a><br /></td></tr>
<tr class="separator:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga83abb572c60d202e100595a989dfe123"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a> (const double *X, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, double **D, int num_out, int num_features, double alpha, int R)</td></tr>
<tr class="memdesc:ga83abb572c60d202e100595a989dfe123"><td class="mdescLeft">&#160;</td><td class="mdescRight">Update weights of the SOM using Kohonen algorithm. <a href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">More...</a><br /></td></tr>
<tr class="separator:ga83abb572c60d202e100595a989dfe123"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gacb42eda8af6ebd6a141a34ab00a0b710"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a> (double **X, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
<tr class="memdesc:gacb42eda8af6ebd6a141a34ab00a0b710"><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="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">More...</a><br /></td></tr>
<tr class="separator:gacb42eda8af6ebd6a141a34ab00a0b710"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Function Documentation</h2>
<a id="gaf5ce14f026d6d231bef29161bac2b485" name="gaf5ce14f026d6d231bef29161bac2b485"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gaf5ce14f026d6d231bef29161bac2b485">&#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. </p>
<p ><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 id="l00088" name="l00088"></a><span class="lineno"> 88</span>{</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <span class="keywordflow">return</span> ((b - a) * (rand() % 100) / 100.f) + a;</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gadc22d512c00a9f5799ee067f4fb90b4b" name="gadc22d512c00a9f5799ee067f4fb90b4b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gadc22d512c00a9f5799ee067f4fb90b4b">&#9670;&nbsp;</a></span>get_min_2d()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void get_min_2d </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>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>x_idx</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>y_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 matrix. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>matrix 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">x_idx</td><td>x-index where minimum value was found </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">y_idx</td><td>y-index where minimum value was found </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a id="l00205" name="l00205"></a><span class="lineno"> 205</span>{</div>
<div class="line"><a id="l00206" name="l00206"></a><span class="lineno"> 206</span> val[0] = INFINITY; <span class="comment">// initial min value</span></div>
<div class="line"><a id="l00207" name="l00207"></a><span class="lineno"> 207</span> </div>
<div class="line"><a id="l00208" name="l00208"></a><span class="lineno"> 208</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++) <span class="comment">// traverse each x-index</span></div>
<div class="line"><a id="l00209" name="l00209"></a><span class="lineno"> 209</span> {</div>
<div class="line"><a id="l00210" name="l00210"></a><span class="lineno"> 210</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; N; j++) <span class="comment">// traverse each y-index</span></div>
<div class="line"><a id="l00211" name="l00211"></a><span class="lineno"> 211</span> {</div>
<div class="line"><a id="l00212" name="l00212"></a><span class="lineno"> 212</span> <span class="keywordflow">if</span> (X[i][j] &lt; val[0]) <span class="comment">// if a lower value is found</span></div>
<div class="line"><a id="l00213" name="l00213"></a><span class="lineno"> 213</span> { <span class="comment">// save the value and its index</span></div>
<div class="line"><a id="l00214" name="l00214"></a><span class="lineno"> 214</span> x_idx[0] = i;</div>
<div class="line"><a id="l00215" name="l00215"></a><span class="lineno"> 215</span> y_idx[0] = j;</div>
<div class="line"><a id="l00216" name="l00216"></a><span class="lineno"> 216</span> val[0] = X[i][j];</div>
<div class="line"><a id="l00217" name="l00217"></a><span class="lineno"> 217</span> }</div>
<div class="line"><a id="l00218" name="l00218"></a><span class="lineno"> 218</span> }</div>
<div class="line"><a id="l00219" name="l00219"></a><span class="lineno"> 219</span> }</div>
<div class="line"><a id="l00220" name="l00220"></a><span class="lineno"> 220</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gac78022076d889bf2ebfb2c41d4527ceb" name="gac78022076d889bf2ebfb2c41d4527ceb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gac78022076d889bf2ebfb2c41d4527ceb">&#9670;&nbsp;</a></span>kohonen_data_3d()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double * kohonen_data_3d </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *&#160;</td>
<td class="paramname"><em>arr</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#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>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>z</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: </p>
<p class="formulaDsp">
\[ X_{i,j,k} = i\times M\times N + j\times N + k \]
</p>
<p> where \(L\), \(M\) and \(N\) are the 3D matrix dimensions. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">arr</td><td>pointer to <a class="el" href="../../d8/db8/structkohonen__array__3d.html" title="to store info regarding 3D arrays">kohonen_array_3d</a> structure </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>first index </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">y</td><td>second index </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">z</td><td>third index </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to (x,y,z)^th location of data </dd></dl>
<div class="fragment"><div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span>{</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> <span class="keywordtype">int</span> offset = (x * arr-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a> * arr-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>) + (y * arr-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>) + z;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordflow">return</span> arr-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#ad546baa2e81c6196d5f1dc0fe2e5bd59">data</a> + offset;</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span>}</div>
<div class="ttc" id="astructkohonen__array__3d_html_a160f14830bdfbbf9f422f382ee754dbf"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">kohonen_array_3d::dim3</a></div><div class="ttdeci">int dim3</div><div class="ttdoc">lengths of thirddimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:51</div></div>
<div class="ttc" id="astructkohonen__array__3d_html_a888d7e007b38c91c7933e12a9566af1d"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">kohonen_array_3d::dim2</a></div><div class="ttdeci">int dim2</div><div class="ttdoc">lengths of second dimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:50</div></div>
<div class="ttc" id="astructkohonen__array__3d_html_ad546baa2e81c6196d5f1dc0fe2e5bd59"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#ad546baa2e81c6196d5f1dc0fe2e5bd59">kohonen_array_3d::data</a></div><div class="ttdeci">double * data</div><div class="ttdoc">pointer to data</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:52</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="gacb42eda8af6ebd6a141a34ab00a0b710" name="gacb42eda8af6ebd6a141a34ab00a0b710"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gacb42eda8af6ebd6a141a34ab00a0b710">&#9670;&nbsp;</a></span>kohonen_som()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void kohonen_som </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">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *&#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">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 id="l00316" name="l00316"></a><span class="lineno"> 316</span>{</div>
<div class="line"><a id="l00317" name="l00317"></a><span class="lineno"> 317</span> <span class="keywordtype">int</span> R = num_out &gt;&gt; 2, iter = 0;</div>
<div class="line"><a id="l00318" name="l00318"></a><span class="lineno"> 318</span> <span class="keywordtype">double</span> **D = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a id="l00319" name="l00319"></a><span class="lineno"> 319</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; num_out; i++)</div>
<div class="line"><a id="l00320" name="l00320"></a><span class="lineno"> 320</span> D[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a id="l00321" name="l00321"></a><span class="lineno"> 321</span> </div>
<div class="line"><a id="l00322" name="l00322"></a><span class="lineno"> 322</span> <span class="keywordtype">double</span> dmin = 1.f; <span class="comment">// average minimum distance of all samples</span></div>
<div class="line"><a id="l00323" name="l00323"></a><span class="lineno"> 323</span> </div>
<div class="line"><a id="l00324" name="l00324"></a><span class="lineno"> 324</span> <span class="comment">// Loop alpha from 1 to slpha_min</span></div>
<div class="line"><a id="l00325" name="l00325"></a><span class="lineno"> 325</span> <span class="keywordflow">for</span> (<span class="keywordtype">double</span> alpha = 1.f; alpha &gt; alpha_min &amp;&amp; dmin &gt; 1e-3;</div>
<div class="line"><a id="l00326" name="l00326"></a><span class="lineno"> 326</span> alpha -= 0.001, iter++)</div>
<div class="line"><a id="l00327" name="l00327"></a><span class="lineno"> 327</span> {</div>
<div class="line"><a id="l00328" name="l00328"></a><span class="lineno"> 328</span> dmin = 0.f;</div>
<div class="line"><a id="l00329" name="l00329"></a><span class="lineno"> 329</span> <span class="comment">// Loop for each sample pattern in the data set</span></div>
<div class="line"><a id="l00330" name="l00330"></a><span class="lineno"> 330</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample &lt; num_samples; sample++)</div>
<div class="line"><a id="l00331" name="l00331"></a><span class="lineno"> 331</span> {</div>
<div class="line"><a id="l00332" name="l00332"></a><span class="lineno"> 332</span> <span class="comment">// update weights for the current input pattern sample</span></div>
<div class="line"><a id="l00333" name="l00333"></a><span class="lineno"> 333</span> dmin += <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a>(X[sample], W, D, num_out,</div>
<div class="line"><a id="l00334" name="l00334"></a><span class="lineno"> 334</span> num_features, alpha, R);</div>
<div class="line"><a id="l00335" name="l00335"></a><span class="lineno"> 335</span> }</div>
<div class="line"><a id="l00336" name="l00336"></a><span class="lineno"> 336</span> </div>
<div class="line"><a id="l00337" name="l00337"></a><span class="lineno"> 337</span> <span class="comment">// every 20th iteration, reduce the neighborhood range</span></div>
<div class="line"><a id="l00338" name="l00338"></a><span class="lineno"> 338</span> <span class="keywordflow">if</span> (iter % 100 == 0 &amp;&amp; R &gt; 1)</div>
<div class="line"><a id="l00339" name="l00339"></a><span class="lineno"> 339</span> R--;</div>
<div class="line"><a id="l00340" name="l00340"></a><span class="lineno"> 340</span> </div>
<div class="line"><a id="l00341" name="l00341"></a><span class="lineno"> 341</span> dmin /= num_samples;</div>
<div class="line"><a id="l00342" name="l00342"></a><span class="lineno"> 342</span> printf(<span class="stringliteral">&quot;iter: %5d\t alpha: %.4g\t R: %d\td_min: %.4g\r&quot;</span>, iter, alpha, R,</div>
<div class="line"><a id="l00343" name="l00343"></a><span class="lineno"> 343</span> dmin);</div>
<div class="line"><a id="l00344" name="l00344"></a><span class="lineno"> 344</span> }</div>
<div class="line"><a id="l00345" name="l00345"></a><span class="lineno"> 345</span> putchar(<span class="charliteral">&#39;\n&#39;</span>);</div>
<div class="line"><a id="l00346" name="l00346"></a><span class="lineno"> 346</span> </div>
<div class="line"><a id="l00347" name="l00347"></a><span class="lineno"> 347</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; num_out; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(D[i]);</div>
<div class="line"><a id="l00348" name="l00348"></a><span class="lineno"> 348</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(D);</div>
<div class="line"><a id="l00349" name="l00349"></a><span class="lineno"> 349</span>}</div>
<div class="ttc" id="agroup__kohonen__2d_html_ga83abb572c60d202e100595a989dfe123"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a></div><div class="ttdeci">double kohonen_update_weights(const double *X, struct kohonen_array_3d *W, double **D, int num_out, int num_features, double alpha, int R)</div><div class="ttdoc">Update weights of the SOM using Kohonen algorithm.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:234</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></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="../../d1/d6b/group__kohonen__2d_gacb42eda8af6ebd6a141a34ab00a0b710_cgraph.svg" width="503" height="88"><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="ga83abb572c60d202e100595a989dfe123" name="ga83abb572c60d202e100595a989dfe123"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga83abb572c60d202e100595a989dfe123">&#9670;&nbsp;</a></span>kohonen_update_weights()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double kohonen_update_weights </td>
<td>(</td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>X</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *&#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>
<dl class="section return"><dt>Returns</dt><dd>minimum distance of sample and trained weights </dd></dl>
<div class="fragment"><div class="line"><a id="l00237" name="l00237"></a><span class="lineno"> 237</span>{</div>
<div class="line"><a id="l00238" name="l00238"></a><span class="lineno"> 238</span> <span class="keywordtype">int</span> x, y, k;</div>
<div class="line"><a id="l00239" name="l00239"></a><span class="lineno"> 239</span> <span class="keywordtype">double</span> d_min = 0.f;</div>
<div class="line"><a id="l00240" name="l00240"></a><span class="lineno"> 240</span> </div>
<div class="line"><a id="l00241" name="l00241"></a><span class="lineno"> 241</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a id="l00242" name="l00242"></a><span class="lineno"> 242</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a id="l00243" name="l00243"></a><span class="lineno"> 243</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00244" name="l00244"></a><span class="lineno"> 244</span> <span class="comment">// step 1: for each 2D output point</span></div>
<div class="line"><a id="l00245" name="l00245"></a><span class="lineno"> 245</span> <span class="keywordflow">for</span> (x = 0; x &lt; num_out; x++)</div>
<div class="line"><a id="l00246" name="l00246"></a><span class="lineno"> 246</span> {</div>
<div class="line"><a id="l00247" name="l00247"></a><span class="lineno"> 247</span> <span class="keywordflow">for</span> (y = 0; y &lt; num_out; y++)</div>
<div class="line"><a id="l00248" name="l00248"></a><span class="lineno"> 248</span> {</div>
<div class="line"><a id="l00249" name="l00249"></a><span class="lineno"> 249</span> D[x][y] = 0.f;</div>
<div class="line"><a id="l00250" name="l00250"></a><span class="lineno"> 250</span> <span class="comment">// compute Euclidian distance of each output</span></div>
<div class="line"><a id="l00251" name="l00251"></a><span class="lineno"> 251</span> <span class="comment">// point from the current sample</span></div>
<div class="line"><a id="l00252" name="l00252"></a><span class="lineno"> 252</span> <span class="keywordflow">for</span> (k = 0; k &lt; num_features; k++)</div>
<div class="line"><a id="l00253" name="l00253"></a><span class="lineno"> 253</span> {</div>
<div class="line"><a id="l00254" name="l00254"></a><span class="lineno"> 254</span> <span class="keywordtype">double</span> *w = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(W, x, y, k);</div>
<div class="line"><a id="l00255" name="l00255"></a><span class="lineno"> 255</span> D[x][y] += (w[0] - X[k]) * (w[0] - X[k]);</div>
<div class="line"><a id="l00256" name="l00256"></a><span class="lineno"> 256</span> }</div>
<div class="line"><a id="l00257" name="l00257"></a><span class="lineno"> 257</span> D[x][y] = sqrt(D[x][y]);</div>
<div class="line"><a id="l00258" name="l00258"></a><span class="lineno"> 258</span> }</div>
<div class="line"><a id="l00259" name="l00259"></a><span class="lineno"> 259</span> }</div>
<div class="line"><a id="l00260" name="l00260"></a><span class="lineno"> 260</span> </div>
<div class="line"><a id="l00261" name="l00261"></a><span class="lineno"> 261</span> <span class="comment">// step 2: get closest node i.e., node with smallest Euclidian distance to</span></div>
<div class="line"><a id="l00262" name="l00262"></a><span class="lineno"> 262</span> <span class="comment">// the current pattern</span></div>
<div class="line"><a id="l00263" name="l00263"></a><span class="lineno"> 263</span> <span class="keywordtype">int</span> d_min_x, d_min_y;</div>
<div class="line"><a id="l00264" name="l00264"></a><span class="lineno"> 264</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a>(D, num_out, &amp;d_min, &amp;d_min_x, &amp;d_min_y);</div>
<div class="line"><a id="l00265" name="l00265"></a><span class="lineno"> 265</span> </div>
<div class="line"><a id="l00266" name="l00266"></a><span class="lineno"> 266</span> <span class="comment">// step 3a: get the neighborhood range</span></div>
<div class="line"><a id="l00267" name="l00267"></a><span class="lineno"> 267</span> <span class="keywordtype">int</span> from_x = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_x - R);</div>
<div class="line"><a id="l00268" name="l00268"></a><span class="lineno"> 268</span> <span class="keywordtype">int</span> to_x = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_x + R + 1);</div>
<div class="line"><a id="l00269" name="l00269"></a><span class="lineno"> 269</span> <span class="keywordtype">int</span> from_y = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_y - R);</div>
<div class="line"><a id="l00270" name="l00270"></a><span class="lineno"> 270</span> <span class="keywordtype">int</span> to_y = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_y + R + 1);</div>
<div class="line"><a id="l00271" name="l00271"></a><span class="lineno"> 271</span> </div>
<div class="line"><a id="l00272" name="l00272"></a><span class="lineno"> 272</span> <span class="comment">// step 3b: update the weights of nodes in the</span></div>
<div class="line"><a id="l00273" name="l00273"></a><span class="lineno"> 273</span> <span class="comment">// neighborhood</span></div>
<div class="line"><a id="l00274" name="l00274"></a><span class="lineno"> 274</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a id="l00275" name="l00275"></a><span class="lineno"> 275</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a id="l00276" name="l00276"></a><span class="lineno"> 276</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00277" name="l00277"></a><span class="lineno"> 277</span> <span class="keywordflow">for</span> (x = from_x; x &lt; to_x; x++)</div>
<div class="line"><a id="l00278" name="l00278"></a><span class="lineno"> 278</span> {</div>
<div class="line"><a id="l00279" name="l00279"></a><span class="lineno"> 279</span> <span class="keywordflow">for</span> (y = from_y; y &lt; to_y; y++)</div>
<div class="line"><a id="l00280" name="l00280"></a><span class="lineno"> 280</span> {</div>
<div class="line"><a id="l00281" name="l00281"></a><span class="lineno"> 281</span> <span class="comment">/* you can enable the following normalization if needed.</span></div>
<div class="line"><a id="l00282" name="l00282"></a><span class="lineno"> 282</span><span class="comment"> personally, I found it detrimental to convergence */</span></div>
<div class="line"><a id="l00283" name="l00283"></a><span class="lineno"> 283</span> <span class="comment">// const double s2pi = sqrt(2.f * M_PI);</span></div>
<div class="line"><a id="l00284" name="l00284"></a><span class="lineno"> 284</span> <span class="comment">// double normalize = 1.f / (alpha * s2pi);</span></div>
<div class="line"><a id="l00285" name="l00285"></a><span class="lineno"> 285</span> </div>
<div class="line"><a id="l00286" name="l00286"></a><span class="lineno"> 286</span> <span class="comment">/* apply scaling inversely proportional to distance from the</span></div>
<div class="line"><a id="l00287" name="l00287"></a><span class="lineno"> 287</span><span class="comment"> current node */</span></div>
<div class="line"><a id="l00288" name="l00288"></a><span class="lineno"> 288</span> <span class="keywordtype">double</span> d2 =</div>
<div class="line"><a id="l00289" name="l00289"></a><span class="lineno"> 289</span> (d_min_x - x) * (d_min_x - x) + (d_min_y - y) * (d_min_y - y);</div>
<div class="line"><a id="l00290" name="l00290"></a><span class="lineno"> 290</span> <span class="keywordtype">double</span> scale_factor = exp(-d2 / (2.f * alpha * alpha));</div>
<div class="line"><a id="l00291" name="l00291"></a><span class="lineno"> 291</span> </div>
<div class="line"><a id="l00292" name="l00292"></a><span class="lineno"> 292</span> <span class="keywordflow">for</span> (k = 0; k &lt; num_features; k++)</div>
<div class="line"><a id="l00293" name="l00293"></a><span class="lineno"> 293</span> {</div>
<div class="line"><a id="l00294" name="l00294"></a><span class="lineno"> 294</span> <span class="keywordtype">double</span> *w = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(W, x, y, k);</div>
<div class="line"><a id="l00295" name="l00295"></a><span class="lineno"> 295</span> <span class="comment">// update weights of nodes in the neighborhood</span></div>
<div class="line"><a id="l00296" name="l00296"></a><span class="lineno"> 296</span> w[0] += alpha * scale_factor * (X[k] - w[0]);</div>
<div class="line"><a id="l00297" name="l00297"></a><span class="lineno"> 297</span> }</div>
<div class="line"><a id="l00298" name="l00298"></a><span class="lineno"> 298</span> }</div>
<div class="line"><a id="l00299" name="l00299"></a><span class="lineno"> 299</span> }</div>
<div class="line"><a id="l00300" name="l00300"></a><span class="lineno"> 300</span> <span class="keywordflow">return</span> d_min;</div>
<div class="line"><a id="l00301" name="l00301"></a><span class="lineno"> 301</span>}</div>
<div class="ttc" id="agroup__kohonen__2d_html_gac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a></div><div class="ttdeci">#define min(a, b)</div><div class="ttdoc">shorthand for minimum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:43</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_gac78022076d889bf2ebfb2c41d4527ceb"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a></div><div class="ttdeci">double * kohonen_data_3d(const struct kohonen_array_3d *arr, int x, int y, int z)</div><div class="ttdoc">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:67</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_gadc22d512c00a9f5799ee067f4fb90b4b"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a></div><div class="ttdeci">void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)</div><div class="ttdoc">Get minimum value and index of the value in a matrix.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:204</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_gaffe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.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_topology.c:39</div></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="../../d1/d6b/group__kohonen__2d_ga83abb572c60d202e100595a989dfe123_cgraph.svg" width="352" height="88"><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="ga6824dc6d973eb3339af7aef5fea78b0c" name="ga6824dc6d973eb3339af7aef5fea78b0c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga6824dc6d973eb3339af7aef5fea78b0c">&#9670;&nbsp;</a></span>save_2d_data()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int save_2d_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 overwritten 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 id="l00104" name="l00104"></a><span class="lineno"> 104</span>{</div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> FILE *fp = fopen(fname, <span class="stringliteral">&quot;wt&quot;</span>);</div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span> {</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span> <span class="keywordtype">char</span> msg[120];</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> sprintf(msg, <span class="stringliteral">&quot;File error (%s): &quot;</span>, fname);</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> perror(msg);</div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> }</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> </div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> <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 id="l00115" name="l00115"></a><span class="lineno"> 115</span> {</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> <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 id="l00117" name="l00117"></a><span class="lineno"> 117</span> {</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> 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 id="l00119" name="l00119"></a><span class="lineno"> 119</span> <span class="keywordflow">if</span> (j &lt; num_features - 1) <span class="comment">// if not the last feature</span></div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> fputc(<span class="charliteral">&#39;,&#39;</span>, fp); <span class="comment">// suffix comma</span></div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> }</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> <span class="keywordflow">if</span> (i &lt; num_points - 1) <span class="comment">// if not the last row</span></div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> fputc(<span class="charliteral">&#39;\n&#39;</span>, fp); <span class="comment">// start a new line</span></div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> }</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> fclose(fp);</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga49d35f68f5d11d8ef6f8cce0d0e7bcba" name="ga49d35f68f5d11d8ef6f8cce0d0e7bcba"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">&#9670;&nbsp;</a></span>save_u_matrix()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int save_u_matrix </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">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *&#160;</td>
<td class="paramname"><em>W</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Create the distance matrix or <a href="https://en.wikipedia.org/wiki/U-matrix" target="_blank">U-matrix</a> from the trained weights and save to disk. </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">W</td><td>model matrix to save </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 id="l00140" name="l00140"></a><span class="lineno"> 140</span>{</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> FILE *fp = fopen(fname, <span class="stringliteral">&quot;wt&quot;</span>);</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span> <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno"> 143</span> {</div>
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno"> 144</span> <span class="keywordtype">char</span> msg[120];</div>
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno"> 145</span> sprintf(msg, <span class="stringliteral">&quot;File error (%s): &quot;</span>, fname);</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> perror(msg);</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> }</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> </div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> <span class="keywordtype">int</span> R = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a> &gt;&gt; 3, 2); <span class="comment">/* neighborhood range */</span></div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> </div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>; i++) <span class="comment">// for each x</span></div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> {</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a>; j++) <span class="comment">// for each y</span></div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> {</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> <span class="keywordtype">double</span> distance = 0.f;</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> <span class="keywordtype">int</span> k;</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> </div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <span class="keywordtype">int</span> from_x = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, i - R);</div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <span class="keywordtype">int</span> to_x = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>, i + R + 1);</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> <span class="keywordtype">int</span> from_y = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, j - R);</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> <span class="keywordtype">int</span> to_y = <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a>, j + R + 1);</div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> <span class="keywordtype">int</span> l;</div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span><span class="preprocessor">#pragma omp parallel for reduction(+ : distance)</span></div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> <span class="keywordflow">for</span> (l = from_x; l &lt; to_x; l++) <span class="comment">// scan neighborhoor in x</span></div>
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span> {</div>
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> m = from_y; m &lt; to_y; m++) <span class="comment">// scan neighborhood in y</span></div>
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span> {</div>
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> <span class="keywordtype">double</span> d = 0.f;</div>
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> <span class="keywordflow">for</span> (k = 0; k &lt; W-&gt;<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>; k++) <span class="comment">// for each feature</span></div>
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> {</div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span> <span class="keywordtype">double</span> *w1 = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(W, i, j, k);</div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> <span class="keywordtype">double</span> *w2 = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(W, l, m, k);</div>
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> d += (w1[0] - w2[0]) * (w1[0] - w2[0]);</div>
<div class="line"><a id="l00177" name="l00177"></a><span class="lineno"> 177</span> <span class="comment">// distance += w1[0] * w1[0];</span></div>
<div class="line"><a id="l00178" name="l00178"></a><span class="lineno"> 178</span> }</div>
<div class="line"><a id="l00179" name="l00179"></a><span class="lineno"> 179</span> distance += sqrt(d);</div>
<div class="line"><a id="l00180" name="l00180"></a><span class="lineno"> 180</span> <span class="comment">// distance += d;</span></div>
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</span> }</div>
<div class="line"><a id="l00182" name="l00182"></a><span class="lineno"> 182</span> }</div>
<div class="line"><a id="l00183" name="l00183"></a><span class="lineno"> 183</span> </div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> distance /= R * R; <span class="comment">// mean distance from neighbors</span></div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> fprintf(fp, <span class="stringliteral">&quot;%.4g&quot;</span>, distance); <span class="comment">// print the mean separation</span></div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span> <span class="keywordflow">if</span> (j &lt; W-&gt;dim2 - 1) <span class="comment">// if not the last column</span></div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> fputc(<span class="charliteral">&#39;,&#39;</span>, fp); <span class="comment">// suffix comma</span></div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> }</div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> <span class="keywordflow">if</span> (i &lt; W-&gt;dim1 - 1) <span class="comment">// if not the last row</span></div>
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span> fputc(<span class="charliteral">&#39;\n&#39;</span>, fp); <span class="comment">// start a new line</span></div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> }</div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span> fclose(fp);</div>
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span>}</div>
<div class="ttc" id="astructkohonen__array__3d_html_a16720581653fa9a34d1029e7229a7377"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">kohonen_array_3d::dim1</a></div><div class="ttdeci">int dim1</div><div class="ttdoc">lengths of first dimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:49</div></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="../../d1/d6b/group__kohonen__2d_ga49d35f68f5d11d8ef6f8cce0d0e7bcba_cgraph.svg" width="290" 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 -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2 </li>
</ul>
</div>
</body>
</html>