Image Component Library (ICL)
MathFunctions.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLMath/src/ICLMath/MathFunctions.h **
10 ** Module : ICLMath **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Random.h>
35 
36 #include <vector>
37 #include <algorithm>
38 #include <cmath>
39 #ifdef ICL_HAVE_IPP
40 #include <ipps.h>
41 #endif
42 
43 namespace icl {
44  namespace math{
46 
51  template <class ForwardIterator>
52  inline float euclidian(ForwardIterator v1Begin, ForwardIterator v1End,
53  ForwardIterator v2Begin) {
54  float fSum = 0.0, fDiff;
55  for (; v1Begin != v1End; ++v1Begin, ++v2Begin) {
56  fDiff = (*v1Begin-*v2Begin);
57  fSum += fDiff*fDiff;
58  }
59  return ::sqrt(fSum);
60  }
61 
63 
67  template <class T>
68  inline float euclidian(const std::vector<T> &a, const std::vector<T> &b) {
69  ICLASSERT_RETURN_VAL(a.size() == b.size(), float(0));
70  return euclidian (a.begin(), a.end(), b.begin());
71  }
72 
74 
77  template <class ForwardIterator>
78  inline double mean(ForwardIterator begin, ForwardIterator end){
79  if(begin == end) return 0;
80  double sum = 0;
81  int num = 0;
82  while(begin != end){
83  sum += *begin++;
84  num++;
85  }
86  return sum / num;
87  }
88 
90 #ifdef ICL_HAVE_IPP
91  template<> inline double mean<const icl32f*>(const icl32f *begin,const icl32f *end){
92  icl32f m = 0;
93  ippsMean_32f(begin,end-begin,&m,ippAlgHintAccurate);
94  return m;
95  }
96  template<> inline double mean<const icl64f*>(const icl64f *begin,const icl64f *end){
97  icl64f m = 0;
98  ippsMean_64f(begin,end-begin,&m);
99  return m;
100  }
101 #endif
102 
106 
112  template <class ForwardIterator>
113  inline double variance(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean = true){
114  if(begin == end) return 0;
115  register double sum = 0;
116  register double d = 0;
117  int num = 0;
118  while(begin != end){
119  d = *begin - mean;
120  sum += d*d;
121  ++begin;
122  num++;
123  }
124  return sum/(empiricMean&&num>1 ? num - 1 : num);
125  }
126 
128 
131  template <class ForwardIterator>
132  inline double variance(ForwardIterator begin, ForwardIterator end){
133  return variance(begin,end,mean(begin,end),true);
134  }
135 
136 
138 
143  template <class ForwardIterator>
144  inline double stdDeviation(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true){
145  return ::sqrt(variance(begin,end,mean,empiricMean));
146  }
147 
149 
152  template <class ForwardIterator>
153  inline double stdDeviation(ForwardIterator begin, ForwardIterator end){
154  return ::sqrt(variance(begin,end));
155  }
156 
157 
159 
163  template<class ForwardIterator>
164  inline std::pair<double,double> meanAndStdDev(ForwardIterator begin,ForwardIterator end){
165  std::pair<double,double> md;
166  md.first = mean(begin,end);
167  md.second = stdDeviation(begin,end,md.first,true);
168  return md;
169  }
170 
171 
172  } // namespace math
173 } //namespace icl
174 
ICLQt_API ImgQ sqrt(const ImgQ &image)
calls sqrt( each pixel)
undocument this line if you encounter any issues!
Definition: Any.h:37
std::pair< double, double > meanAndStdDev(ForwardIterator begin, ForwardIterator end)
Calculates mean and standard deviation of given data range simultanously.
Definition: MathFunctions.h:164
double mean(ForwardIterator begin, ForwardIterator end)
computes the mean value of a data range
Definition: MathFunctions.h:78
double variance(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true)
Compute the variance of a given data range with given mean value.
Definition: MathFunctions.h:113
Ipp32f icl32f
32Bit floating point type for the ICL
Definition: BasicTypes.h:55
#define ICLASSERT_RETURN_VAL(X, VALUE)
Definition: Macros.h:148
Ipp64f icl64f
64Bit floating point type for the ICL
Definition: BasicTypes.h:52
double stdDeviation(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true)
Compute std-deviation of a data set with given mean (calls sqrt(variance(..))
Definition: MathFunctions.h:144
float euclidian(ForwardIterator v1Begin, ForwardIterator v1End, ForwardIterator v2Begin)
Calculate the euclidian distance of two vectors v1 and v2.
Definition: MathFunctions.h:52