Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

statistics.H

Go to the documentation of this file.
00001 #ifndef STATISTICS_H_IS_INCLUDED
00002 #define STATISTICS_H_IS_INCLUDED
00003 
00004 /*!
00005  *  \file statistics.H
00006  *  \brief Statistical functions.
00007  *  \ingroup group_MLIB
00008  *
00009  */
00010 
00011 namespace mlib {
00012 
00013 //! \brief Calculates standard statistical information like average, standard
00014 //! deviation, min and max and optionally print it or return them.
00015 //!
00016 //! The template parameter \p T should be some sort of numerical type for this
00017 //! function to work.
00018 //!
00019 //! \param[in]  list    ARRAY of values over which statistics will be computed.
00020 //! \param[in]  print   Whether or not to print results to stderr.
00021 //! \param[out] average Average of values in \p list.
00022 //! \param[out] std_d   Standard deviation of values in \p list.
00023 //! \param[out] _max    Maximum of values in \p list.
00024 //! \param[out] _min    Minimum of values in \p list.
00025 //!
00026 //! \question Do we want this function to print its results to stderr or to stdout?
00027 //!
00028 //! \remark The sun compiler rejects default argument values in templated
00029 //! functions.
00030 template <class T>
00031 void
00032 statistics(
00033    CARRAY<T> &list,
00034    bool print,
00035    double *average,
00036    double* std_d,
00037    T* _max,
00038    T* _min)
00039 {
00040    double sum=0; 
00041    double sqr_sum=0; 
00042    T max=0;
00043    T min=DBL_MAX;
00044 
00045    for(int i=0;i<list.num();i++) 
00046    { 
00047       sum+=list[i]; 
00048       sqr_sum+=(list[i]*list[i]);
00049       if(list[i]>max) max=list[i];
00050       if(list[i]<min) min=list[i];
00051    }
00052    double avg = sum/list.num();
00053    double std = sqrt(sqr_sum/list.num() - avg*avg);
00054 
00055    if(print)
00056       cerr << "Statistics ------ # of samples = " << list.num()<<
00057       ", Average = " << avg << ", Std D= " << std<< ", Max= " << max<< ", Min= " <<min<<endl;
00058 
00059    if(average) *average=avg;
00060    if(std_d) *std_d=std;
00061    if(_max) *_max = max;
00062    if(_min) *_min = min;
00063 }
00064 
00065 } // namespace mlib
00066 
00067 #endif // STATISTICS_H_IS_INCLUDED
00068 
00069 // End of file statistics.H

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4