Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MathFunctions.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLMath/src/ICLMath/MathFunctions.h                    **
00010 ** Module : ICLMath                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Random.h>
00035 
00036 #include <vector>
00037 #include <algorithm>
00038 #include <cmath>
00039 #ifdef ICL_HAVE_IPP
00040 #include <ipps.h>
00041 #endif
00042 
00043 namespace icl {
00044   namespace math{
00046 
00051     template <class ForwardIterator> 
00052     inline float euclidian(ForwardIterator v1Begin, ForwardIterator v1End,
00053                            ForwardIterator v2Begin) {
00054       float fSum = 0.0, fDiff;
00055       for (; v1Begin != v1End; ++v1Begin, ++v2Begin) {
00056         fDiff = (*v1Begin-*v2Begin);
00057         fSum += fDiff*fDiff;
00058       }
00059       return ::sqrt(fSum);
00060     }
00061     
00063 
00067     template <class T>
00068     inline float euclidian(const std::vector<T> &a, const std::vector<T> &b) {
00069       ICLASSERT_RETURN_VAL(a.size() == b.size(), float(0));
00070       return euclidian (a.begin(), a.end(), b.begin());
00071     }
00072     
00074 
00077     template <class ForwardIterator>
00078     inline double mean(ForwardIterator begin, ForwardIterator end){
00079       if(begin == end) return 0;
00080       double sum = 0;
00081       int num = 0;
00082       while(begin != end){
00083         sum += *begin++;
00084         num++;
00085       }
00086       return sum / num;
00087     }
00088 
00090 #ifdef ICL_HAVE_IPP
00091     template<> inline double mean<const icl32f*>(const icl32f *begin,const icl32f *end){
00092       icl32f m = 0;
00093       ippsMean_32f(begin,end-begin,&m,ippAlgHintAccurate);
00094       return m;
00095     }
00096     template<> inline double mean<const icl64f*>(const icl64f *begin,const icl64f *end){
00097       icl64f m = 0;
00098       ippsMean_64f(begin,end-begin,&m);
00099       return m;
00100     }
00101 #endif
00102 
00106 
00107 
00112     template <class ForwardIterator>
00113     inline double variance(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean = true){
00114       if(begin == end) return 0;
00115       register double sum = 0;
00116       register double d = 0;
00117       int num = 0;
00118       while(begin != end){
00119         d = *begin - mean;
00120         sum += d*d;
00121         ++begin;
00122         num++;
00123       }
00124       return sum/(empiricMean&&num>1 ? num - 1 : num); 
00125     }
00126   
00128 
00131     template <class ForwardIterator>
00132     inline double variance(ForwardIterator begin, ForwardIterator end){
00133       return variance(begin,end,mean(begin,end),true);
00134     }
00135     
00136     
00138 
00143     template <class ForwardIterator>
00144     inline double stdDeviation(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true){
00145       return ::sqrt(variance(begin,end,mean,empiricMean));
00146     }
00147   
00149 
00152     template <class ForwardIterator>
00153     inline double stdDeviation(ForwardIterator begin, ForwardIterator end){
00154       return ::sqrt(variance(begin,end));
00155     }
00156   
00157   
00159 
00163     template<class ForwardIterator>
00164     inline std::pair<double,double> meanAndStdDev(ForwardIterator begin,ForwardIterator end){
00165       std::pair<double,double> md;
00166       md.first = mean(begin,end);
00167       md.second = stdDeviation(begin,end,md.first,true);
00168       return md;
00169     }
00170   
00171                                  
00172   } // namespace math
00173 } //namespace icl
00174 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines