Math.hpp
Go to the documentation of this file.
00001 /*      
00002  * File: Math.h
00003  * Project: DUtils library
00004  * Author: Dorian Galvez-Lopez
00005  * Date: April 2010
00006  * Description: some math functions
00007  *
00008  * 
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  */
00023 
00024 #pragma once
00025 #ifndef __D_MATH__
00026 #define __D_MATH__
00027 
00028 #include <vector>
00029 #include <algorithm>
00030 #include <numeric>
00031 #include <cmath>
00032 #include <opencv/cv.h>
00033 
00034 namespace DUtils {
00035 
00036         class Math {
00037 
00038         public:
00039 
00040                 /* Returns the mean of a population
00041                  * @param v
00042                  */
00043                 template <class T>
00044                 static double Mean(const std::vector<T> &v)
00045                 {
00046                         if(v.empty())
00047                                 return 0;
00048                         else{
00049                                 double sum = 0;
00050                                 typename std::vector<T>::const_iterator it;
00051                                 for(it = v.begin(); it != v.end(); it++){
00052                                         sum += *it;
00053                                 }
00054                                 return sum/double(v.size());
00055                         }
00056                 }
00057 
00058                 /* Returns the standard deviation of a population
00059                  * @param v
00060                  * @param mean (optional): the mean of the population
00061                  */
00062                 template <class T>
00063                 static double Stdev(const std::vector<T> &v)
00064                 {
00065                         return Math::Stdev<T>(v, Math::Mean<T>(v));
00066                 }
00067 
00068                 template <class T>
00069                 static double Stdev(const std::vector<T> &v, double mean)
00070                 {
00071                         if(v.size() <= 1)
00072                                 return 0;
00073                         else{
00074                                 // stdev = sqrt( Sum{ (x_i - mean)^2 } / (N-1) )
00075                                 double sum = 0;
00076                                 typename std::vector<T>::const_iterator it;
00077                                 for(it = v.begin(); it != v.end(); it++){
00078                                         sum += pow(*it - mean, 2);
00079                                 }
00080                                 return sqrt(sum/double(v.size()-1));
00081                         }
00082                 }
00083 
00084         };
00085 
00086 }
00087 
00088 #endif
00089 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:52