2020-06-28 23:07:29 +08:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/xhtml;charset=UTF-8" / >
< meta http-equiv = "X-UA-Compatible" content = "IE=9" / >
< meta name = "generator" content = "Doxygen 1.8.18" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1" / >
< title > Algorithms_in_C: machine_learning/kohonen_som_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 style = "height: 56px;" >
< td id = "projectalign" style = "padding-left: 0.5em;" >
< div id = "projectname" > Algorithms_in_C
  < span id = "projectnumber" > 1.0.0< / span >
< / div >
< div id = "projectbrief" > Set of algorithms implemented in C.< / div >
< / td >
< / tr >
< / tbody >
< / table >
< / div >
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @license-end */
< / script >
< script type = "text/javascript" src = "../../menudata.js" > < / script >
< script type = "text/javascript" src = "../../menu.js" > < / script >
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */< / script >
< div id = "main-nav" > < / div >
< / div > <!-- top -->
< div id = "side-nav" class = "ui-resizable side-nav-resizable" >
< div id = "nav-tree" >
< div id = "nav-tree-contents" >
< div id = "nav-sync" class = "sync" > < / div >
< / div >
< / div >
< div id = "splitbar" style = "-moz-user-select:none;"
class="ui-resizable-handle">
< / div >
< / div >
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(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 > |
< a href = "#define-members" > Macros< / a > |
< 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" > Kohonen self organizing map< / a > (topological map)
< a href = "#details" > More...< / a > < / p >
< div class = "textblock" > < code > #include < math.h> < / code > < br / >
< code > #include < stdio.h> < / code > < br / >
< code > #include < stdlib.h> < / code > < br / >
< code > #include < time.h> < / 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 name = "nested-classes" > < / a >
Data Structures< / h2 > < / td > < / tr >
< tr class = "memitem:" > < td class = "memItemLeft" align = "right" valign = "top" > struct   < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > < / td > < / tr >
< tr class = "separator:" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< / table > < table class = "memberdecls" >
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a name = "define-members" > < / a >
Macros< / h2 > < / td > < / tr >
< tr class = "memitem:a525335710b53cb064ca56b936120431e" > < td class = "memItemLeft" align = "right" valign = "top" > #define  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a525335710b53cb064ca56b936120431e" > _USE_MATH_DEFINES< / a > < / td > < / tr >
< tr class = "separator:a525335710b53cb064ca56b936120431e" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:affe776513b24d84b39af8ab0930fef7f" > < td class = "memItemLeft" align = "right" valign = "top" > #define  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (a, b)      (((a) > (b)) ? (a) : (b))< / td > < / tr >
< tr class = "separator:affe776513b24d84b39af8ab0930fef7f" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:ac6afabdc09a49a433ee19d8a9486056d" > < td class = "memItemLeft" align = "right" valign = "top" > #define  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > (a, b)      (((a) < (b)) ? (a) : (b))< / td > < / tr >
< tr class = "separator:ac6afabdc09a49a433ee19d8a9486056d" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< / table > < table class = "memberdecls" >
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a name = "func-members" > < / a >
Functions< / h2 > < / td > < / tr >
< tr class = "memitem:a7237d2cf3b3f9d034477268c1d6631da" > < td class = "memItemLeft" align = "right" valign = "top" > double *  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (const struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *arr, int x, int y, int z)< / td > < / tr >
< tr class = "separator:a7237d2cf3b3f9d034477268c1d6631da" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:af5ce14f026d6d231bef29161bac2b485" > < td class = "memItemLeft" align = "right" valign = "top" > double  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (double a, double b)< / td > < / tr >
< tr class = "separator:af5ce14f026d6d231bef29161bac2b485" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a6824dc6d973eb3339af7aef5fea78b0c" > < td class = "memItemLeft" align = "right" valign = "top" > int  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c" > save_2d_data< / a > (const char *fname, double **X, int num_points, int num_features)< / td > < / tr >
< tr class = "separator:a6824dc6d973eb3339af7aef5fea78b0c" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:adee1bc3871c881f6e04cb9e0b1d01158" > < td class = "memItemLeft" align = "right" valign = "top" > int  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (const char *fname, struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *W)< / td > < / tr >
< tr class = "separator:adee1bc3871c881f6e04cb9e0b1d01158" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:adc22d512c00a9f5799ee067f4fb90b4b" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b" > get_min_2d< / a > (double **X, int < a class = "el" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , double *val, int *x_idx, int *y_idx)< / td > < / tr >
< tr class = "separator:adc22d512c00a9f5799ee067f4fb90b4b" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:ad139de302fa8135a433d9c1bf971b13b" > < td class = "memItemLeft" align = "right" valign = "top" > double  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#ad139de302fa8135a433d9c1bf971b13b" > update_weights< / a > (const double *X, struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *W, double **D, int num_out, int num_features, double alpha, int R)< / td > < / tr >
< tr class = "separator:ad139de302fa8135a433d9c1bf971b13b" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a1983dc0f35e734015772e9f8c0ff19f5" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5" > kohonen_som< / a > (double **X, struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *W, int num_samples, int num_features, int num_out, double alpha_min)< / td > < / tr >
< tr class = "separator:a1983dc0f35e734015772e9f8c0ff19f5" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:adb5ded007be1fd666fab9affe6764018" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / 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 < a class = "el" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > )< / td > < / tr >
< tr class = "separator:adb5ded007be1fd666fab9affe6764018" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a1440a7779ac56f47a3f355ce4a8c7da0" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0" > test1< / a > ()< / td > < / tr >
< tr class = "separator:a1440a7779ac56f47a3f355ce4a8c7da0" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:ad9e25202bb8b481461f932668f249dbc" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / 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 < a class = "el" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > )< / td > < / tr >
< tr class = "separator:ad9e25202bb8b481461f932668f249dbc" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a0283886819c7c140a023582b7269e2d0" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a0283886819c7c140a023582b7269e2d0" > test2< / a > ()< / td > < / tr >
< tr class = "separator:a0283886819c7c140a023582b7269e2d0" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a5bb02a8322d717ead1b11182c5f02a3a" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / 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 < a class = "el" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > )< / td > < / tr >
< tr class = "separator:a5bb02a8322d717ead1b11182c5f02a3a" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a6d0455dd5c30adda100e95f0423c786e" > < td class = "memItemLeft" align = "right" valign = "top" > void  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "../../d2/df6/kohonen__som__topology_8c.html#a6d0455dd5c30adda100e95f0423c786e" > test3< / a > ()< / td > < / tr >
< tr class = "separator:a6d0455dd5c30adda100e95f0423c786e" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a2256c10b16edba377b64a44b6c656908" > < td class = "memItemLeft" align = "right" valign = "top" > double  < / 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 = "separator:a2256c10b16edba377b64a44b6c656908" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a3c04138a5bfe5d72780bb7e82a18e627" > < td class = "memItemLeft" align = "right" valign = "top" > int  < / 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 = "separator:a3c04138a5bfe5d72780bb7e82a18e627" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< / table >
< a name = "details" id = "details" > < / a > < h2 class = "groupheader" > Detailed Description< / h2 >
< div class = "textblock" > < p > < a href = "https://en.wikipedia.org/wiki/Self-organizing_map" > Kohonen self organizing map< / a > (topological map) < / p >
2020-06-28 23:30:37 +08:00
< dl class = "section author" > < dt > Author< / dt > < dd > < a href = "https://github.com/kvedala" > Krishna Vedala< / a > 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 varipus data points in a much higher dimesional 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" / > < / dd > < / dl >
2020-06-28 23:07:29 +08:00
< 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" > Macro Definition Documentation< / h2 >
< a id = "a525335710b53cb064ca56b936120431e" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a525335710b53cb064ca56b936120431e" > ◆ < / a > < / span > _USE_MATH_DEFINES< / h2 >
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
< td class = "memname" > #define _USE_MATH_DEFINES< / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > required for MS Visual C < / p >
< / div >
< / div >
< a id = "affe776513b24d84b39af8ab0930fef7f" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#affe776513b24d84b39af8ab0930fef7f" > ◆ < / a > < / span > max< / h2 >
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
< td class = "memname" > #define max< / td >
< td > (< / td >
< td class = "paramtype" >   < / td >
< td class = "paramname" > a, < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" >   < / td >
< td class = "paramname" > b  < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td >       (((a) > (b)) ? (a) : (b))< / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > shorthand for maximum value \ < / p >
< / div >
< / div >
< a id = "ac6afabdc09a49a433ee19d8a9486056d" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#ac6afabdc09a49a433ee19d8a9486056d" > ◆ < / a > < / span > min< / h2 >
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
< td class = "memname" > #define min< / td >
< td > (< / td >
< td class = "paramtype" >   < / td >
< td class = "paramname" > a, < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" >   < / td >
< td class = "paramname" > b  < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td >       (((a) < (b)) ? (a) : (b))< / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > shorthand for minimum value \ < / p >
< / div >
< / div >
< h2 class = "groupheader" > Function Documentation< / h2 >
< a id = "af5ce14f026d6d231bef29161bac2b485" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#af5ce14f026d6d231bef29161bac2b485" > ◆ < / 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  < / td >
< td class = "paramname" > < em > a< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double  < / td >
< td class = "paramname" > < em > b< / em >   < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td > < / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > Helper function to generate a random number in a given interval. < br / >
Steps:< / p > < ol type = "1" >
< li > < code > r1 = rand() % 100< / code > gets a random number between 0 and 99< / li >
< li > < code > r2 = r1 / 100< / code > converts random number to be between 0 and 0.99< / li >
< li > scale and offset the random number to given range of \([a,b)\) < p class = "formulaDsp" >
\[ y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \; \text{mod}\; 100}{100} + a \]
< / p >
< / li >
< / ol >
< dl class = "params" > < dt > Parameters< / dt > < dd >
< table class = "params" >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > a< / td > < td > lower limit < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > b< / td > < td > upper limit < / td > < / tr >
< / table >
< / dd >
< / dl >
< dl class = "section return" > < dt > Returns< / dt > < dd > random number in the range \([a,b)\) < / dd > < / dl >
< div class = "fragment" > < div class = "line" > < a name = "l00099" > < / a > < span class = "lineno" > 99< / span >   {< / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a7237d2cf3b3f9d034477268c1d6631da" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a7237d2cf3b3f9d034477268c1d6631da" > ◆ < / a > < / span > data_3d()< / h2 >
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
< td class = "memname" > double* data_3d < / td >
< td > (< / td >
< td class = "paramtype" > const struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *  < / td >
< td class = "paramname" > < em > arr< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > x< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > y< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > z< / em >   < / 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 = "../../d0/dc8/structarray__3d.html" > 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 name = "l00083" > < / a > < span class = "lineno" > 83< / span >   {< / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a2256c10b16edba377b64a44b6c656908" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a2256c10b16edba377b64a44b6c656908" > ◆ < / 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  < / td >
< td class = "paramname" > < em > start_t< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > clock_t  < / td >
< td class = "paramname" > < em > end_t< / em >   < / 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 >
2020-06-28 23:30:37 +08:00
< div class = "fragment" > < div class = "line" > < a name = "l00660" > < / a > < span class = "lineno" > 660< / span >   {< / div >
2020-06-28 23:07:29 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "adc22d512c00a9f5799ee067f4fb90b4b" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#adc22d512c00a9f5799ee067f4fb90b4b" > ◆ < / 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 **  < / td >
< td class = "paramname" > < em > X< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > N< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double *  < / td >
< td class = "paramname" > < em > val< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int *  < / td >
< td class = "paramname" > < em > x_idx< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int *  < / td >
< td class = "paramname" > < em > y_idx< / em >   < / 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" > idx_x< / td > < td > x-index where minimum value was found < / td > < / tr >
< tr > < td class = "paramdir" > [out]< / td > < td class = "paramname" > idx_y< / td > < td > y-index where minimum value was found < / td > < / tr >
< / table >
< / dd >
< / dl >
< div class = "fragment" > < div class = "line" > < a name = "l00204" > < / a > < span class = "lineno" > 204< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00205" > < / a > < span class = "lineno" > 205< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > j = 0; j < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; j++) < span class = "comment" > // traverse each y-index< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00206" > < / a > < span class = "lineno" > 206< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00207" > < / a > < span class = "lineno" > 207< / span >   < span class = "keywordflow" > if< / span > (X[i][j] < val[0]) < span class = "comment" > // if a lower value is found< / span > < / div >
< div class = "line" > < a name = "l00208" > < / a > < span class = "lineno" > 208< / span >   { < span class = "comment" > // save the value and its index< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00209" > < / a > < span class = "lineno" > 209< / span >   x_idx[0] = i;< / div >
< div class = "line" > < a name = "l00210" > < / a > < span class = "lineno" > 210< / span >   y_idx[0] = j;< / div >
< div class = "line" > < a name = "l00211" > < / a > < span class = "lineno" > 211< / span >   val[0] = X[i][j];< / div >
< div class = "line" > < a name = "l00212" > < / a > < span class = "lineno" > 212< / span >   }< / div >
< div class = "line" > < a name = "l00213" > < / a > < span class = "lineno" > 213< / span >   }< / div >
< div class = "line" > < a name = "l00214" > < / a > < span class = "lineno" > 214< / span >   }< / div >
< div class = "line" > < a name = "l00215" > < / a > < span class = "lineno" > 215< / span >   }< / div >
< div class = "line" > < a name = "l00216" > < / a > < span class = "lineno" > 216< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00217" > < / a > < span class = "lineno" > 217< / span >   < span class = "comment" > /**< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a1983dc0f35e734015772e9f8c0ff19f5" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a1983dc0f35e734015772e9f8c0ff19f5" > ◆ < / 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 **  < / td >
< td class = "paramname" > < em > X< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *  < / td >
< td class = "paramname" > < em > W< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_samples< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_features< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_out< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double  < / td >
< td class = "paramname" > < em > alpha_min< / em >   < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td > < / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.< / p >
< dl class = "params" > < dt > Parameters< / dt > < dd >
< table class = "params" >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > X< / td > < td > data set < / td > < / tr >
< tr > < td class = "paramdir" > [in,out]< / td > < td class = "paramname" > W< / td > < td > weights matrix < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > D< / td > < td > temporary vector to store distances < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > num_samples< / td > < td > number of output points < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > num_features< / td > < td > number of features per input sample < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > num_out< / td > < td > number of output points < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > alpha_min< / td > < td > terminal value of alpha < / td > < / tr >
< / table >
< / dd >
< / dl >
< div class = "fragment" > < div class = "line" > < a name = "l00311" > < / a > < span class = "lineno" > 311< / span >   {< / div >
< div class = "line" > < a name = "l00312" > < / a > < span class = "lineno" > 312< / span >   < span class = "keywordtype" > int< / span > R = num_out > > 2, iter = 0;< / div >
< div class = "line" > < a name = "l00313" > < / a > < span class = "lineno" > 313< / span >   < span class = "keywordtype" > double< / span > **D = (< span class = "keywordtype" > double< / span > **)malloc(num_out * < span class = "keyword" > sizeof< / span > (< span class = "keywordtype" > double< / span > *));< / div >
< div class = "line" > < a name = "l00314" > < / a > < span class = "lineno" > 314< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < num_out; i++)< / div >
< div class = "line" > < a name = "l00315" > < / a > < span class = "lineno" > 315< / span >   D[i] = (< span class = "keywordtype" > double< / span > *)malloc(num_out * < span class = "keyword" > sizeof< / span > (< span class = "keywordtype" > double< / span > ));< / div >
< div class = "line" > < a name = "l00316" > < / a > < span class = "lineno" > 316< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00317" > < / a > < span class = "lineno" > 317< / span >   < span class = "keywordtype" > double< / span > dmin = 1.f; < span class = "comment" > // average minimum distance of all samples< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00318" > < / a > < span class = "lineno" > 318< / span >   < / div >
< div class = "line" > < a name = "l00319" > < / a > < span class = "lineno" > 319< / span >   < span class = "comment" > // Loop alpha from 1 to slpha_min< / span > < / div >
< div class = "line" > < a name = "l00320" > < / a > < span class = "lineno" > 320< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > double< / span > alpha = 1.f; alpha > alpha_min & & dmin > 1e-3;< / div >
< div class = "line" > < a name = "l00321" > < / a > < span class = "lineno" > 321< / span >   alpha -= 0.001, iter++)< / div >
< div class = "line" > < a name = "l00322" > < / a > < span class = "lineno" > 322< / span >   {< / div >
< div class = "line" > < a name = "l00323" > < / a > < span class = "lineno" > 323< / span >   dmin = 0.f;< / div >
< div class = "line" > < a name = "l00324" > < / a > < span class = "lineno" > 324< / span >   < span class = "comment" > // Loop for each sample pattern in the data set< / span > < / div >
< div class = "line" > < a name = "l00325" > < / a > < span class = "lineno" > 325< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > sample = 0; sample < num_samples; sample++)< / div >
< div class = "line" > < a name = "l00326" > < / a > < span class = "lineno" > 326< / span >   {< / div >
< div class = "line" > < a name = "l00327" > < / a > < span class = "lineno" > 327< / span >   < span class = "comment" > // update weights for the current input pattern sample< / span > < / div >
< div class = "line" > < a name = "l00328" > < / a > < span class = "lineno" > 328< / span >   dmin += < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ad139de302fa8135a433d9c1bf971b13b" > update_weights< / a > (X[sample], W, D, num_out, num_features,< / div >
< div class = "line" > < a name = "l00329" > < / a > < span class = "lineno" > 329< / span >   alpha, R);< / div >
< div class = "line" > < a name = "l00330" > < / a > < span class = "lineno" > 330< / span >   }< / div >
< div class = "line" > < a name = "l00331" > < / a > < span class = "lineno" > 331< / span >   < / div >
< div class = "line" > < a name = "l00332" > < / a > < span class = "lineno" > 332< / span >   < span class = "comment" > // every 20th iteration, reduce the neighborhood range< / span > < / div >
< div class = "line" > < a name = "l00333" > < / a > < span class = "lineno" > 333< / span >   < span class = "keywordflow" > if< / span > (iter % 100 == 0 & & R > 1)< / div >
< div class = "line" > < a name = "l00334" > < / a > < span class = "lineno" > 334< / span >   R--;< / div >
< div class = "line" > < a name = "l00335" > < / a > < span class = "lineno" > 335< / span >   < / div >
< div class = "line" > < a name = "l00336" > < / a > < span class = "lineno" > 336< / span >   dmin /= num_samples;< / div >
< div class = "line" > < a name = "l00337" > < / a > < span class = "lineno" > 337< / span >   printf(< span class = "stringliteral" > " iter: %5d\t alpha: %.4g\t R: %d\td_min: %.4g\r" < / span > , iter, alpha, R,< / div >
< div class = "line" > < a name = "l00338" > < / a > < span class = "lineno" > 338< / span >   dmin);< / div >
< div class = "line" > < a name = "l00339" > < / a > < span class = "lineno" > 339< / span >   }< / div >
< div class = "line" > < a name = "l00340" > < / a > < span class = "lineno" > 340< / span >   putchar(< span class = "charliteral" > ' \n' < / span > );< / div >
< div class = "line" > < a name = "l00341" > < / a > < span class = "lineno" > 341< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00342" > < / a > < span class = "lineno" > 342< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < num_out; i++) free(D[i]);< / div >
< div class = "line" > < a name = "l00343" > < / a > < span class = "lineno" > 343< / span >   free(D);< / div >
< div class = "line" > < a name = "l00344" > < / a > < span class = "lineno" > 344< / span >   }< / div >
< div class = "line" > < a name = "l00345" > < / a > < span class = "lineno" > 345< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00346" > < / a > < span class = "lineno" > 346< / span >   < span class = "comment" > /** Creates a random set of points distributed in four clusters in< / span > < / div >
2020-06-28 23:07:29 +08:00
< / 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_a1983dc0f35e734015772e9f8c0ff19f5_cgraph.svg" width = "412" 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 = "a3c04138a5bfe5d72780bb7e82a18e627" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a3c04138a5bfe5d72780bb7e82a18e627" > ◆ < / 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  < / td >
< td class = "paramname" > < em > argc< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > char **  < / td >
< td class = "paramname" > < em > argv< / em >   < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td > < / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > Main function < / p >
2020-06-28 23:30:37 +08:00
< div class = "fragment" > < div class = "line" > < a name = "l00686" > < / a > < span class = "lineno" > 686< / span >   : Calculated times include: writing files to disk.)\n\n< span class = "stringliteral" > " );< / span > < / div >
< div class = "line" > < a name = "l00687" > < / a > < span class = "lineno" > 687< / span >   < span class = "stringliteral" > return 0;< / span > < / div >
< div class = "line" > < a name = "l00688" > < / a > < span class = "lineno" > 688< / span >   < span class = "stringliteral" > }< / span > < / div >
2020-06-28 23:07:29 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a6824dc6d973eb3339af7aef5fea78b0c" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a6824dc6d973eb3339af7aef5fea78b0c" > ◆ < / 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 *  < / td >
< td class = "paramname" > < em > fname< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double **  < / td >
< td class = "paramname" > < em > X< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_points< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_features< / em >   < / td >
< / tr >
< tr >
< td > < / td >
< td > )< / td >
< td > < / td > < td > < / td >
< / tr >
< / table >
< / div > < div class = "memdoc" >
< p > Save a given n-dimensional data martix to file.< / p >
< dl class = "params" > < dt > Parameters< / dt > < dd >
< table class = "params" >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > fname< / td > < td > filename to save in (gets overwriten without confirmation) < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > X< / td > < td > matrix to save < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > num_points< / td > < td > rows in the matrix = number of points < / td > < / tr >
< tr > < td class = "paramdir" > [in]< / td > < td class = "paramname" > num_features< / td > < td > columns in the matrix = dimensions of points < / td > < / tr >
< / table >
< / dd >
< / dl >
< dl class = "section return" > < dt > Returns< / dt > < dd > 0 if all ok < / dd >
< dd >
-1 if file creation failed < / dd > < / dl >
< div class = "fragment" > < div class = "line" > < a name = "l00099" > < / a > < span class = "lineno" > 99< / span >   {< / div >
< div class = "line" > < a name = "l00100" > < / a > < span class = "lineno" > 100< / span >   FILE *fp = fopen(fname, < span class = "stringliteral" > " wt" < / span > );< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00101" > < / a > < span class = "lineno" > 101< / span >   < span class = "keywordflow" > if< / span > (!fp) < span class = "comment" > // error with fopen< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00102" > < / a > < span class = "lineno" > 102< / span >   {< / div >
< div class = "line" > < a name = "l00103" > < / a > < span class = "lineno" > 103< / span >   < span class = "keywordtype" > char< / span > msg[120];< / div >
< div class = "line" > < a name = "l00104" > < / a > < span class = "lineno" > 104< / span >   sprintf(msg, < span class = "stringliteral" > " File error (%s): " < / span > , fname);< / div >
< div class = "line" > < a name = "l00105" > < / a > < span class = "lineno" > 105< / span >   perror(msg);< / div >
< div class = "line" > < a name = "l00106" > < / a > < span class = "lineno" > 106< / span >   < span class = "keywordflow" > return< / span > -1;< / div >
< div class = "line" > < a name = "l00107" > < / a > < span class = "lineno" > 107< / span >   }< / div >
< div class = "line" > < a name = "l00108" > < / a > < span class = "lineno" > 108< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00109" > < / a > < span class = "lineno" > 109< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < num_points; i++) < span class = "comment" > // for each point in the array< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00110" > < / a > < span class = "lineno" > 110< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00111" > < / a > < span class = "lineno" > 111< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > j = 0; j < num_features; j++) < span class = "comment" > // for each feature in the array< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00112" > < / a > < span class = "lineno" > 112< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00113" > < / a > < span class = "lineno" > 113< / span >   fprintf(fp, < span class = "stringliteral" > " %.4g" < / span > , X[i][j]); < span class = "comment" > // print the feature value< / span > < / div >
< div class = "line" > < a name = "l00114" > < / a > < span class = "lineno" > 114< / span >   < span class = "keywordflow" > if< / span > (j < num_features - 1) < span class = "comment" > // if not the last feature< / span > < / div >
< div class = "line" > < a name = "l00115" > < / a > < span class = "lineno" > 115< / span >   fputc(< span class = "charliteral" > ' ,' < / span > , fp); < span class = "comment" > // suffix comma< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00116" > < / a > < span class = "lineno" > 116< / span >   }< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00117" > < / a > < span class = "lineno" > 117< / span >   < span class = "keywordflow" > if< / span > (i < num_points - 1) < span class = "comment" > // if not the last row< / span > < / div >
< div class = "line" > < a name = "l00118" > < / a > < span class = "lineno" > 118< / span >   fputc(< span class = "charliteral" > ' \n' < / span > , fp); < span class = "comment" > // start a new line< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00119" > < / a > < span class = "lineno" > 119< / span >   }< / div >
< div class = "line" > < a name = "l00120" > < / a > < span class = "lineno" > 120< / span >   fclose(fp);< / div >
< div class = "line" > < a name = "l00121" > < / a > < span class = "lineno" > 121< / span >   < span class = "keywordflow" > return< / span > 0;< / div >
< div class = "line" > < a name = "l00122" > < / a > < span class = "lineno" > 122< / span >   }< / div >
< div class = "line" > < a name = "l00123" > < / a > < span class = "lineno" > 123< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00124" > < / a > < span class = "lineno" > 124< / span >   < span class = "comment" > /**< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "adee1bc3871c881f6e04cb9e0b1d01158" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#adee1bc3871c881f6e04cb9e0b1d01158" > ◆ < / 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 *  < / td >
< td class = "paramname" > < em > fname< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *  < / td >
< td class = "paramname" > < em > W< / em >   < / 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" > 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 name = "l00138" > < / a > < span class = "lineno" > 138< / span >   {< / div >
< div class = "line" > < a name = "l00139" > < / a > < span class = "lineno" > 139< / span >   < span class = "keywordtype" > char< / span > msg[120];< / div >
< div class = "line" > < a name = "l00140" > < / a > < span class = "lineno" > 140< / span >   sprintf(msg, < span class = "stringliteral" > " File error (%s): " < / span > , fname);< / div >
< div class = "line" > < a name = "l00141" > < / a > < span class = "lineno" > 141< / span >   perror(msg);< / div >
< div class = "line" > < a name = "l00142" > < / a > < span class = "lineno" > 142< / span >   < span class = "keywordflow" > return< / span > -1;< / div >
< div class = "line" > < a name = "l00143" > < / a > < span class = "lineno" > 143< / span >   }< / div >
< div class = "line" > < a name = "l00144" > < / a > < span class = "lineno" > 144< / span >   < / div >
< div class = "line" > < a name = "l00145" > < / a > < span class = "lineno" > 145< / span >   < span class = "keywordtype" > int< / span > R = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4" > dim1< / a > > > 3, 2); < span class = "comment" > /* neighborhood range */< / span > < / div >
< div class = "line" > < a name = "l00146" > < / a > < span class = "lineno" > 146< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00147" > < / a > < span class = "lineno" > 147< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4" > dim1< / a > ; i++) < span class = "comment" > // for each x< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00148" > < / a > < span class = "lineno" > 148< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00149" > < / a > < span class = "lineno" > 149< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > j = 0; j < W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#aa431cc0a41fd379270dbd2b0e3ac1b45" > dim2< / a > ; j++) < span class = "comment" > // for each y< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00150" > < / a > < span class = "lineno" > 150< / span >   {< / div >
< div class = "line" > < a name = "l00151" > < / a > < span class = "lineno" > 151< / span >   < span class = "keywordtype" > double< / span > distance = 0.f;< / div >
< div class = "line" > < a name = "l00152" > < / a > < span class = "lineno" > 152< / span >   < span class = "keywordtype" > int< / span > k;< / div >
< div class = "line" > < a name = "l00153" > < / a > < span class = "lineno" > 153< / span >   < / div >
< div class = "line" > < a name = "l00154" > < / a > < span class = "lineno" > 154< / span >   < span class = "keywordtype" > int< / span > from_x = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (0, i - R);< / div >
< div class = "line" > < a name = "l00155" > < / a > < span class = "lineno" > 155< / span >   < span class = "keywordtype" > int< / span > to_x = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > (W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4" > dim1< / a > , i + R + 1);< / div >
< div class = "line" > < a name = "l00156" > < / a > < span class = "lineno" > 156< / span >   < span class = "keywordtype" > int< / span > from_y = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (0, j - R);< / div >
< div class = "line" > < a name = "l00157" > < / a > < span class = "lineno" > 157< / span >   < span class = "keywordtype" > int< / span > to_y = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > (W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#aa431cc0a41fd379270dbd2b0e3ac1b45" > dim2< / a > , j + R + 1);< / div >
< div class = "line" > < a name = "l00158" > < / a > < span class = "lineno" > 158< / span >   < span class = "keywordtype" > int< / span > l;< / div >
< div class = "line" > < a name = "l00159" > < / a > < span class = "lineno" > 159< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00160" > < / a > < span class = "lineno" > 160< / span >   < span class = "preprocessor" > #pragma omp parallel for reduction(+ : distance)< / span > < / div >
< div class = "line" > < a name = "l00161" > < / a > < span class = "lineno" > 161< / span >   < span class = "preprocessor" > #endif< / span > < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00162" > < / a > < span class = "lineno" > 162< / span >   < span class = "keywordflow" > for< / span > (l = from_x; l < to_x; l++) < span class = "comment" > // scan neighborhoor in x< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00163" > < / a > < span class = "lineno" > 163< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00164" > < / a > < span class = "lineno" > 164< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > m = from_y; m < to_y; m++) < span class = "comment" > // scan neighborhood in y< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00165" > < / a > < span class = "lineno" > 165< / span >   {< / div >
< div class = "line" > < a name = "l00166" > < / a > < span class = "lineno" > 166< / span >   < span class = "keywordtype" > double< / span > d = 0.f;< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00167" > < / a > < span class = "lineno" > 167< / span >   < span class = "keywordflow" > for< / span > (k = 0; k < W-> < a class = "code" href = "../../d0/dc8/structarray__3d.html#ad823bd5424a0fc158da52a0497d8c10a" > dim3< / a > ; k++) < span class = "comment" > // for each feature< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00168" > < / a > < span class = "lineno" > 168< / span >   {< / div >
< div class = "line" > < a name = "l00169" > < / a > < span class = "lineno" > 169< / span >   < span class = "keywordtype" > double< / span > *w1 = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (W, i, j, k);< / div >
< div class = "line" > < a name = "l00170" > < / a > < span class = "lineno" > 170< / span >   < span class = "keywordtype" > double< / span > *w2 = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (W, l, m, k);< / div >
< div class = "line" > < a name = "l00171" > < / a > < span class = "lineno" > 171< / span >   d += (w1[0] - w2[0]) * (w1[0] - w2[0]);< / div >
< div class = "line" > < a name = "l00172" > < / a > < span class = "lineno" > 172< / span >   < span class = "comment" > // distance += w1[0] * w1[0];< / span > < / div >
< div class = "line" > < a name = "l00173" > < / a > < span class = "lineno" > 173< / span >   }< / div >
< div class = "line" > < a name = "l00174" > < / a > < span class = "lineno" > 174< / span >   distance += sqrt(d);< / div >
< div class = "line" > < a name = "l00175" > < / a > < span class = "lineno" > 175< / span >   < span class = "comment" > // distance += d;< / span > < / div >
< div class = "line" > < a name = "l00176" > < / a > < span class = "lineno" > 176< / span >   }< / div >
< div class = "line" > < a name = "l00177" > < / a > < span class = "lineno" > 177< / span >   }< / div >
< div class = "line" > < a name = "l00178" > < / a > < span class = "lineno" > 178< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00179" > < / a > < span class = "lineno" > 179< / span >   distance /= R * R; < span class = "comment" > // mean distance from neighbors< / span > < / div >
< div class = "line" > < a name = "l00180" > < / a > < span class = "lineno" > 180< / span >   fprintf(fp, < span class = "stringliteral" > " %.4g" < / span > , distance); < span class = "comment" > // print the mean separation< / span > < / div >
< div class = "line" > < a name = "l00181" > < / a > < span class = "lineno" > 181< / span >   < span class = "keywordflow" > if< / span > (j < W-> dim2 - 1) < span class = "comment" > // if not the last column< / span > < / div >
< div class = "line" > < a name = "l00182" > < / a > < span class = "lineno" > 182< / span >   fputc(< span class = "charliteral" > ' ,' < / span > , fp); < span class = "comment" > // suffix comma< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00183" > < / a > < span class = "lineno" > 183< / span >   }< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00184" > < / a > < span class = "lineno" > 184< / span >   < span class = "keywordflow" > if< / span > (i < W-> dim1 - 1) < span class = "comment" > // if not the last row< / span > < / div >
< div class = "line" > < a name = "l00185" > < / a > < span class = "lineno" > 185< / span >   fputc(< span class = "charliteral" > ' \n' < / span > , fp); < span class = "comment" > // start a new line< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00186" > < / a > < span class = "lineno" > 186< / span >   }< / div >
< div class = "line" > < a name = "l00187" > < / a > < span class = "lineno" > 187< / span >   fclose(fp);< / div >
< div class = "line" > < a name = "l00188" > < / a > < span class = "lineno" > 188< / span >   < span class = "keywordflow" > return< / span > 0;< / div >
< div class = "line" > < a name = "l00189" > < / a > < span class = "lineno" > 189< / span >   }< / div >
< div class = "line" > < a name = "l00190" > < / a > < span class = "lineno" > 190< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00191" > < / a > < span class = "lineno" > 191< / span >   < span class = "comment" > /**< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a1440a7779ac56f47a3f355ce4a8c7da0" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a1440a7779ac56f47a3f355ce4a8c7da0" > ◆ < / 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. The following < a href = "https://en.wikipedia.org/wiki/Comma-separated_values" > CSV< / a > files are created to validate the execution:< / p > < ul >
< li > < code > test1.csv< / code > : random test samples points with a circular pattern< / li >
< li > < code > w11.csv< / code > : initial random U-matrix< / li >
< li > < code > w12.csv< / code > : trained SOM U-matrix < / li >
< / ul >
< div class = "fragment" > < div class = "line" > < a name = "l00414" > < / a > < span class = "lineno" > 414< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00415" > < / a > < span class = "lineno" > 415< / span >   < span class = "keywordflow" > if< / span > (i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ) < span class = "comment" > // only add new arrays if i < N< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00416" > < / a > < span class = "lineno" > 416< / span >   X[i] = (< span class = "keywordtype" > double< / span > *)malloc(features * < span class = "keyword" > sizeof< / span > (< span class = "keywordtype" > double< / span > ));< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00417" > < / a > < span class = "lineno" > 417< / span >   < span class = "keywordflow" > if< / span > (i < num_out) < span class = "comment" > // only add new arrays if i < num_out< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00418" > < / a > < span class = "lineno" > 418< / span >   {< / div >
< div class = "line" > < a name = "l00419" > < / a > < span class = "lineno" > 419< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > k = 0; k < num_out; k++)< / div >
< div class = "line" > < a name = "l00420" > < / a > < span class = "lineno" > 420< / span >   {< / div >
< div class = "line" > < a name = "l00421" > < / a > < span class = "lineno" > 421< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00422" > < / a > < span class = "lineno" > 422< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00423" > < / a > < span class = "lineno" > 423< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00424" > < / a > < span class = "lineno" > 424< / span >   < span class = "comment" > // preallocate with random initial weights< / span > < / div >
< div class = "line" > < a name = "l00425" > < / a > < span class = "lineno" > 425< / span >   < span class = "keywordflow" > for< / span > (j = 0; j < features; j++)< / div >
< div class = "line" > < a name = "l00426" > < / a > < span class = "lineno" > 426< / span >   {< / div >
< div class = "line" > < a name = "l00427" > < / a > < span class = "lineno" > 427< / span >   < span class = "keywordtype" > double< / span > *w = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (& W, i, k, j);< / div >
< div class = "line" > < a name = "l00428" > < / a > < span class = "lineno" > 428< / span >   w[0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (-5, 5);< / div >
< div class = "line" > < a name = "l00429" > < / a > < span class = "lineno" > 429< / span >   }< / div >
< div class = "line" > < a name = "l00430" > < / a > < span class = "lineno" > 430< / span >   }< / div >
< div class = "line" > < a name = "l00431" > < / a > < span class = "lineno" > 431< / span >   }< / div >
< div class = "line" > < a name = "l00432" > < / a > < span class = "lineno" > 432< / span >   }< / div >
< div class = "line" > < a name = "l00433" > < / a > < span class = "lineno" > 433< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00434" > < / a > < span class = "lineno" > 434< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adb5ded007be1fd666fab9affe6764018" > test_2d_classes< / a > (X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ); < span class = "comment" > // create test data around circumference of a circle< / span > < / div >
< div class = "line" > < a name = "l00435" > < / a > < span class = "lineno" > 435< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c" > save_2d_data< / a > (< span class = "stringliteral" > " test1.csv" < / span > , X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features); < span class = "comment" > // save test data points< / span > < / div >
< div class = "line" > < a name = "l00436" > < / a > < span class = "lineno" > 436< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w11.csv" < / span > , & W); < span class = "comment" > // save initial random weights< / span > < / div >
< div class = "line" > < a name = "l00437" > < / a > < span class = "lineno" > 437< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5" > kohonen_som< / a > (X, & W, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features, num_out, 1e-4); < span class = "comment" > // train the SOM< / span > < / div >
< div class = "line" > < a name = "l00438" > < / a > < span class = "lineno" > 438< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w12.csv" < / span > , & W); < span class = "comment" > // save the resultant weights< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00439" > < / a > < span class = "lineno" > 439< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00440" > < / a > < span class = "lineno" > 440< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++) free(X[i]);< / div >
< div class = "line" > < a name = "l00441" > < / a > < span class = "lineno" > 441< / span >   free(X);< / div >
< div class = "line" > < a name = "l00442" > < / a > < span class = "lineno" > 442< / span >   free(W.data);< / div >
< div class = "line" > < a name = "l00443" > < / a > < span class = "lineno" > 443< / span >   }< / div >
< div class = "line" > < a name = "l00444" > < / a > < span class = "lineno" > 444< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00445" > < / a > < span class = "lineno" > 445< / span >   < span class = "comment" > /** Creates a random set of points distributed in four clusters in< / span > < / div >
2020-06-28 23:07:29 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a0283886819c7c140a023582b7269e2d0" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a0283886819c7c140a023582b7269e2d0" > ◆ < / 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. The following < a href = "https://en.wikipedia.org/wiki/Comma-separated_values" > CSV< / a > files are created to validate the execution:< / p > < ul >
< li > < code > test2.csv< / code > : random test samples points< / 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" > < a name = "l00514" > < / a > < span class = "lineno" > 514< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00515" > < / a > < span class = "lineno" > 515< / span >   < span class = "keywordflow" > if< / span > (i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ) < span class = "comment" > // only add new arrays if i < N< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00516" > < / a > < span class = "lineno" > 516< / span >   X[i] = (< span class = "keywordtype" > double< / span > *)malloc(features * < span class = "keyword" > sizeof< / span > (< span class = "keywordtype" > double< / span > ));< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00517" > < / a > < span class = "lineno" > 517< / span >   < span class = "keywordflow" > if< / span > (i < num_out) < span class = "comment" > // only add new arrays if i < num_out< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00518" > < / a > < span class = "lineno" > 518< / span >   {< / div >
< div class = "line" > < a name = "l00519" > < / a > < span class = "lineno" > 519< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > k = 0; k < num_out; k++)< / div >
< div class = "line" > < a name = "l00520" > < / a > < span class = "lineno" > 520< / span >   {< / div >
< div class = "line" > < a name = "l00521" > < / a > < span class = "lineno" > 521< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00522" > < / a > < span class = "lineno" > 522< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00523" > < / a > < span class = "lineno" > 523< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00524" > < / a > < span class = "lineno" > 524< / span >   < span class = "keywordflow" > for< / span > (j = 0; j < features; j++)< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00525" > < / a > < span class = "lineno" > 525< / span >   { < span class = "comment" > // preallocate with random initial weights< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00526" > < / a > < span class = "lineno" > 526< / span >   < span class = "keywordtype" > double< / span > *w = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (& W, i, k, j);< / div >
< div class = "line" > < a name = "l00527" > < / a > < span class = "lineno" > 527< / span >   w[0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (-5, 5);< / div >
< div class = "line" > < a name = "l00528" > < / a > < span class = "lineno" > 528< / span >   }< / div >
< div class = "line" > < a name = "l00529" > < / a > < span class = "lineno" > 529< / span >   }< / div >
< div class = "line" > < a name = "l00530" > < / a > < span class = "lineno" > 530< / span >   }< / div >
< div class = "line" > < a name = "l00531" > < / a > < span class = "lineno" > 531< / span >   }< / div >
< div class = "line" > < a name = "l00532" > < / a > < span class = "lineno" > 532< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00533" > < / a > < span class = "lineno" > 533< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ad9e25202bb8b481461f932668f249dbc" > test_3d_classes1< / a > (X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ); < span class = "comment" > // create test data< / span > < / div >
< div class = "line" > < a name = "l00534" > < / a > < span class = "lineno" > 534< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c" > save_2d_data< / a > (< span class = "stringliteral" > " test2.csv" < / span > , X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features); < span class = "comment" > // save test data points< / span > < / div >
< div class = "line" > < a name = "l00535" > < / a > < span class = "lineno" > 535< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w21.csv" < / span > , & W); < span class = "comment" > // save initial random weights< / span > < / div >
< div class = "line" > < a name = "l00536" > < / a > < span class = "lineno" > 536< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5" > kohonen_som< / a > (X, & W, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features, num_out, 1e-4); < span class = "comment" > // train the SOM< / span > < / div >
< div class = "line" > < a name = "l00537" > < / a > < span class = "lineno" > 537< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w22.csv" < / span > , & W); < span class = "comment" > // save the resultant weights< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00538" > < / a > < span class = "lineno" > 538< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00539" > < / a > < span class = "lineno" > 539< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++) free(X[i]);< / div >
< div class = "line" > < a name = "l00540" > < / a > < span class = "lineno" > 540< / span >   free(X);< / div >
< div class = "line" > < a name = "l00541" > < / a > < span class = "lineno" > 541< / span >   free(W.data);< / div >
< div class = "line" > < a name = "l00542" > < / a > < span class = "lineno" > 542< / span >   }< / div >
< div class = "line" > < a name = "l00543" > < / a > < span class = "lineno" > 543< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00544" > < / a > < span class = "lineno" > 544< / span >   < span class = "comment" > /** Creates a random set of points distributed in four clusters in< / span > < / div >
2020-06-28 23:07:29 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a6d0455dd5c30adda100e95f0423c786e" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a6d0455dd5c30adda100e95f0423c786e" > ◆ < / 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. The following < a href = "https://en.wikipedia.org/wiki/Comma-separated_values" > CSV< / a > files are created to validate the execution:< / p > < ul >
< li > < code > test3.csv< / code > : random test samples points< / 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" > < a name = "l00615" > < / a > < span class = "lineno" > 615< / span >   {< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00616" > < / a > < span class = "lineno" > 616< / span >   < span class = "keywordflow" > if< / span > (i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ) < span class = "comment" > // only add new arrays if i < N< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00617" > < / a > < span class = "lineno" > 617< / span >   X[i] = (< span class = "keywordtype" > double< / span > *)malloc(features * < span class = "keyword" > sizeof< / span > (< span class = "keywordtype" > double< / span > ));< / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00618" > < / a > < span class = "lineno" > 618< / span >   < span class = "keywordflow" > if< / span > (i < num_out) < span class = "comment" > // only add new arrays if i < num_out< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00619" > < / a > < span class = "lineno" > 619< / span >   {< / div >
< div class = "line" > < a name = "l00620" > < / a > < span class = "lineno" > 620< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > k = 0; k < num_out; k++)< / div >
< div class = "line" > < a name = "l00621" > < / a > < span class = "lineno" > 621< / span >   {< / div >
< div class = "line" > < a name = "l00622" > < / a > < span class = "lineno" > 622< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00623" > < / a > < span class = "lineno" > 623< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00624" > < / a > < span class = "lineno" > 624< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00625" > < / a > < span class = "lineno" > 625< / span >   < span class = "comment" > // preallocate with random initial weights< / span > < / div >
< div class = "line" > < a name = "l00626" > < / a > < span class = "lineno" > 626< / span >   < span class = "keywordflow" > for< / span > (j = 0; j < features; j++)< / div >
< div class = "line" > < a name = "l00627" > < / a > < span class = "lineno" > 627< / span >   {< / div >
< div class = "line" > < a name = "l00628" > < / a > < span class = "lineno" > 628< / span >   < span class = "keywordtype" > double< / span > *w = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (& W, i, k, j);< / div >
< div class = "line" > < a name = "l00629" > < / a > < span class = "lineno" > 629< / span >   w[0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (-5, 5);< / div >
< div class = "line" > < a name = "l00630" > < / a > < span class = "lineno" > 630< / span >   }< / div >
< div class = "line" > < a name = "l00631" > < / a > < span class = "lineno" > 631< / span >   }< / div >
< div class = "line" > < a name = "l00632" > < / a > < span class = "lineno" > 632< / span >   }< / div >
< div class = "line" > < a name = "l00633" > < / a > < span class = "lineno" > 633< / span >   }< / div >
< div class = "line" > < a name = "l00634" > < / a > < span class = "lineno" > 634< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00635" > < / a > < span class = "lineno" > 635< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a5bb02a8322d717ead1b11182c5f02a3a" > test_3d_classes2< / a > (X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ); < span class = "comment" > // create test data around the lamniscate< / span > < / div >
< div class = "line" > < a name = "l00636" > < / a > < span class = "lineno" > 636< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c" > save_2d_data< / a > (< span class = "stringliteral" > " test3.csv" < / span > , X, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features); < span class = "comment" > // save test data points< / span > < / div >
< div class = "line" > < a name = "l00637" > < / a > < span class = "lineno" > 637< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w31.csv" < / span > , & W); < span class = "comment" > // save initial random weights< / span > < / div >
< div class = "line" > < a name = "l00638" > < / a > < span class = "lineno" > 638< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5" > kohonen_som< / a > (X, & W, < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > , features, num_out, 0.01); < span class = "comment" > // train the SOM< / span > < / div >
< div class = "line" > < a name = "l00639" > < / a > < span class = "lineno" > 639< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > (< span class = "stringliteral" > " w32.csv" < / span > , & W); < span class = "comment" > // save the resultant weights< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00640" > < / a > < span class = "lineno" > 640< / span >   < / div >
2020-06-28 23:30:37 +08:00
< div class = "line" > < a name = "l00641" > < / a > < span class = "lineno" > 641< / span >   < span class = "keywordflow" > for< / span > (< span class = "keywordtype" > int< / span > i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++) free(X[i]);< / div >
< div class = "line" > < a name = "l00642" > < / a > < span class = "lineno" > 642< / span >   free(X);< / div >
< div class = "line" > < a name = "l00643" > < / a > < span class = "lineno" > 643< / span >   free(W.data);< / div >
< div class = "line" > < a name = "l00644" > < / a > < span class = "lineno" > 644< / span >   }< / div >
< div class = "line" > < a name = "l00645" > < / a > < span class = "lineno" > 645< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00646" > < / a > < span class = "lineno" > 646< / span >   < span class = "comment" > /**< / span > < / div >
2020-06-28 23:07:29 +08:00
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "adb5ded007be1fd666fab9affe6764018" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#adb5ded007be1fd666fab9affe6764018" > ◆ < / 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 *  < / td >
< td class = "paramname" > < em > data< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > N< / em >   < / 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 >
2020-06-28 23:30:37 +08:00
< div class = "fragment" > < div class = "line" > < a name = "l00361" > < / a > < span class = "lineno" > 361< / span >   {< / div >
< div class = "line" > < a name = "l00362" > < / a > < span class = "lineno" > 362< / span >   < span class = "comment" > // centres of each class cluster< / span > < / div >
< div class = "line" > < a name = "l00363" > < / a > < span class = "lineno" > 363< / span >   {.5, .5}, < span class = "comment" > // centre of class 1< / span > < / div >
< div class = "line" > < a name = "l00364" > < / a > < span class = "lineno" > 364< / span >   {.5, -.5}, < span class = "comment" > // centre of class 2< / span > < / div >
< div class = "line" > < a name = "l00365" > < / a > < span class = "lineno" > 365< / span >   {-.5, .5}, < span class = "comment" > // centre of class 3< / span > < / div >
< div class = "line" > < a name = "l00366" > < / a > < span class = "lineno" > 366< / span >   {-.5, -.5} < span class = "comment" > // centre of class 4< / span > < / div >
< div class = "line" > < a name = "l00367" > < / a > < span class = "lineno" > 367< / span >   };< / div >
< div class = "line" > < a name = "l00368" > < / a > < span class = "lineno" > 368< / span >   < / div >
< div class = "line" > < a name = "l00369" > < / a > < span class = "lineno" > 369< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00370" > < / a > < span class = "lineno" > 370< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00371" > < / a > < span class = "lineno" > 371< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00372" > < / a > < span class = "lineno" > 372< / span >   < span class = "keywordflow" > for< / span > (i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++)< / div >
< div class = "line" > < a name = "l00373" > < / a > < span class = "lineno" > 373< / span >   {< / div >
< div class = "line" > < a name = "l00374" > < / a > < span class = "lineno" > 374< / span >   < span class = "keywordtype" > int< / span > < span class = "keyword" > class < / span > =< / div >
< div class = "line" > < a name = "l00375" > < / a > < span class = "lineno" > 375< / span >   rand() % num_classes; < span class = "comment" > // select a random class for the point< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00376" > < / a > < span class = "lineno" > 376< / span >   < / div >
< div class = "line" > < a name = "l00377" > < / a > < span class = "lineno" > 377< / span >   < span class = "comment" > // create random coordinates (x,y,z) around the centre of the class< / span > < / div >
< div class = "line" > < a name = "l00378" > < / a > < span class = "lineno" > 378< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][0] - R, centres[< span class = "keyword" > class< / span > ][0] + R);< / div >
< div class = "line" > < a name = "l00379" > < / a > < span class = "lineno" > 379< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][1] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][1] - R, centres[< span class = "keyword" > class< / span > ][1] + R);< / div >
< div class = "line" > < a name = "l00380" > < / a > < span class = "lineno" > 380< / span >   < / div >
< div class = "line" > < a name = "l00381" > < / a > < span class = "lineno" > 381< / span >   < span class = "comment" > /* The follosing can also be used< / span > < / div >
< div class = "line" > < a name = "l00382" > < / a > < span class = "lineno" > 382< / span >   < span class = "comment" > for (int j = 0; j < 2; j++)< / span > < / div >
< div class = "line" > < a name = "l00383" > < / a > < span class = "lineno" > 383< / span >   < span class = "comment" > data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);< / span > < / div >
< div class = "line" > < a name = "l00384" > < / a > < span class = "lineno" > 384< / span >   < span class = "comment" > */< / span > < / div >
< div class = "line" > < a name = "l00385" > < / a > < span class = "lineno" > 385< / span >   }< / div >
< div class = "line" > < a name = "l00386" > < / a > < span class = "lineno" > 386< / span >   }< / div >
< div class = "line" > < a name = "l00387" > < / a > < span class = "lineno" > 387< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00388" > < / a > < span class = "lineno" > 388< / span >   < span class = "comment" > /** Test that creates a random set of points distributed in four clusters in< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "ad9e25202bb8b481461f932668f249dbc" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#ad9e25202bb8b481461f932668f249dbc" > ◆ < / 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 *  < / td >
< td class = "paramname" > < em > data< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > N< / em >   < / 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 >
2020-06-28 23:30:37 +08:00
< div class = "fragment" > < div class = "line" > < a name = "l00460" > < / a > < span class = "lineno" > 460< / span >   {< / div >
< div class = "line" > < a name = "l00461" > < / a > < span class = "lineno" > 461< / span >   < span class = "comment" > // centres of each class cluster< / span > < / div >
< div class = "line" > < a name = "l00462" > < / a > < span class = "lineno" > 462< / span >   {.5, .5, .5}, < span class = "comment" > // centre of class 1< / span > < / div >
< div class = "line" > < a name = "l00463" > < / a > < span class = "lineno" > 463< / span >   {.5, -.5, -.5}, < span class = "comment" > // centre of class 2< / span > < / div >
< div class = "line" > < a name = "l00464" > < / a > < span class = "lineno" > 464< / span >   {-.5, .5, .5}, < span class = "comment" > // centre of class 3< / span > < / div >
< div class = "line" > < a name = "l00465" > < / a > < span class = "lineno" > 465< / span >   {-.5, -.5 - .5} < span class = "comment" > // centre of class 4< / span > < / div >
< div class = "line" > < a name = "l00466" > < / a > < span class = "lineno" > 466< / span >   };< / div >
< div class = "line" > < a name = "l00467" > < / a > < span class = "lineno" > 467< / span >   < / div >
< div class = "line" > < a name = "l00468" > < / a > < span class = "lineno" > 468< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00469" > < / a > < span class = "lineno" > 469< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00470" > < / a > < span class = "lineno" > 470< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00471" > < / a > < span class = "lineno" > 471< / span >   < span class = "keywordflow" > for< / span > (i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++)< / div >
< div class = "line" > < a name = "l00472" > < / a > < span class = "lineno" > 472< / span >   {< / div >
< div class = "line" > < a name = "l00473" > < / a > < span class = "lineno" > 473< / span >   < span class = "keywordtype" > int< / span > < span class = "keyword" > class < / span > =< / div >
< div class = "line" > < a name = "l00474" > < / a > < span class = "lineno" > 474< / span >   rand() % num_classes; < span class = "comment" > // select a random class for the point< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00475" > < / a > < span class = "lineno" > 475< / span >   < / div >
< div class = "line" > < a name = "l00476" > < / a > < span class = "lineno" > 476< / span >   < span class = "comment" > // create random coordinates (x,y,z) around the centre of the class< / span > < / div >
< div class = "line" > < a name = "l00477" > < / a > < span class = "lineno" > 477< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][0] - R, centres[< span class = "keyword" > class< / span > ][0] + R);< / div >
< div class = "line" > < a name = "l00478" > < / a > < span class = "lineno" > 478< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][1] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][1] - R, centres[< span class = "keyword" > class< / span > ][1] + R);< / div >
< div class = "line" > < a name = "l00479" > < / a > < span class = "lineno" > 479< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][2] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][2] - R, centres[< span class = "keyword" > class< / span > ][2] + R);< / div >
< div class = "line" > < a name = "l00480" > < / a > < span class = "lineno" > 480< / span >   < / div >
< div class = "line" > < a name = "l00481" > < / a > < span class = "lineno" > 481< / span >   < span class = "comment" > /* The follosing can also be used< / span > < / div >
< div class = "line" > < a name = "l00482" > < / a > < span class = "lineno" > 482< / span >   < span class = "comment" > for (int j = 0; j < 3; j++)< / span > < / div >
< div class = "line" > < a name = "l00483" > < / a > < span class = "lineno" > 483< / span >   < span class = "comment" > data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);< / span > < / div >
< div class = "line" > < a name = "l00484" > < / a > < span class = "lineno" > 484< / span >   < span class = "comment" > */< / span > < / div >
< div class = "line" > < a name = "l00485" > < / a > < span class = "lineno" > 485< / span >   }< / div >
< div class = "line" > < a name = "l00486" > < / a > < span class = "lineno" > 486< / span >   }< / div >
< div class = "line" > < a name = "l00487" > < / a > < span class = "lineno" > 487< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00488" > < / a > < span class = "lineno" > 488< / span >   < span class = "comment" > /** Test that creates a random set of points distributed in 4 clusters in< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "a5bb02a8322d717ead1b11182c5f02a3a" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#a5bb02a8322d717ead1b11182c5f02a3a" > ◆ < / 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 *  < / td >
< td class = "paramname" > < em > data< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > N< / em >   < / 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 >
2020-06-28 23:30:37 +08:00
< div class = "fragment" > < div class = "line" > < a name = "l00559" > < / a > < span class = "lineno" > 559< / span >   {< / div >
< div class = "line" > < a name = "l00560" > < / a > < span class = "lineno" > 560< / span >   < span class = "comment" > // centres of each class cluster< / span > < / div >
< div class = "line" > < a name = "l00561" > < / a > < span class = "lineno" > 561< / span >   {.5, .5, .5}, < span class = "comment" > // centre of class 1< / span > < / div >
< div class = "line" > < a name = "l00562" > < / a > < span class = "lineno" > 562< / span >   {.5, .5, -.5}, < span class = "comment" > // centre of class 2< / span > < / div >
< div class = "line" > < a name = "l00563" > < / a > < span class = "lineno" > 563< / span >   {.5, -.5, .5}, < span class = "comment" > // centre of class 3< / span > < / div >
< div class = "line" > < a name = "l00564" > < / a > < span class = "lineno" > 564< / span >   {.5, -.5, -.5}, < span class = "comment" > // centre of class 4< / span > < / div >
< div class = "line" > < a name = "l00565" > < / a > < span class = "lineno" > 565< / span >   {-.5, .5, .5}, < span class = "comment" > // centre of class 5< / span > < / div >
< div class = "line" > < a name = "l00566" > < / a > < span class = "lineno" > 566< / span >   {-.5, .5, -.5}, < span class = "comment" > // centre of class 6< / span > < / div >
< div class = "line" > < a name = "l00567" > < / a > < span class = "lineno" > 567< / span >   {-.5, -.5, .5}, < span class = "comment" > // centre of class 7< / span > < / div >
< div class = "line" > < a name = "l00568" > < / a > < span class = "lineno" > 568< / span >   {-.5, -.5, -.5} < span class = "comment" > // centre of class 8< / span > < / div >
< div class = "line" > < a name = "l00569" > < / a > < span class = "lineno" > 569< / span >   };< / div >
< div class = "line" > < a name = "l00570" > < / a > < span class = "lineno" > 570< / span >   < / div >
< div class = "line" > < a name = "l00571" > < / a > < span class = "lineno" > 571< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00572" > < / a > < span class = "lineno" > 572< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00573" > < / a > < span class = "lineno" > 573< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00574" > < / a > < span class = "lineno" > 574< / span >   < span class = "keywordflow" > for< / span > (i = 0; i < < a class = "code" href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > ; i++)< / div >
< div class = "line" > < a name = "l00575" > < / a > < span class = "lineno" > 575< / span >   {< / div >
< div class = "line" > < a name = "l00576" > < / a > < span class = "lineno" > 576< / span >   < span class = "keywordtype" > int< / span > < span class = "keyword" > class < / span > =< / div >
< div class = "line" > < a name = "l00577" > < / a > < span class = "lineno" > 577< / span >   rand() % num_classes; < span class = "comment" > // select a random class for the point< / span > < / div >
2020-06-28 23:07:29 +08:00
< div class = "line" > < a name = "l00578" > < / a > < span class = "lineno" > 578< / span >   < / div >
< div class = "line" > < a name = "l00579" > < / a > < span class = "lineno" > 579< / span >   < span class = "comment" > // create random coordinates (x,y,z) around the centre of the class< / span > < / div >
< div class = "line" > < a name = "l00580" > < / a > < span class = "lineno" > 580< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][0] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][0] - R, centres[< span class = "keyword" > class< / span > ][0] + R);< / div >
< div class = "line" > < a name = "l00581" > < / a > < span class = "lineno" > 581< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][1] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][1] - R, centres[< span class = "keyword" > class< / span > ][1] + R);< / div >
< div class = "line" > < a name = "l00582" > < / a > < span class = "lineno" > 582< / span >   < a class = "code" href = "../../df/dea/structdata.html" > data< / a > [i][2] = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > (centres[< span class = "keyword" > class< / span > ][2] - R, centres[< span class = "keyword" > class< / span > ][2] + R);< / div >
< div class = "line" > < a name = "l00583" > < / a > < span class = "lineno" > 583< / span >   < / div >
< div class = "line" > < a name = "l00584" > < / a > < span class = "lineno" > 584< / span >   < span class = "comment" > /* The follosing can also be used< / span > < / div >
< div class = "line" > < a name = "l00585" > < / a > < span class = "lineno" > 585< / span >   < span class = "comment" > for (int j = 0; j < 3; j++)< / span > < / div >
< div class = "line" > < a name = "l00586" > < / a > < span class = "lineno" > 586< / span >   < span class = "comment" > data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);< / span > < / div >
< div class = "line" > < a name = "l00587" > < / a > < span class = "lineno" > 587< / span >   < span class = "comment" > */< / span > < / div >
< div class = "line" > < a name = "l00588" > < / a > < span class = "lineno" > 588< / span >   }< / div >
< div class = "line" > < a name = "l00589" > < / a > < span class = "lineno" > 589< / span >   }< / div >
< div class = "line" > < a name = "l00590" > < / a > < span class = "lineno" > 590< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00591" > < / a > < span class = "lineno" > 591< / span >   < span class = "comment" > /** Test that creates a random set of points distributed in eight clusters in< / span > < / div >
< / div > <!-- fragment -->
< / div >
< / div >
< a id = "ad139de302fa8135a433d9c1bf971b13b" > < / a >
< h2 class = "memtitle" > < span class = "permalink" > < a href = "#ad139de302fa8135a433d9c1bf971b13b" > ◆ < / a > < / span > update_weights()< / h2 >
< div class = "memitem" >
< div class = "memproto" >
< table class = "memname" >
< tr >
< td class = "memname" > double update_weights < / td >
< td > (< / td >
< td class = "paramtype" > const double *  < / td >
< td class = "paramname" > < em > X< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > struct < a class = "el" href = "../../d0/dc8/structarray__3d.html" > array_3d< / a > *  < / td >
< td class = "paramname" > < em > W< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double **  < / td >
< td class = "paramname" > < em > D< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_out< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > num_features< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > double  < / td >
< td class = "paramname" > < em > alpha< / em > , < / td >
< / tr >
< tr >
< td class = "paramkey" > < / td >
< td > < / td >
< td class = "paramtype" > int  < / td >
< td class = "paramname" > < em > R< / em >   < / 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< \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 name = "l00231" > < / a > < span class = "lineno" > 231< / span >   {< / div >
< div class = "line" > < a name = "l00232" > < / a > < span class = "lineno" > 232< / span >   < span class = "keywordtype" > int< / span > x, y, k;< / div >
< div class = "line" > < a name = "l00233" > < / a > < span class = "lineno" > 233< / span >   < span class = "keywordtype" > double< / span > d_min = 0.f;< / div >
< div class = "line" > < a name = "l00234" > < / a > < span class = "lineno" > 234< / span >   < / div >
< div class = "line" > < a name = "l00235" > < / a > < span class = "lineno" > 235< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00236" > < / a > < span class = "lineno" > 236< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00237" > < / a > < span class = "lineno" > 237< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00238" > < / a > < span class = "lineno" > 238< / span >   < span class = "comment" > // step 1: for each 2D output point< / span > < / div >
< div class = "line" > < a name = "l00239" > < / a > < span class = "lineno" > 239< / span >   < span class = "keywordflow" > for< / span > (x = 0; x < num_out; x++)< / div >
< div class = "line" > < a name = "l00240" > < / a > < span class = "lineno" > 240< / span >   {< / div >
< div class = "line" > < a name = "l00241" > < / a > < span class = "lineno" > 241< / span >   < span class = "keywordflow" > for< / span > (y = 0; y < num_out; y++)< / div >
< div class = "line" > < a name = "l00242" > < / a > < span class = "lineno" > 242< / span >   {< / div >
< div class = "line" > < a name = "l00243" > < / a > < span class = "lineno" > 243< / span >   D[x][y] = 0.f;< / div >
< div class = "line" > < a name = "l00244" > < / a > < span class = "lineno" > 244< / span >   < span class = "comment" > // compute Euclidian distance of each output< / span > < / div >
< div class = "line" > < a name = "l00245" > < / a > < span class = "lineno" > 245< / span >   < span class = "comment" > // point from the current sample< / span > < / div >
< div class = "line" > < a name = "l00246" > < / a > < span class = "lineno" > 246< / span >   < span class = "keywordflow" > for< / span > (k = 0; k < num_features; k++)< / div >
< div class = "line" > < a name = "l00247" > < / a > < span class = "lineno" > 247< / span >   {< / div >
< div class = "line" > < a name = "l00248" > < / a > < span class = "lineno" > 248< / span >   < span class = "keywordtype" > double< / span > *w = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (W, x, y, k);< / div >
< div class = "line" > < a name = "l00249" > < / a > < span class = "lineno" > 249< / span >   D[x][y] += (w[0] - X[k]) * (w[0] - X[k]);< / div >
< div class = "line" > < a name = "l00250" > < / a > < span class = "lineno" > 250< / span >   }< / div >
< div class = "line" > < a name = "l00251" > < / a > < span class = "lineno" > 251< / span >   D[x][y] = sqrt(D[x][y]);< / div >
< div class = "line" > < a name = "l00252" > < / a > < span class = "lineno" > 252< / span >   }< / div >
< div class = "line" > < a name = "l00253" > < / a > < span class = "lineno" > 253< / span >   }< / div >
< div class = "line" > < a name = "l00254" > < / a > < span class = "lineno" > 254< / span >   < / div >
< div class = "line" > < a name = "l00255" > < / a > < span class = "lineno" > 255< / span >   < span class = "comment" > // step 2: get closest node i.e., node with smallest Euclidian distance to< / span > < / div >
< div class = "line" > < a name = "l00256" > < / a > < span class = "lineno" > 256< / span >   < span class = "comment" > // the current pattern< / span > < / div >
< div class = "line" > < a name = "l00257" > < / a > < span class = "lineno" > 257< / span >   < span class = "keywordtype" > int< / span > d_min_x, d_min_y;< / div >
< div class = "line" > < a name = "l00258" > < / a > < span class = "lineno" > 258< / span >   < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b" > get_min_2d< / a > (D, num_out, & d_min, & d_min_x, & d_min_y);< / div >
< div class = "line" > < a name = "l00259" > < / a > < span class = "lineno" > 259< / span >   < / div >
< div class = "line" > < a name = "l00260" > < / a > < span class = "lineno" > 260< / span >   < span class = "comment" > // step 3a: get the neighborhood range< / span > < / div >
< div class = "line" > < a name = "l00261" > < / a > < span class = "lineno" > 261< / span >   < span class = "keywordtype" > int< / span > from_x = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (0, d_min_x - R);< / div >
< div class = "line" > < a name = "l00262" > < / a > < span class = "lineno" > 262< / span >   < span class = "keywordtype" > int< / span > to_x = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > (num_out, d_min_x + R + 1);< / div >
< div class = "line" > < a name = "l00263" > < / a > < span class = "lineno" > 263< / span >   < span class = "keywordtype" > int< / span > from_y = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > (0, d_min_y - R);< / div >
< div class = "line" > < a name = "l00264" > < / a > < span class = "lineno" > 264< / span >   < span class = "keywordtype" > int< / span > to_y = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > (num_out, d_min_y + R + 1);< / div >
< div class = "line" > < a name = "l00265" > < / a > < span class = "lineno" > 265< / span >   < / div >
< div class = "line" > < a name = "l00266" > < / a > < span class = "lineno" > 266< / span >   < span class = "comment" > // step 3b: update the weights of nodes in the< / span > < / div >
< div class = "line" > < a name = "l00267" > < / a > < span class = "lineno" > 267< / span >   < span class = "comment" > // neighborhood< / span > < / div >
< div class = "line" > < a name = "l00268" > < / a > < span class = "lineno" > 268< / span >   < span class = "preprocessor" > #ifdef _OPENMP< / span > < / div >
< div class = "line" > < a name = "l00269" > < / a > < span class = "lineno" > 269< / span >   < span class = "preprocessor" > #pragma omp for< / span > < / div >
< div class = "line" > < a name = "l00270" > < / a > < span class = "lineno" > 270< / span >   < span class = "preprocessor" > #endif< / span > < / div >
< div class = "line" > < a name = "l00271" > < / a > < span class = "lineno" > 271< / span >   < span class = "keywordflow" > for< / span > (x = from_x; x < to_x; x++)< / div >
< div class = "line" > < a name = "l00272" > < / a > < span class = "lineno" > 272< / span >   {< / div >
< div class = "line" > < a name = "l00273" > < / a > < span class = "lineno" > 273< / span >   < span class = "keywordflow" > for< / span > (y = from_y; y < to_y; y++)< / div >
< div class = "line" > < a name = "l00274" > < / a > < span class = "lineno" > 274< / span >   {< / div >
< div class = "line" > < a name = "l00275" > < / a > < span class = "lineno" > 275< / span >   < span class = "comment" > /* you can enable the following normalization if needed.< / span > < / div >
< div class = "line" > < a name = "l00276" > < / a > < span class = "lineno" > 276< / span >   < span class = "comment" > personally, I found it detrimental to convergence */< / span > < / div >
< div class = "line" > < a name = "l00277" > < / a > < span class = "lineno" > 277< / span >   < span class = "comment" > // const double s2pi = sqrt(2.f * M_PI);< / span > < / div >
< div class = "line" > < a name = "l00278" > < / a > < span class = "lineno" > 278< / span >   < span class = "comment" > // double normalize = 1.f / (alpha * s2pi);< / span > < / div >
< div class = "line" > < a name = "l00279" > < / a > < span class = "lineno" > 279< / span >   < / div >
< div class = "line" > < a name = "l00280" > < / a > < span class = "lineno" > 280< / span >   < span class = "comment" > /* apply scaling inversely proportional to distance from the< / span > < / div >
< div class = "line" > < a name = "l00281" > < / a > < span class = "lineno" > 281< / span >   < span class = "comment" > current node */< / span > < / div >
< div class = "line" > < a name = "l00282" > < / a > < span class = "lineno" > 282< / span >   < span class = "keywordtype" > double< / span > d2 =< / div >
< div class = "line" > < a name = "l00283" > < / a > < span class = "lineno" > 283< / span >   (d_min_x - x) * (d_min_x - x) + (d_min_y - y) * (d_min_y - y);< / div >
< div class = "line" > < a name = "l00284" > < / a > < span class = "lineno" > 284< / span >   < span class = "keywordtype" > double< / span > scale_factor = exp(-d2 / (2.f * alpha * alpha));< / div >
< div class = "line" > < a name = "l00285" > < / a > < span class = "lineno" > 285< / span >   < / div >
< div class = "line" > < a name = "l00286" > < / a > < span class = "lineno" > 286< / span >   < span class = "keywordflow" > for< / span > (k = 0; k < num_features; k++)< / div >
< div class = "line" > < a name = "l00287" > < / a > < span class = "lineno" > 287< / span >   {< / div >
< div class = "line" > < a name = "l00288" > < / a > < span class = "lineno" > 288< / span >   < span class = "keywordtype" > double< / span > *w = < a class = "code" href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > (W, x, y, k);< / div >
< div class = "line" > < a name = "l00289" > < / a > < span class = "lineno" > 289< / span >   < span class = "comment" > // update weights of nodes in the neighborhood< / span > < / div >
< div class = "line" > < a name = "l00290" > < / a > < span class = "lineno" > 290< / span >   w[0] += alpha * scale_factor * (X[k] - w[0]);< / div >
< div class = "line" > < a name = "l00291" > < / a > < span class = "lineno" > 291< / span >   }< / div >
< div class = "line" > < a name = "l00292" > < / a > < span class = "lineno" > 292< / span >   }< / div >
< div class = "line" > < a name = "l00293" > < / a > < span class = "lineno" > 293< / span >   }< / div >
< div class = "line" > < a name = "l00294" > < / a > < span class = "lineno" > 294< / span >   < span class = "keywordflow" > return< / span > d_min;< / div >
< div class = "line" > < a name = "l00295" > < / a > < span class = "lineno" > 295< / span >   }< / div >
< div class = "line" > < a name = "l00296" > < / a > < span class = "lineno" > 296< / span >   < span class = "comment" > < / span > < / div >
< div class = "line" > < a name = "l00297" > < / a > < span class = "lineno" > 297< / span >   < span class = "comment" > /**< / span > < / div >
< / div > <!-- fragment --> < div class = "dynheader" >
Here is the call graph for this function:< / div >
< div class = "dyncontent" >
< div class = "center" > < iframe scrolling = "no" frameborder = "0" src = "../../d2/df6/kohonen__som__topology_8c_ad139de302fa8135a433d9c1bf971b13b_cgraph.svg" width = "262" 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 >
< / div > <!-- contents -->
< / div > <!-- doc - content -->
< div class = "ttc" id = "akohonen__som__topology_8c_html_ac6afabdc09a49a433ee19d8a9486056d" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#ac6afabdc09a49a433ee19d8a9486056d" > min< / a > < / div > < div class = "ttdeci" > #define min(a, b)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:37< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_ad139de302fa8135a433d9c1bf971b13b" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#ad139de302fa8135a433d9c1bf971b13b" > update_weights< / a > < / div > < div class = "ttdeci" > double update_weights(const double *X, struct array_3d *W, double **D, int num_out, int num_features, double alpha, int R)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:231< / div > < / div >
< div class = "ttc" id = "astructdata_html" > < div class = "ttname" > < a href = "../../df/dea/structdata.html" > data< / a > < / div > < div class = "ttdef" > < b > Definition:< / b > prime_factoriziation.c:25< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_a7237d2cf3b3f9d034477268c1d6631da" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#a7237d2cf3b3f9d034477268c1d6631da" > data_3d< / a > < / div > < div class = "ttdeci" > double * data_3d(const struct array_3d *arr, int x, int y, int z)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:64< / div > < / div >
2020-06-28 23:30:37 +08:00
< 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 = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:457< / div > < / 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 = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:556< / div > < / div >
< div class = "ttc" id = "aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4" > < div class = "ttname" > < a href = "../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4" > N< / a > < / div > < div class = "ttdeci" > #define N< / div > < div class = "ttdef" > < b > Definition:< / b > sol1.c:109< / div > < / div >
2020-06-28 23:07:29 +08:00
< div class = "ttc" id = "akohonen__som__topology_8c_html_adee1bc3871c881f6e04cb9e0b1d01158" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#adee1bc3871c881f6e04cb9e0b1d01158" > save_u_matrix< / a > < / div > < div class = "ttdeci" > int save_u_matrix(const char *fname, struct array_3d *W)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:136< / div > < / div >
< div class = "ttc" id = "astructarray__3d_html_ad823bd5424a0fc158da52a0497d8c10a" > < div class = "ttname" > < a href = "../../d0/dc8/structarray__3d.html#ad823bd5424a0fc158da52a0497d8c10a" > array_3d::dim3< / a > < / div > < div class = "ttdeci" > int dim3< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:48< / div > < / div >
< div class = "ttc" id = "astructarray__3d_html_ad5d3ee546eea3c05dc8f0dc90ec194f4" > < div class = "ttname" > < a href = "../../d0/dc8/structarray__3d.html#ad5d3ee546eea3c05dc8f0dc90ec194f4" > array_3d::dim1< / a > < / div > < div class = "ttdeci" > int dim1< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:46< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_adc22d512c00a9f5799ee067f4fb90b4b" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#adc22d512c00a9f5799ee067f4fb90b4b" > 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 = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:201< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_af5ce14f026d6d231bef29161bac2b485" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#af5ce14f026d6d231bef29161bac2b485" > _random< / a > < / div > < div class = "ttdeci" > double _random(double a, double b)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:84< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_a6824dc6d973eb3339af7aef5fea78b0c" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#a6824dc6d973eb3339af7aef5fea78b0c" > 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 = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:99< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_affe776513b24d84b39af8ab0930fef7f" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#affe776513b24d84b39af8ab0930fef7f" > max< / a > < / div > < div class = "ttdeci" > #define max(a, b)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:31< / div > < / div >
< div class = "ttc" id = "astructarray__3d_html_aa431cc0a41fd379270dbd2b0e3ac1b45" > < div class = "ttname" > < a href = "../../d0/dc8/structarray__3d.html#aa431cc0a41fd379270dbd2b0e3ac1b45" > array_3d::dim2< / a > < / div > < div class = "ttdeci" > int dim2< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:47< / div > < / div >
< div class = "ttc" id = "akohonen__som__topology_8c_html_a1983dc0f35e734015772e9f8c0ff19f5" > < div class = "ttname" > < a href = "../../d2/df6/kohonen__som__topology_8c.html#a1983dc0f35e734015772e9f8c0ff19f5" > kohonen_som< / a > < / div > < div class = "ttdeci" > void kohonen_som(double **X, struct array_3d *W, int num_samples, int num_features, int num_out, double alpha_min)< / div > < div class = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:311< / div > < / div >
2020-06-28 23:30:37 +08:00
< 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 = "ttdef" > < b > Definition:< / b > kohonen_som_topology.c:358< / div > < / div >
2020-06-28 23:07:29 +08:00
<!-- 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 >
2020-06-28 23:30:37 +08:00
< li class = "footer" > Generated by
2020-06-28 23:07:29 +08:00
< a href = "http://www.doxygen.org/index.html" >
< img class = "footer" src = "../../doxygen.png" alt = "doxygen" / > < / a > 1.8.18 < / li >
< / ul >
< / div >
< / body >
< / html >