TheAlgorithms-C/d2/df6/kohonen__som__topology_8c.html
2022-01-14 20:11:27 +00:00

795 lines
78 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.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: machine_learning/kohonen_som_topology.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 id="projectrow">
<td id="projectalign">
<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.3 -->
<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('d2/df6/kohonen__som__topology_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="#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.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Self-organizing_map" target="_blank">Kohonen self organizing map</a> (topological map)
<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_topology.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dc/dd4/kohonen__som__topology_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 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:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e" name="a525335710b53cb064ca56b936120431e"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>_USE_MATH_DEFINES</b></td></tr>
<tr class="memdesc:a525335710b53cb064ca56b936120431e"><td class="mdescLeft">&#160;</td><td class="mdescRight">required for MS Visual C <br /></td></tr>
<tr class="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaffe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top">
#define&#160;</td><td class="memItemRight" valign="bottom"><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">
#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>
<tr class="memitem:adb5ded007be1fd666fab9affe6764018"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">test_2d_classes</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:adb5ded007be1fd666fab9affe6764018"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <a href="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">More...</a><br /></td></tr>
<tr class="separator:adb5ded007be1fd666fab9affe6764018"><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="../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
<tr class="memdesc:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM that finds the topological pattern. <a href="../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">More...</a><br /></td></tr>
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad9e25202bb8b481461f932668f249dbc"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">test_3d_classes1</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:ad9e25202bb8b481461f932668f249dbc"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <a href="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">More...</a><br /></td></tr>
<tr class="separator:ad9e25202bb8b481461f932668f249dbc"><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="../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
<tr class="memdesc:a0283886819c7c140a023582b7269e2d0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that finds the topological pattern. <a href="../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0">More...</a><br /></td></tr>
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a5bb02a8322d717ead1b11182c5f02a3a"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">test_3d_classes2</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int N)</td></tr>
<tr class="memdesc:a5bb02a8322d717ead1b11182c5f02a3a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <a href="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">More...</a><br /></td></tr>
<tr class="separator:a5bb02a8322d717ead1b11182c5f02a3a"><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="../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e">test3</a> ()</td></tr>
<tr class="memdesc:a6d0455dd5c30adda100e95f0423c786e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM that finds the topological pattern. <a href="../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e">More...</a><br /></td></tr>
<tr class="separator:a6d0455dd5c30adda100e95f0423c786e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2256c10b16edba377b64a44b6c656908"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a> (clock_t start_t, clock_t end_t)</td></tr>
<tr class="memdesc:a2256c10b16edba377b64a44b6c656908"><td class="mdescLeft">&#160;</td><td class="mdescRight">Convert clock cycle difference to time in seconds. <a href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">More...</a><br /></td></tr>
<tr class="separator:a2256c10b16edba377b64a44b6c656908"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d2/df6/kohonen__som__topology_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p ><a href="https://en.wikipedia.org/wiki/Self-organizing_map" target="_blank">Kohonen self organizing map</a> (topological map) </p>
<p >This example implements a powerful unsupervised learning algorithm called as a self organizing map. The algorithm creates a connected network of weights that closely follows the given data points. This thus creates a topological map of the given data i.e., it maintains the relationship between various data points in a much higher dimensional space by creating an equivalent in a 2-dimensional space. <img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/2D_Kohonen_SOM.svg" alt="Trained topological maps for the test cases in the program" style="pointer-events: none;" class="inline"/> </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
<dl class="section warning"><dt>Warning</dt><dd>MSVC 2019 compiler generates code that does not execute as expected. However, MinGW, Clang for GCC and Clang for MSVC compilers on windows perform as expected. Any insights and suggestions should be directed to the author. </dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html" title="Kohonen self organizing map (data tracing)">kohonen_som_trace.c</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a2256c10b16edba377b64a44b6c656908" name="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"><span class="lineno"> 664</span>{</div>
<div class="line"><span class="lineno"> 665</span> <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)(end_t - start_t) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC;</div>
<div class="line"><span class="lineno"> 666</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="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"><span class="lineno"> 670</span>{</div>
<div class="line"><span class="lineno"> 671</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 672</span> printf(<span class="stringliteral">&quot;Using OpenMP based parallelization\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 673</span><span class="preprocessor">#else</span></div>
<div class="line"><span class="lineno"> 674</span> printf(<span class="stringliteral">&quot;NOT using OpenMP based parallelization\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 675</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 676</span> clock_t start_clk, end_clk;</div>
<div class="line"><span class="lineno"> 677</span> </div>
<div class="line"><span class="lineno"> 678</span> start_clk = clock();</div>
<div class="line"><span class="lineno"> 679</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a>();</div>
<div class="line"><span class="lineno"> 680</span> end_clk = clock();</div>
<div class="line"><span class="lineno"> 681</span> printf(<span class="stringliteral">&quot;Test 1 completed in %.4g sec\n&quot;</span>,</div>
<div class="line"><span class="lineno"> 682</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk));</div>
<div class="line"><span class="lineno"> 683</span> </div>
<div class="line"><span class="lineno"> 684</span> start_clk = clock();</div>
<div class="line"><span class="lineno"> 685</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a>();</div>
<div class="line"><span class="lineno"> 686</span> end_clk = clock();</div>
<div class="line"><span class="lineno"> 687</span> printf(<span class="stringliteral">&quot;Test 2 completed in %.4g sec\n&quot;</span>,</div>
<div class="line"><span class="lineno"> 688</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk));</div>
<div class="line"><span class="lineno"> 689</span> </div>
<div class="line"><span class="lineno"> 690</span> start_clk = clock();</div>
<div class="line"><span class="lineno"> 691</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e">test3</a>();</div>
<div class="line"><span class="lineno"> 692</span> end_clk = clock();</div>
<div class="line"><span class="lineno"> 693</span> printf(<span class="stringliteral">&quot;Test 3 completed in %.4g sec\n&quot;</span>,</div>
<div class="line"><span class="lineno"> 694</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk));</div>
<div class="line"><span class="lineno"> 695</span> </div>
<div class="line"><span class="lineno"> 696</span> printf(<span class="stringliteral">&quot;(Note: Calculated times include: writing files to disk.)\n\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 697</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 698</span>}</div>
<div class="ttc" id="akohonen__som__topology_8c_html_a0283886819c7c140a023582b7269e2d0"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a></div><div class="ttdeci">void test2()</div><div class="ttdoc">Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that...</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:506</div></div>
<div class="ttc" id="akohonen__som__topology_8c_html_a1440a7779ac56f47a3f355ce4a8c7da0"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a></div><div class="ttdeci">void test1()</div><div class="ttdoc">Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM t...</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:406</div></div>
<div class="ttc" id="akohonen__som__topology_8c_html_a2256c10b16edba377b64a44b6c656908"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a></div><div class="ttdeci">double get_clock_diff(clock_t start_t, clock_t end_t)</div><div class="ttdoc">Convert clock cycle difference to time in seconds.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:663</div></div>
<div class="ttc" id="akohonen__som__topology_8c_html_a6d0455dd5c30adda100e95f0423c786e"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e">test3</a></div><div class="ttdeci">void test3()</div><div class="ttdoc">Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM ...</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:609</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d2/df6/kohonen__som__topology_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="100%" height="600"><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>
<a id="a1440a7779ac56f47a3f355ce4a8c7da0" name="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 in four clusters in 2D space and trains an SOM that finds the topological pattern. </p>
<p >The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">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 U-matrix</li>
<li><code>w12.csv</code>: trained SOM U-matrix </li>
</ul>
<div class="fragment"><div class="line"><span class="lineno"> 407</span>{</div>
<div class="line"><span class="lineno"> 408</span> <span class="keywordtype">int</span> j, N = 300;</div>
<div class="line"><span class="lineno"> 409</span> <span class="keywordtype">int</span> features = 2;</div>
<div class="line"><span class="lineno"> 410</span> <span class="keywordtype">int</span> num_out = 30; <span class="comment">// image size - N x N</span></div>
<div class="line"><span class="lineno"> 411</span> </div>
<div class="line"><span class="lineno"> 412</span> <span class="comment">// 2D space, hence size = number of rows * 2</span></div>
<div class="line"><span class="lineno"> 413</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><span class="lineno"> 414</span> </div>
<div class="line"><span class="lineno"> 415</span> <span class="comment">// cluster nodex in &#39;x&#39; * cluster nodes in &#39;y&#39; * 2</span></div>
<div class="line"><span class="lineno"> 416</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> W;</div>
<div class="line"><span class="lineno"> 417</span> W.<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a> = num_out;</div>
<div class="line"><span class="lineno"> 418</span> W.dim2 = num_out;</div>
<div class="line"><span class="lineno"> 419</span> W.dim3 = features;</div>
<div class="line"><span class="lineno"> 420</span> W.data = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * num_out * features *</div>
<div class="line"><span class="lineno"> 421</span> <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>)); <span class="comment">// assign rows</span></div>
<div class="line"><span class="lineno"> 422</span> </div>
<div class="line"><span class="lineno"> 423</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++) <span class="comment">// loop till max(N, num_out)</span></div>
<div class="line"><span class="lineno"> 424</span> {</div>
<div class="line"><span class="lineno"> 425</span> <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><span class="lineno"> 426</span> X[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><span class="lineno"> 427</span> <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"><span class="lineno"> 428</span> {</div>
<div class="line"><span class="lineno"> 429</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; num_out; k++)</div>
<div class="line"><span class="lineno"> 430</span> {</div>
<div class="line"><span class="lineno"> 431</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 432</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 433</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 434</span> <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><span class="lineno"> 435</span> <span class="keywordflow">for</span> (j = 0; j &lt; features; j++)</div>
<div class="line"><span class="lineno"> 436</span> {</div>
<div class="line"><span class="lineno"> 437</span> <span class="keywordtype">double</span> *w = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(&amp;W, i, k, j);</div>
<div class="line"><span class="lineno"> 438</span> w[0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-5, 5);</div>
<div class="line"><span class="lineno"> 439</span> }</div>
<div class="line"><span class="lineno"> 440</span> }</div>
<div class="line"><span class="lineno"> 441</span> }</div>
<div class="line"><span class="lineno"> 442</span> }</div>
<div class="line"><span class="lineno"> 443</span> </div>
<div class="line"><span class="lineno"> 444</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">test_2d_classes</a>(X, N); <span class="comment">// create test data around circumference of a circle</span></div>
<div class="line"><span class="lineno"> 445</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<span class="stringliteral">&quot;test1.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><span class="lineno"> 446</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w11.csv&quot;</span>, &amp;W); <span class="comment">// save initial random weights</span></div>
<div class="line"><span class="lineno"> 447</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a>(X, &amp;W, N, features, num_out, 1e-4); <span class="comment">// train the SOM</span></div>
<div class="line"><span class="lineno"> 448</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w12.csv&quot;</span>, &amp;W); <span class="comment">// save the resultant weights</span></div>
<div class="line"><span class="lineno"> 449</span> </div>
<div class="line"><span class="lineno"> 450</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X[i]);</div>
<div class="line"><span class="lineno"> 451</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
<div class="line"><span class="lineno"> 452</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(W.data);</div>
<div class="line"><span class="lineno"> 453</span>}</div>
<div class="ttc" id="agroup__kohonen__2d_html_ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a></div><div class="ttdeci">int save_u_matrix(const char *fname, struct kohonen_array_3d *W)</div><div class="ttdoc">Create the distance matrix or U-matrix from the trained weights and save to disk.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:139</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_ga6824dc6d973eb3339af7aef5fea78b0c"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a></div><div class="ttdeci">int save_2d_data(const char *fname, double **X, int num_points, int num_features)</div><div class="ttdoc">Save a given n-dimensional data martix to file.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:102</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_gacb42eda8af6ebd6a141a34ab00a0b710"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a></div><div class="ttdeci">void kohonen_som(double **X, struct kohonen_array_3d *W, int num_samples, int num_features, int num_out, double alpha_min)</div><div class="ttdoc">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:314</div></div>
<div class="ttc" id="agroup__kohonen__2d_html_gaf5ce14f026d6d231bef29161bac2b485"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a></div><div class="ttdeci">double _random(double a, double b)</div><div class="ttdoc">Helper function to generate a random number in a given interval.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:87</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 class="ttc" id="akohonen__som__topology_8c_html_adb5ded007be1fd666fab9affe6764018"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018">test_2d_classes</a></div><div class="ttdeci">void test_2d_classes(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:366</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 class="ttc" id="astructkohonen__array__3d_html"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a></div><div class="ttdoc">to store info regarding 3D arrays</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:48</div></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="../../d2/df6/kohonen__som__topology_8c_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg" width="616" height="272"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0283886819c7c140a023582b7269e2d0" name="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 in 4 clusters in 3D space and trains an SOM that finds the topological pattern. </p>
<p >The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV</a> files are created to validate the execution:</p><ul>
<li><code>test2.csv</code>: random test samples points</li>
<li><code>w21.csv</code>: initial random U-matrix</li>
<li><code>w22.csv</code>: trained SOM U-matrix </li>
</ul>
<div class="fragment"><div class="line"><span class="lineno"> 507</span>{</div>
<div class="line"><span class="lineno"> 508</span> <span class="keywordtype">int</span> j, N = 500;</div>
<div class="line"><span class="lineno"> 509</span> <span class="keywordtype">int</span> features = 3;</div>
<div class="line"><span class="lineno"> 510</span> <span class="keywordtype">int</span> num_out = 30; <span class="comment">// image size - N x N</span></div>
<div class="line"><span class="lineno"> 511</span> </div>
<div class="line"><span class="lineno"> 512</span> <span class="comment">// 3D space, hence size = number of rows * 3</span></div>
<div class="line"><span class="lineno"> 513</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><span class="lineno"> 514</span> </div>
<div class="line"><span class="lineno"> 515</span> <span class="comment">// cluster nodex in &#39;x&#39; * cluster nodes in &#39;y&#39; * 2</span></div>
<div class="line"><span class="lineno"> 516</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> W;</div>
<div class="line"><span class="lineno"> 517</span> W.<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a> = num_out;</div>
<div class="line"><span class="lineno"> 518</span> W.dim2 = num_out;</div>
<div class="line"><span class="lineno"> 519</span> W.dim3 = features;</div>
<div class="line"><span class="lineno"> 520</span> W.data = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * num_out * features *</div>
<div class="line"><span class="lineno"> 521</span> <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>)); <span class="comment">// assign rows</span></div>
<div class="line"><span class="lineno"> 522</span> </div>
<div class="line"><span class="lineno"> 523</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++) <span class="comment">// loop till max(N, num_out)</span></div>
<div class="line"><span class="lineno"> 524</span> {</div>
<div class="line"><span class="lineno"> 525</span> <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><span class="lineno"> 526</span> X[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><span class="lineno"> 527</span> <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"><span class="lineno"> 528</span> {</div>
<div class="line"><span class="lineno"> 529</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; num_out; k++)</div>
<div class="line"><span class="lineno"> 530</span> {</div>
<div class="line"><span class="lineno"> 531</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 532</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 533</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 534</span> <span class="keywordflow">for</span> (j = 0; j &lt; features; j++)</div>
<div class="line"><span class="lineno"> 535</span> { <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><span class="lineno"> 536</span> <span class="keywordtype">double</span> *w = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(&amp;W, i, k, j);</div>
<div class="line"><span class="lineno"> 537</span> w[0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-5, 5);</div>
<div class="line"><span class="lineno"> 538</span> }</div>
<div class="line"><span class="lineno"> 539</span> }</div>
<div class="line"><span class="lineno"> 540</span> }</div>
<div class="line"><span class="lineno"> 541</span> }</div>
<div class="line"><span class="lineno"> 542</span> </div>
<div class="line"><span class="lineno"> 543</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">test_3d_classes1</a>(X, N); <span class="comment">// create test data</span></div>
<div class="line"><span class="lineno"> 544</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<span class="stringliteral">&quot;test2.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><span class="lineno"> 545</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w21.csv&quot;</span>, &amp;W); <span class="comment">// save initial random weights</span></div>
<div class="line"><span class="lineno"> 546</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a>(X, &amp;W, N, features, num_out, 1e-4); <span class="comment">// train the SOM</span></div>
<div class="line"><span class="lineno"> 547</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w22.csv&quot;</span>, &amp;W); <span class="comment">// save the resultant weights</span></div>
<div class="line"><span class="lineno"> 548</span> </div>
<div class="line"><span class="lineno"> 549</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X[i]);</div>
<div class="line"><span class="lineno"> 550</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
<div class="line"><span class="lineno"> 551</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(W.data);</div>
<div class="line"><span class="lineno"> 552</span>}</div>
<div class="ttc" id="akohonen__som__topology_8c_html_ad9e25202bb8b481461f932668f249dbc"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc">test_3d_classes1</a></div><div class="ttdeci">void test_3d_classes1(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:465</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="../../d2/df6/kohonen__som__topology_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="623" height="272"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a6d0455dd5c30adda100e95f0423c786e" name="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 eight clusters in 3D space and trains an SOM that finds the topological pattern. </p>
<p >The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV</a> files are created to validate the execution:</p><ul>
<li><code>test3.csv</code>: random test samples points</li>
<li><code>w31.csv</code>: initial random U-matrix</li>
<li><code>w32.csv</code>: trained SOM U-matrix </li>
</ul>
<div class="fragment"><div class="line"><span class="lineno"> 610</span>{</div>
<div class="line"><span class="lineno"> 611</span> <span class="keywordtype">int</span> j, N = 500;</div>
<div class="line"><span class="lineno"> 612</span> <span class="keywordtype">int</span> features = 3;</div>
<div class="line"><span class="lineno"> 613</span> <span class="keywordtype">int</span> num_out = 30;</div>
<div class="line"><span class="lineno"> 614</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><span class="lineno"> 615</span> </div>
<div class="line"><span class="lineno"> 616</span> <span class="comment">// cluster nodex in &#39;x&#39; * cluster nodes in &#39;y&#39; * 2</span></div>
<div class="line"><span class="lineno"> 617</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> W;</div>
<div class="line"><span class="lineno"> 618</span> W.<a class="code hl_variable" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a> = num_out;</div>
<div class="line"><span class="lineno"> 619</span> W.dim2 = num_out;</div>
<div class="line"><span class="lineno"> 620</span> W.dim3 = features;</div>
<div class="line"><span class="lineno"> 621</span> W.data = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * num_out * features *</div>
<div class="line"><span class="lineno"> 622</span> <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>)); <span class="comment">// assign rows</span></div>
<div class="line"><span class="lineno"> 623</span> </div>
<div class="line"><span class="lineno"> 624</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(num_out, N); i++) <span class="comment">// loop till max(N, num_out)</span></div>
<div class="line"><span class="lineno"> 625</span> {</div>
<div class="line"><span class="lineno"> 626</span> <span class="keywordflow">if</span> (i &lt; N) <span class="comment">// only add new arrays if i &lt; N</span></div>
<div class="line"><span class="lineno"> 627</span> X[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><span class="lineno"> 628</span> <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"><span class="lineno"> 629</span> {</div>
<div class="line"><span class="lineno"> 630</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; num_out; k++)</div>
<div class="line"><span class="lineno"> 631</span> {</div>
<div class="line"><span class="lineno"> 632</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 633</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 634</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 635</span> <span class="comment">// preallocate with random initial weights</span></div>
<div class="line"><span class="lineno"> 636</span> <span class="keywordflow">for</span> (j = 0; j &lt; features; j++)</div>
<div class="line"><span class="lineno"> 637</span> {</div>
<div class="line"><span class="lineno"> 638</span> <span class="keywordtype">double</span> *w = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gac78022076d889bf2ebfb2c41d4527ceb">kohonen_data_3d</a>(&amp;W, i, k, j);</div>
<div class="line"><span class="lineno"> 639</span> w[0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-5, 5);</div>
<div class="line"><span class="lineno"> 640</span> }</div>
<div class="line"><span class="lineno"> 641</span> }</div>
<div class="line"><span class="lineno"> 642</span> }</div>
<div class="line"><span class="lineno"> 643</span> }</div>
<div class="line"><span class="lineno"> 644</span> </div>
<div class="line"><span class="lineno"> 645</span> <a class="code hl_function" href="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">test_3d_classes2</a>(X, N); <span class="comment">// create test data around the lamniscate</span></div>
<div class="line"><span class="lineno"> 646</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a>(<span class="stringliteral">&quot;test3.csv&quot;</span>, X, N, features); <span class="comment">// save test data points</span></div>
<div class="line"><span class="lineno"> 647</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w31.csv&quot;</span>, &amp;W); <span class="comment">// save initial random weights</span></div>
<div class="line"><span class="lineno"> 648</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a>(X, &amp;W, N, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
<div class="line"><span class="lineno"> 649</span> <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a>(<span class="stringliteral">&quot;w32.csv&quot;</span>, &amp;W); <span class="comment">// save the resultant weights</span></div>
<div class="line"><span class="lineno"> 650</span> </div>
<div class="line"><span class="lineno"> 651</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X[i]);</div>
<div class="line"><span class="lineno"> 652</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
<div class="line"><span class="lineno"> 653</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(W.data);</div>
<div class="line"><span class="lineno"> 654</span>}</div>
<div class="ttc" id="akohonen__som__topology_8c_html_a5bb02a8322d717ead1b11182c5f02a3a"><div class="ttname"><a href="../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a">test_3d_classes2</a></div><div class="ttdeci">void test_3d_classes2(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:564</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="../../d2/df6/kohonen__som__topology_8c_a6d0455dd5c30adda100e95f0423c786e_cgraph.svg" width="623" height="272"><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="adb5ded007be1fd666fab9affe6764018" name="adb5ded007be1fd666fab9affe6764018"></a>
<h2 class="memtitle"><span class="permalink"><a href="#adb5ded007be1fd666fab9affe6764018">&#9670;&nbsp;</a></span>test_2d_classes()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_2d_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"><span class="lineno"> 367</span>{</div>
<div class="line"><span class="lineno"> 368</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.3; <span class="comment">// radius of cluster</span></div>
<div class="line"><span class="lineno"> 369</span> <span class="keywordtype">int</span> i;</div>
<div class="line"><span class="lineno"> 370</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
<div class="line"><span class="lineno"> 371</span> <span class="keyword">const</span> <span class="keywordtype">double</span> centres[][2] = {</div>
<div class="line"><span class="lineno"> 372</span> <span class="comment">// centres of each class cluster</span></div>
<div class="line"><span class="lineno"> 373</span> {.5, .5}, <span class="comment">// centre of class 1</span></div>
<div class="line"><span class="lineno"> 374</span> {.5, -.5}, <span class="comment">// centre of class 2</span></div>
<div class="line"><span class="lineno"> 375</span> {-.5, .5}, <span class="comment">// centre of class 3</span></div>
<div class="line"><span class="lineno"> 376</span> {-.5, -.5} <span class="comment">// centre of class 4</span></div>
<div class="line"><span class="lineno"> 377</span> };</div>
<div class="line"><span class="lineno"> 378</span> </div>
<div class="line"><span class="lineno"> 379</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 380</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 381</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 382</span> <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><span class="lineno"> 383</span> {</div>
<div class="line"><span class="lineno"> 384</span> <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
<div class="line"><span class="lineno"> 385</span> rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
<div class="line"><span class="lineno"> 386</span> </div>
<div class="line"><span class="lineno"> 387</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<div class="line"><span class="lineno"> 388</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
<div class="line"><span class="lineno"> 389</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
<div class="line"><span class="lineno"> 390</span> </div>
<div class="line"><span class="lineno"> 391</span> <span class="comment">/* The follosing can also be used</span></div>
<div class="line"><span class="lineno"> 392</span><span class="comment"> for (int j = 0; j &lt; 2; j++)</span></div>
<div class="line"><span class="lineno"> 393</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
<div class="line"><span class="lineno"> 394</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 395</span> }</div>
<div class="line"><span class="lineno"> 396</span>}</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><!-- 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="../../d2/df6/kohonen__som__topology_8c_adb5ded007be1fd666fab9affe6764018_cgraph.svg" width="248" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ad9e25202bb8b481461f932668f249dbc" name="ad9e25202bb8b481461f932668f249dbc"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad9e25202bb8b481461f932668f249dbc">&#9670;&nbsp;</a></span>test_3d_classes1()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_3d_classes1 </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"><span class="lineno"> 466</span>{</div>
<div class="line"><span class="lineno"> 467</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.2; <span class="comment">// radius of cluster</span></div>
<div class="line"><span class="lineno"> 468</span> <span class="keywordtype">int</span> i;</div>
<div class="line"><span class="lineno"> 469</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
<div class="line"><span class="lineno"> 470</span> <span class="keyword">const</span> <span class="keywordtype">double</span> centres[][3] = {</div>
<div class="line"><span class="lineno"> 471</span> <span class="comment">// centres of each class cluster</span></div>
<div class="line"><span class="lineno"> 472</span> {.5, .5, .5}, <span class="comment">// centre of class 1</span></div>
<div class="line"><span class="lineno"> 473</span> {.5, -.5, -.5}, <span class="comment">// centre of class 2</span></div>
<div class="line"><span class="lineno"> 474</span> {-.5, .5, .5}, <span class="comment">// centre of class 3</span></div>
<div class="line"><span class="lineno"> 475</span> {-.5, -.5 - .5} <span class="comment">// centre of class 4</span></div>
<div class="line"><span class="lineno"> 476</span> };</div>
<div class="line"><span class="lineno"> 477</span> </div>
<div class="line"><span class="lineno"> 478</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 479</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 480</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 481</span> <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><span class="lineno"> 482</span> {</div>
<div class="line"><span class="lineno"> 483</span> <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
<div class="line"><span class="lineno"> 484</span> rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
<div class="line"><span class="lineno"> 485</span> </div>
<div class="line"><span class="lineno"> 486</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<div class="line"><span class="lineno"> 487</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
<div class="line"><span class="lineno"> 488</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
<div class="line"><span class="lineno"> 489</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][2] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][2] - R, centres[<span class="keyword">class</span>][2] + R);</div>
<div class="line"><span class="lineno"> 490</span> </div>
<div class="line"><span class="lineno"> 491</span> <span class="comment">/* The follosing can also be used</span></div>
<div class="line"><span class="lineno"> 492</span><span class="comment"> for (int j = 0; j &lt; 3; j++)</span></div>
<div class="line"><span class="lineno"> 493</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
<div class="line"><span class="lineno"> 494</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 495</span> }</div>
<div class="line"><span class="lineno"> 496</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="../../d2/df6/kohonen__som__topology_8c_ad9e25202bb8b481461f932668f249dbc_cgraph.svg" width="255" 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="a5bb02a8322d717ead1b11182c5f02a3a" name="a5bb02a8322d717ead1b11182c5f02a3a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a5bb02a8322d717ead1b11182c5f02a3a">&#9670;&nbsp;</a></span>test_3d_classes2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test_3d_classes2 </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"><span class="lineno"> 565</span>{</div>
<div class="line"><span class="lineno"> 566</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.2; <span class="comment">// radius of cluster</span></div>
<div class="line"><span class="lineno"> 567</span> <span class="keywordtype">int</span> i;</div>
<div class="line"><span class="lineno"> 568</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 8;</div>
<div class="line"><span class="lineno"> 569</span> <span class="keyword">const</span> <span class="keywordtype">double</span> centres[][3] = {</div>
<div class="line"><span class="lineno"> 570</span> <span class="comment">// centres of each class cluster</span></div>
<div class="line"><span class="lineno"> 571</span> {.5, .5, .5}, <span class="comment">// centre of class 1</span></div>
<div class="line"><span class="lineno"> 572</span> {.5, .5, -.5}, <span class="comment">// centre of class 2</span></div>
<div class="line"><span class="lineno"> 573</span> {.5, -.5, .5}, <span class="comment">// centre of class 3</span></div>
<div class="line"><span class="lineno"> 574</span> {.5, -.5, -.5}, <span class="comment">// centre of class 4</span></div>
<div class="line"><span class="lineno"> 575</span> {-.5, .5, .5}, <span class="comment">// centre of class 5</span></div>
<div class="line"><span class="lineno"> 576</span> {-.5, .5, -.5}, <span class="comment">// centre of class 6</span></div>
<div class="line"><span class="lineno"> 577</span> {-.5, -.5, .5}, <span class="comment">// centre of class 7</span></div>
<div class="line"><span class="lineno"> 578</span> {-.5, -.5, -.5} <span class="comment">// centre of class 8</span></div>
<div class="line"><span class="lineno"> 579</span> };</div>
<div class="line"><span class="lineno"> 580</span> </div>
<div class="line"><span class="lineno"> 581</span><span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><span class="lineno"> 582</span><span class="preprocessor">#pragma omp for</span></div>
<div class="line"><span class="lineno"> 583</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 584</span> <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><span class="lineno"> 585</span> {</div>
<div class="line"><span class="lineno"> 586</span> <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
<div class="line"><span class="lineno"> 587</span> rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
<div class="line"><span class="lineno"> 588</span> </div>
<div class="line"><span class="lineno"> 589</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
<div class="line"><span class="lineno"> 590</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
<div class="line"><span class="lineno"> 591</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
<div class="line"><span class="lineno"> 592</span> <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i][2] = <a class="code hl_function" href="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][2] - R, centres[<span class="keyword">class</span>][2] + R);</div>
<div class="line"><span class="lineno"> 593</span> </div>
<div class="line"><span class="lineno"> 594</span> <span class="comment">/* The follosing can also be used</span></div>
<div class="line"><span class="lineno"> 595</span><span class="comment"> for (int j = 0; j &lt; 3; j++)</span></div>
<div class="line"><span class="lineno"> 596</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
<div class="line"><span class="lineno"> 597</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 598</span> }</div>
<div class="line"><span class="lineno"> 599</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="../../d2/df6/kohonen__som__topology_8c_a5bb02a8322d717ead1b11182c5f02a3a_cgraph.svg" width="255" 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="navelem"><a class="el" href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html">kohonen_som_topology.c</a></li>
<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.3 </li>
</ul>
</div>
</body>
</html>