aimsdata  4.7.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 //============================================================================
52 namespace aims
53 {
54 
58  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 //============================================================================
91 namespace 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>
117  class MathUtil
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 extrema_difference( Iterator b, Iterator e )
239  {
240  T mi, mx;
241  mi = min( b, e );
242  mx = max( b, e );
243  return absdiff<T>( mx, mi );
244  }
245 
246  template <typename Iterator>
247  static T sum( Iterator b, Iterator e )
248  {
249  Iterator i;
250  double accumulate = 0.0;
251 
252  // Goes through the data and sum values
253  for( i=b; i!=e; ++i )
254  accumulate += (double)(*i);
255  return (T)accumulate;
256  }
257 
258  template <typename Iterator>
259  static T variance( Iterator b, Iterator e )
260  {
261  Iterator i;
262  double accumulate = 0.0;
263  double accumulateSq = 0.0;
264  uint32_t count = 0;
265 
266  for( i=b; i!=e; ++i ) {
267  accumulate += (double)(*i);
268  accumulateSq += ( (double)(*i) * (double)(*i) );
269  ++count;
270  }
271 
272  double dcount = (double) count;
273  return (T) ( accumulateSq / dcount -
274  ( accumulate / dcount ) *
275  ( accumulate / dcount ) );
276  }
277 
278  template <typename Iterator>
279  static T stdev( Iterator b, Iterator e )
280  {
281  Iterator i;
282  double accumulate = 0.0;
283  double accumulateSq = 0.0;
284  uint32_t count = 0;
285 
286  for( i=b; i!=e; ++i ) {
287  accumulate += (double)(*i);
288  accumulateSq += ( (double)(*i) * (double)(*i) );
289  ++count;
290  }
291 
292  double dcount = (double) count;
293  return (T) std::sqrt( accumulateSq / dcount -
294  ( accumulate / dcount ) *
295  ( accumulate / dcount ) );
296  }
297 
298  }; // MathUtil
299 
300 } // namespace aims
301 
302 //============================================================================
303 // TRIGONOMETRY
304 //============================================================================
307 template <class T>
309 double AimsArgument(T re,T im);
311 
312 template <class T> inline
313 double AimsArgument(T re,T im)
314 {
315  double re2=(double)re;
316  double im2=(double)im;
317 
318  if ((!re2) && (!im2)) return(0);
319  if (!re2)
320  {
321  if (im2>=0) return M_PI/2;
322  else return -M_PI/2;
323  }
324  if (re2>0) return std::atan(im2/re2);
325  else if (im2>=0) return std::atan(im2/re2)+M_PI;
326  else return std::atan(im2/re2)-M_PI;
327 }
328 
329 
330 //============================================================================
331 // BASIC OPERATIONS
332 //============================================================================
333 template <class T> inline
334 T square(const T& val)
335 {
336  return val*val;
337 }
338 
339 
340 template <class T> inline
341 T cube(const T& val)
342 {
343  return val*val*val;
344 }
345 
346 
347 template< class T > inline
348 T pythagore( const T& a, const T& b )
349 {
350  T c, d;
351 
352  c = (T)std::fabs( a );
353  d = (T)std::fabs( b );
354 
355  if ( c > d )
356  return c * (T)std::sqrt( (T)1 + square( d / c ) );
357  else
358  return ( d == (T)0 ) ? (T)0 : d * (T)std::sqrt( (T)1 + square( c / d ) );
359 }
360 
361 
364 AIMSDATA_API int AimsNextPowerOfTwo(int number);
367 AIMSDATA_API double AimsSigmoid(double x);
369 
370 #endif
double AimsArgument(T re, T im)
Get the argument of a complex re + i.im.
Definition: mathelem.h:313
AIMSDATA_API double AimsSigmoid(double x)
Sigmoidal function (useful for neural networks)
T cub(const T &x)
Get the cube of x.
Definition: mathelem.h:65
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
T pythagore(const T &a, const T &b)
Definition: mathelem.h:348
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
static T min(Iterator b, Iterator e, T default_value=std::numeric_limits< T >::max())
Definition: mathelem.h:132
#define AIMSDATA_API
T max(const Volume< T > &vol)
static T extrema_difference(Iterator b, Iterator e)
Definition: mathelem.h:238
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
The class for EcatSino data write operation.
Definition: border.h:42
static T mean(Iterator b, Iterator e)
Definition: mathelem.h:172
Defines basic math functions that run on iterators.
Definition: mathelem.h:117
T min(const Volume< T > &vol)
T cube(const T &val)
Definition: mathelem.h:341
AIMSDATA_API int AimsNextPowerOfTwo(int number)
Return the next power of two closest to an integer.
static T sum(Iterator b, Iterator e)
Definition: mathelem.h:247
static T stdev(Iterator b, Iterator e)
Definition: mathelem.h:279
static T notnull_mean(Iterator b, Iterator e)
Definition: mathelem.h:187
bool is_min(T a, T b)
Definition: mathelem.h:96
T sqr(const T &x)
Get the square of x.
Definition: mathelem.h:59
static T max(Iterator b, Iterator e, T default_value=carto::min_limit< T >())
Definition: mathelem.h:121
float sgn(const T &x)
Get the sign of x.
Definition: mathelem.h:71
static T variance(Iterator b, Iterator e)
Definition: mathelem.h:259
static T median(Iterator b, Iterator e, T default_value=(T) 0)
Definition: mathelem.h:143
T square(const T &val)
Definition: mathelem.h:334
OUTP accumulate(const Volume< T > &vol, BinaryFunction func, OUTP initial)