aboutsummaryrefslogtreecommitdiffstats
path: root/doc/html/math_2utility_8h_source.html
blob: 147f1518f6fcbc9de4fd5e9fd0c85844eecedb81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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.3.1"/>
<title>Templates -- Meow: meowpp/math/utility.h Source File</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="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<link href="custom.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="projectlogo"><img alt="Logo" src="logo.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">Templates -- Meow
   &#160;<span id="projectnumber">1.1.2</span>
   </div>
   <div id="projectbrief">不能,也不應該先編譯成obj-file的templates</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
</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">
$(document).ready(function(){initNavTree('math_2utility_8h_source.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">utility.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="math_2utility_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef   math_utility_H__</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor"></span><span class="preprocessor">#define   math_utility_H__</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor"></span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;cstdlib&gt;</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &lt;algorithm&gt;</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &lt;cmath&gt;</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;</div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="keyword">namespace </span>meow{</div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div>
<div class="line"><a name="l00012"></a><span class="lineno"><a class="code" href="namespacemeow.html#aa86d5920087a4e73078b759bc3996f76">   12</a></span>&#160;<span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="namespacemeow.html#aa86d5920087a4e73078b759bc3996f76" title="圓周率...">PI</a> = 3.14159265358979323846264338327950288;</div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00018"></a><span class="lineno"><a class="code" href="namespacemeow.html#ab9456533c0ae85fba812bfb5b017c8d2">   18</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#ab9456533c0ae85fba812bfb5b017c8d2" title="如果abs(輸入的數值) &lt; eps, 則回傳0, 否則回傳輸入的數值">noEPS</a>(T value, T eps = 1e-9){</div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;  T epsp((eps &lt; T(0)) ? -eps : eps);</div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;  <span class="keywordflow">return</span> ((value &lt; -epsp || value &gt; epsp) ? value : T(0));</div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;}</div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00027"></a><span class="lineno"><a class="code" href="namespacemeow.html#aebe1ad335556790c578c4cf7836fd9ca">   27</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#aebe1ad335556790c578c4cf7836fd9ca" title="(value-lower)/(upper-lower)">normalize</a>(T lower, T upper, T value){</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;  <span class="keywordflow">return</span> (value - lower) / (upper - lower);</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;}</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00035"></a><span class="lineno"><a class="code" href="namespacemeow.html#af95cfca15ad7a51ed4d07b06e1296080">   35</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#af95cfca15ad7a51ed4d07b06e1296080" title="(lower+_ratio*(upper-lower))">denormalize</a>(T lower, T upper, T _ratio){</div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;  <span class="keywordflow">return</span> lower + _ratio * (upper - lower);</div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;}</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00043"></a><span class="lineno"><a class="code" href="namespacemeow.html#a3b01a91eb4a02909a61a35e406fa5732">   43</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#a3b01a91eb4a02909a61a35e406fa5732" title="denormalize(l2,u2,normalize(l1,u1,m1))">ratioMapping</a>(T l1, T u1, T m1, T l2, T u2){</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;  <span class="keywordflow">return</span> <a class="code" href="namespacemeow.html#af95cfca15ad7a51ed4d07b06e1296080" title="(lower+_ratio*(upper-lower))">denormalize</a>(l2, u2, <a class="code" href="namespacemeow.html#aebe1ad335556790c578c4cf7836fd9ca" title="(value-lower)/(upper-lower)">normalize</a>(l1, u1, m1));</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;}</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00051"></a><span class="lineno"><a class="code" href="namespacemeow.html#a98d838d338a3e6f3c57e94562fab3fa7">   51</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#a98d838d338a3e6f3c57e94562fab3fa7" title="std::min(mx,std::max(mn,v))">inRange</a>(T <span class="keyword">const</span>&amp; mn, T <span class="keyword">const</span>&amp; mx, T <span class="keyword">const</span>&amp; v){</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  <span class="keywordflow">return</span> std::min(mx, std::max(mn, v));</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;}</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00059"></a><span class="lineno"><a class="code" href="namespacemeow.html#a1954e235973a2ac79a7a72a4b7d25573">   59</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#a1954e235973a2ac79a7a72a4b7d25573" title="x*x ">squ</a>(T <span class="keyword">const</span>&amp; x){</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;  <span class="keywordflow">return</span> x * x;</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;}</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00067"></a><span class="lineno"><a class="code" href="namespacemeow.html#a10a8cca705773b7d9f6f91d3fb08c104">   67</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#a10a8cca705773b7d9f6f91d3fb08c104" title="x*x*x ">cub</a>(T <span class="keyword">const</span>&amp; x){</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;  <span class="keywordflow">return</span> x * x * x;</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;}</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00075"></a><span class="lineno"><a class="code" href="namespacemeow.html#ad4d1e5fa951e5534204a0ee3eee22777">   75</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemeow.html#ad4d1e5fa951e5534204a0ee3eee22777" title="只將 sigs 個標準差以內的數據拿來取平均">average</a>(T <span class="keyword">const</span>&amp; beg, T <span class="keyword">const</span>&amp; end, <span class="keywordtype">double</span> sigs){</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;  <span class="keywordtype">int</span> N = 0;</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;  <span class="keywordtype">double</span> av = 0;</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;  <span class="keywordflow">for</span>(T it = beg; it != end; it++, N++){</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    av += *it;</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;  }</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;  av /= N;</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;  <span class="keywordtype">double</span> sig = 0;</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;  <span class="keywordflow">for</span>(T it = beg; it != end; it++){</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;    sig += (*it - av) * (*it - av);</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;  }</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;  sig = sqrt(sig / N);</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;  <span class="keywordtype">double</span> lower = av - sig * sigs, upper = av + sig * sigs;</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;  <span class="keywordtype">double</span> ret = 0, retn = 0;</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;  <span class="keywordflow">for</span>(T it = beg; it != end; it++){</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    <span class="keywordflow">if</span>(lower &lt;= *it &amp;&amp; *it &lt;= upper){</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;      ret += *it;</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;      retn++;</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;    }</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;  }</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;  <span class="keywordflow">return</span> ret / retn;</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;}</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00102"></a><span class="lineno"><a class="code" href="namespacemeow.html#a6ae78e60d507c85de003f8fa8423b650">  102</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemeow.html#ad4d1e5fa951e5534204a0ee3eee22777" title="只將 sigs 個標準差以內的數據拿來取平均">average</a>(T <span class="keyword">const</span>&amp; beg, T <span class="keyword">const</span>&amp; end, T <span class="keyword">const</span>&amp; p, <span class="keywordtype">double</span> sigs){</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;  <span class="keywordtype">int</span> N = 0;</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;  <span class="keywordtype">double</span> ps = 0;</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;  <span class="keywordflow">for</span>(T it = beg, ip = p; it != end; it++, N++, ip++){</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;    ps += *ip;</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;  }</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;  <span class="keywordtype">double</span> av = 0;</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;  <span class="keywordflow">for</span>(T it = beg, ip = p; it != end; it++, ip++){</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    av += *it * *ip / ps;</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  }</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;  <span class="keywordtype">double</span> sig = 0;</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;  <span class="keywordflow">for</span>(T it = beg, ip = p; it != end; it++, ip++){</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;    sig += *ip / ps * (*it - av) * (*it - av);</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;  }</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;  sig = sqrt(sig);</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;  <span class="keywordtype">double</span> lower = av - sig * sigs, upper = av + sig * sigs;</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;  <span class="keywordtype">double</span> ret = 0, retn = 0;</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;  <span class="keywordflow">for</span>(T it = beg, ip = p; it != end; it++, ip++){</div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;    <span class="keywordflow">if</span>(lower &lt;= *it &amp;&amp; *it &lt;= upper){</div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;      ret  += *it * *ip;</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;      retn += *ip;</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;    }</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;  }</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;  <span class="keywordflow">if</span>(retn &lt;= 1e-10) <span class="keywordflow">return</span> av;</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;  <span class="keywordflow">return</span> ret / retn;</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;}</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line"><a name="l00133"></a><span class="lineno"><a class="code" href="namespacemeow.html#ae880337dfdcc67984a0dccdb50e5c5d0">  133</a></span>&#160;<span class="keyword">inline</span> T <a class="code" href="namespacemeow.html#ae880337dfdcc67984a0dccdb50e5c5d0" title="就只是個取絕對值">tAbs</a>(T <span class="keyword">const</span>&amp; t){</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;  <span class="keywordflow">return</span> (t &lt; 0 ? -t : t);</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;}</div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;}</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;</div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;<span class="preprocessor">#endif // math_utility_H__</span></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.3.1-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_92fecd8d02dd5e7a67429447fdf0f60f.html">meowpp</a></li><li class="navelem"><a class="el" href="dir_77e0ab67466b1e7d76f50725a5ab9910.html">math</a></li><li class="navelem"><a class="el" href="math_2utility_8h.html">utility.h</a></li>
    <li class="footer">Generated on Sun Jun 1 2014 13:54:33 for Templates -- Meow by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>