00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #pragma once
00026 #ifndef __D_MATH__
00027 #define __D_MATH__
00028
00029 #include <vector>
00030 #include <algorithm>
00031 #include <numeric>
00032 #include <cmath>
00033 #include <limits>
00034
00035 namespace DUtils {
00036
00038 class Math {
00039
00040 public:
00041
00046 template <class T>
00047 static double Mean(const std::vector<T> &v)
00048 {
00049 if(v.empty())
00050 return 0;
00051 else{
00052 double sum = 0;
00053 typename std::vector<T>::const_iterator it;
00054 for(it = v.begin(); it != v.end(); it++){
00055 sum += *it;
00056 }
00057 return sum/double(v.size());
00058 }
00059 }
00060
00061
00062
00067 template <class T>
00068 static double Stdev(const std::vector<T> &v)
00069 {
00070 return Math::Stdev<T>(v, Math::Mean<T>(v));
00071 }
00072
00073
00074
00080 template <class T>
00081 static double Stdev(const std::vector<T> &v, double mean)
00082 {
00083 if(v.size() <= 1)
00084 return 0;
00085 else{
00086
00087 double sum = 0;
00088 typename std::vector<T>::const_iterator it;
00089 for(it = v.begin(); it != v.end(); it++){
00090 sum += pow(*it - mean, 2);
00091 }
00092 return sqrt(sum/double(v.size()-1));
00093 }
00094 }
00095
00096
00097
00102 template <class T>
00103 static double Median(const std::vector<T> &v)
00104 {
00105 if(v.empty())
00106 return 0;
00107 else{
00108 std::vector<T> w = v;
00109 sort(w.begin(), w.end());
00110 int i = w.size() / 2;
00111 if(w.size() % 2 == 1)
00112 {
00113 return (double)w[i];
00114 }
00115 else
00116 {
00117 return ((double)w[i-1] + (double)w[i]) / 2.;
00118 }
00119 }
00120 }
00121
00122
00123
00129 template <class T>
00130 static T Min(const std::vector<T> &v)
00131 {
00132 return Math::MinMax(v, true,
00133 bool2type<std::numeric_limits<T>::is_specialized>());
00134 }
00135
00141 template <class T>
00142 static T Max(const std::vector<T> &v)
00143 {
00144 return Math::MinMax(v, false,
00145 bool2type<std::numeric_limits<T>::is_specialized>());
00146 }
00147
00148
00149
00155 template<class T>
00156 inline static T signedAngle(T angle)
00157 {
00158 return ( angle <= 180 ? angle : angle - 360 );
00159 }
00160
00166 template<class T>
00167 inline static T absoluteAngle(T angle)
00168 {
00169 return ( angle >= 0 ? angle : angle + 360 );
00170 }
00171
00172
00173
00174 protected:
00175
00176 template<bool> struct bool2type { };
00177
00183 template<class T>
00184 static T MinMax(const std::vector<T> &v, bool minormax, bool2type<true>);
00185
00191 template<class T>
00192 static T MinMax(const std::vector<T> &v, bool minormax, bool2type<false>);
00193
00194 };
00195
00196
00197
00198 template<class T>
00199 T Math::MinMax(const std::vector<T> &v, bool minormax, bool2type<true>)
00200 {
00201 if(v.empty())
00202 return std::numeric_limits<T>::quiet_NaN( );
00203 else if(minormax)
00204 return *std::min_element(v.begin(), v.end());
00205 else
00206 return *std::max_element(v.begin(), v.end());
00207 }
00208
00209
00210
00211 template<class T>
00212 T Math::MinMax(const std::vector<T> &v, bool minormax, bool2type<false>)
00213 {
00214 if(v.empty())
00215 return T();
00216 else if(minormax)
00217 return *std::min_element(v.begin(), v.end());
00218 else
00219 return *std::max_element(v.begin(), v.end());
00220 }
00221
00222
00223
00224 }
00225
00226 #endif
00227