aimsdata 6.0.0
Neuroimaging data handling
mathelem.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34/*
35 * Elementary math functions.
36 */
37#ifndef AIMS_MATH_MATHELEM_H
38#define AIMS_MATH_MATHELEM_H
39
41#include <cartobase/type/converter.h> // carto::min_limit<T>()
42#include <cstdlib>
43#include <cmath>
44#include <algorithm>
45#include <vector>
46#include <map>
47#include <limits>
48
49//============================================================================
50// BASIC OPERATIONS
51//============================================================================
52namespace aims
53{
54
58
59 template <typename T> inline T sqr( const T & x)
60 {
61 return x * x;
62 }
63
65 template <typename T> inline T cub( const T & x )
66 {
67 return x * x * x;
68 }
69
71 template <typename T> inline float sgn( const T & x )
72 {
73 return x < 0 ? -1.0f : +1.0f;
74 }
75
79 template <typename T>
80 inline T absdiff( const T& x, const T& y )
81 {
82 return std::abs(static_cast<double>(y) - static_cast<double>(x));
83 }
84
86}
87
88//============================================================================
89// OPERATIONS ON ITERATED VALUES
90//============================================================================
91namespace aims {
92
93 // helper function in private namespace
94 namespace internal {
95 template <typename T>
96 bool is_min( T a, T b )
97 {
98 return a < b;
99 }
100 }
101
116 template <typename T>
118 {
119 public:
120 template<typename Iterator>
121 static T max( Iterator b, Iterator e, T default_value = carto::min_limit<T>() )
122 {
123 Iterator m;
124 m = std::max_element( b, e, internal::is_min<T> );
125 if( m == e )
126 return default_value;
127 else
128 return T(*m);
129 }
130
131 template <typename Iterator>
132 static T min( Iterator b, Iterator e, T default_value = std::numeric_limits<T>::max() )
133 {
134 Iterator m;
135 m = std::min_element( b, e, internal::is_min<T> );
136 if( m == e )
137 return default_value;
138 else
139 return T(*m);
140 }
141
142 template <typename Iterator>
143 static T median( Iterator b, Iterator e, T default_value = (T)0 )
144 {
145 Iterator i;
146 std::vector<T> values;
147 for( i=b; i!=e; ++i )
148 values.push_back( (T) *i );
149
150 if( values.size() == 0 )
151 return default_value;
152 std::sort( values.begin(), values.end() );
153 return values[ values.size() / 2 ];
154 }
155
156 template<typename Iterator>
157 static T notnull_median( Iterator b, Iterator e, T default_value = (T)0 )
158 {
159 Iterator i;
160 std::vector<T> values;
161 for( i=b; i!=e; ++i )
162 if( *i != 0 )
163 values.push_back( *i );
164
165 if( values.size() == 0 )
166 return default_value;
167 std::sort( values.begin(), values.end() );
168 return values[ values.size() / 2 ];
169 }
170
171 template <typename Iterator>
172 static T mean( Iterator b, Iterator e )
173 {
174 Iterator i = b;
175 double sum = 0.0;
176 uint32_t count = 0;
177
178 // Sum values and count values
179 for( ; i != e; ++i) {
180 sum += (double)(*i);
181 ++count;
182 }
183 return (count != 0 ? (T)( sum / count ) : (T)0);
184 }
185
186 template <typename Iterator>
187 static T notnull_mean( Iterator b, Iterator e )
188 {
189 Iterator i;
190 double sum = 0.0;
191 uint32_t count = 0;
192
193 // Sum values and count values
194 for( i=b; i!=e; ++i )
195 if( *i != 0 ) {
196 sum += (double)(*i);
197 ++count;
198 }
199 return (count != 0 ? (T)( sum / count ) : (T)0);
200 }
201
202 template <typename Iterator>
203 static T majority( Iterator b, Iterator e, T default_value = (T)0 )
204 {
205 Iterator i;
206 T currentclass, majorityclass = default_value;
207 uint32_t currentclasscases = 0, majoritycases = 0;
208 std::map<T, uint32_t> classcases;
209
210 // Goes through the data and count number of values for each class
211 for( i=b; i!=e; ++i )
212 {
213 currentclass = (*i);
214
215 if ( !classcases[ currentclass ] )
216 {
217 classcases[ currentclass ] = 1;
218 currentclasscases = 1;
219 }
220 else
221 {
222 currentclasscases = classcases[ currentclass ] + 1;
223 classcases[ currentclass ] = currentclasscases;
224 }
225
226 if (currentclasscases > majoritycases)
227 {
228 // Set the new majority cases and class for which it occurs
229 majorityclass = currentclass;
230 majoritycases = currentclasscases;
231 }
232 }
233
234 return majorityclass;
235 }
236
237 template <typename Iterator>
238 static T notnull_majority( Iterator b, Iterator e, T default_value = (T)0 )
239 {
240 Iterator i;
241 T currentclass, majorityclass = default_value;
242 uint32_t currentclasscases = 0, majoritycases = 0;
243 std::map<T, uint32_t> classcases;
244
245 // Goes through the data and count number of values for each class
246 for( i=b; i!=e; ++i )
247 {
248 if (*i!=0)
249 {
250 currentclass = (*i);
251
252 if ( !classcases[ currentclass ] )
253 {
254 classcases[ currentclass ] = 1;
255 currentclasscases = 1;
256 }
257 else
258 {
259 currentclasscases = classcases[ currentclass ] + 1;
260 classcases[ currentclass ] = currentclasscases;
261 }
262
263 if (currentclasscases > majoritycases)
264 {
265 // Set the new majority cases and class for which it occurs
266 majorityclass = currentclass;
267 majoritycases = currentclasscases;
268 }
269 }
270 }
271
272 return majorityclass;
273 }
274
275 template <typename Iterator>
276 static T extrema_difference( Iterator b, Iterator e )
277 {
278 T mi, mx;
279 mi = min( b, e );
280 mx = max( b, e );
281 return absdiff<T>( mx, mi );
282 }
283
284 template <typename Iterator>
285 static T sum( Iterator b, Iterator e )
286 {
287 Iterator i;
288 double accumulate = 0.0;
289
290 // Goes through the data and sum values
291 for( i=b; i!=e; ++i )
292 accumulate += (double)(*i);
293 return (T)accumulate;
294 }
295
296 template <typename Iterator>
297 static T variance( Iterator b, Iterator e )
298 {
299 Iterator i;
300 double accumulate = 0.0;
301 double accumulateSq = 0.0;
302 uint32_t count = 0;
303
304 for( i=b; i!=e; ++i ) {
305 accumulate += (double)(*i);
306 accumulateSq += ( (double)(*i) * (double)(*i) );
307 ++count;
308 }
309
310 double dcount = (double) count;
311 return (T) ( accumulateSq / dcount -
312 ( accumulate / dcount ) *
313 ( accumulate / dcount ) );
314 }
315
316 template <typename Iterator>
317 static T stdev( Iterator b, Iterator e )
318 {
319 Iterator i;
320 double accumulate = 0.0;
321 double accumulateSq = 0.0;
322 uint32_t count = 0;
323
324 for( i=b; i!=e; ++i ) {
325 accumulate += (double)(*i);
326 accumulateSq += ( (double)(*i) * (double)(*i) );
327 ++count;
328 }
329
330 double dcount = (double) count;
331 return (T) std::sqrt( accumulateSq / dcount -
332 ( accumulate / dcount ) *
333 ( accumulate / dcount ) );
334 }
335
336 }; // MathUtil
337
338} // namespace aims
339
340//============================================================================
341// TRIGONOMETRY
342//============================================================================
345
346template <class T>
347double AimsArgument(T re,T im);
349
350template <class T> inline
351double AimsArgument(T re,T im)
352{
353 double re2=(double)re;
354 double im2=(double)im;
355
356 if ((!re2) && (!im2)) return(0);
357 if (!re2)
358 {
359 if (im2>=0) return M_PI/2;
360 else return -M_PI/2;
361 }
362 if (re2>0) return std::atan(im2/re2);
363 else if (im2>=0) return std::atan(im2/re2)+M_PI;
364 else return std::atan(im2/re2)-M_PI;
365}
366
367
368//============================================================================
369// BASIC OPERATIONS
370//============================================================================
371template <class T> inline
372T square(const T& val)
373{
374 return val*val;
375}
376
377
378template <class T> inline
379T cube(const T& val)
380{
381 return val*val*val;
382}
383
384
385template< class T > inline
386T pythagore( const T& a, const T& b )
387{
388 T c, d;
389
390 c = (T)std::fabs( a );
391 d = (T)std::fabs( b );
392
393 if ( c > d )
394 return c * (T)std::sqrt( (T)1 + square( d / c ) );
395 else
396 return ( d == (T)0 ) ? (T)0 : d * (T)std::sqrt( (T)1 + square( c / d ) );
397}
398
399
402
405AIMSDATA_API double AimsSigmoid(double x);
407
408#endif
#define AIMSDATA_API
Defines basic math functions that run on iterators.
Definition mathelem.h:118
static T notnull_mean(Iterator b, Iterator e)
Definition mathelem.h:187
static T median(Iterator b, Iterator e, T default_value=(T) 0)
Definition mathelem.h:143
static T mean(Iterator b, Iterator e)
Definition mathelem.h:172
static T variance(Iterator b, Iterator e)
Definition mathelem.h:297
static T extrema_difference(Iterator b, Iterator e)
Definition mathelem.h:276
static T min(Iterator b, Iterator e, T default_value=std::numeric_limits< T >::max())
Definition mathelem.h:132
static T majority(Iterator b, Iterator e, T default_value=(T) 0)
Definition mathelem.h:203
static T notnull_median(Iterator b, Iterator e, T default_value=(T) 0)
Definition mathelem.h:157
static T stdev(Iterator b, Iterator e)
Definition mathelem.h:317
static T max(Iterator b, Iterator e, T default_value=carto::min_limit< T >())
Definition mathelem.h:121
static T sum(Iterator b, Iterator e)
Definition mathelem.h:285
static T notnull_majority(Iterator b, Iterator e, T default_value=(T) 0)
Definition mathelem.h:238
T pythagore(const T &a, const T &b)
Definition mathelem.h:386
AIMSDATA_API double AimsSigmoid(double x)
Sigmoidal function (useful for neural networks)
double AimsArgument(T re, T im)
Get the argument of a complex re + i.im.
Definition mathelem.h:351
T square(const T &val)
Definition mathelem.h:372
AIMSDATA_API int AimsNextPowerOfTwo(int number)
Return the next power of two closest to an integer.
T cube(const T &val)
Definition mathelem.h:379
bool is_min(T a, T b)
Definition mathelem.h:96
The class for EcatSino data write operation.
T absdiff(const T &x, const T &y)
Get the absolute difference between 2 values without having to take care of used type this is particu...
Definition mathelem.h:80
float sgn(const T &x)
Get the sign of x.
Definition mathelem.h:71
T sqr(const T &x)
Get the square of x.
Definition mathelem.h:59
T cub(const T &x)
Get the cube of x.
Definition mathelem.h:65
OUTP accumulate(const Volume< T > &vol, BinaryFunction func, OUTP initial)
T min_limit()
static _Tp max()