Image Component Library (ICL)
|
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.GPL ** 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 <vector> 00034 #include <algorithm> 00035 #include <cmath> 00036 #ifdef HAVE_IPP 00037 #include <ipps.h> 00038 #endif 00039 00040 #include <ICLUtils/Random.h> 00041 00042 namespace icl { 00043 namespace math{ 00045 00050 template <class ForwardIterator> 00051 inline float euclidian(ForwardIterator v1Begin, ForwardIterator v1End, 00052 ForwardIterator v2Begin) { 00053 float fSum = 0.0, fDiff; 00054 for (; v1Begin != v1End; ++v1Begin, ++v2Begin) { 00055 fDiff = (*v1Begin-*v2Begin); 00056 fSum += fDiff*fDiff; 00057 } 00058 return ::sqrt(fSum); 00059 } 00060 00062 00066 template <class T> 00067 inline float euclidian(const std::vector<T> &a, const std::vector<T> &b) { 00068 ICLASSERT_RETURN_VAL(a.size() == b.size(), float(0)); 00069 return euclidian (a.begin(), a.end(), b.begin()); 00070 } 00071 00073 00076 template <class ForwardIterator> 00077 inline double mean(ForwardIterator begin, ForwardIterator end){ 00078 if(begin == end) return 0; 00079 double sum = 0; 00080 int num = 0; 00081 while(begin != end){ 00082 sum += *begin++; 00083 num++; 00084 } 00085 return sum / num; 00086 } 00087 00089 #ifdef HAVE_IPP 00090 template<> inline double mean<const icl32f*>(const icl32f *begin,const icl32f *end){ 00091 icl32f m = 0; 00092 ippsMean_32f(begin,end-begin,&m,ippAlgHintAccurate); 00093 return m; 00094 } 00095 template<> inline double mean<const icl64f*>(const icl64f *begin,const icl64f *end){ 00096 icl64f m = 0; 00097 ippsMean_64f(begin,end-begin,&m); 00098 return m; 00099 } 00100 #endif 00101 00105 00106 00111 template <class ForwardIterator> 00112 inline double variance(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true){ 00113 if(begin == end) return 0; 00114 register double sum = 0; 00115 register double d = 0; 00116 int num = 0; 00117 while(begin != end){ 00118 d = *begin - mean; 00119 sum += d*d; 00120 ++begin; 00121 num++; 00122 } 00123 return sum/(empiricMean&&num>1 ? num - 1 : num); 00124 } 00125 00127 00130 template <class ForwardIterator> 00131 inline double variance(ForwardIterator begin, ForwardIterator end){ 00132 return variance(begin,end,mean(begin,end),true); 00133 } 00134 00135 00137 00142 template <class ForwardIterator> 00143 inline double stdDeviation(ForwardIterator begin, ForwardIterator end, double mean, bool empiricMean=true){ 00144 return ::sqrt(variance(begin,end,mean,empiricMean)); 00145 } 00146 00148 00151 template <class ForwardIterator> 00152 inline double stdDeviation(ForwardIterator begin, ForwardIterator end){ 00153 return ::sqrt(variance(begin,end)); 00154 } 00155 00156 00158 00162 template<class ForwardIterator> 00163 inline std::pair<double,double> meanAndStdDev(ForwardIterator begin,ForwardIterator end){ 00164 std::pair<double,double> md; 00165 md.first = mean(begin,end); 00166 md.second = stdDeviation(begin,end,md.first,true); 00167 return md; 00168 } 00169 00170 00171 } // namespace math 00172 } //namespace icl 00173