proj home

Files   Classes   Functions   Hierarchy  

histogram.h

Go to the documentation of this file.
00001 #ifndef HISTOGRAM_H
00002 #define HISTOGRAM_H
00003 
00004 #include <cassert>
00005 #include <iostream>
00006 #include <vector>
00007 using namespace std;
00008 
00009 #include <typedefs.h>
00010 
00022 template< typename T >
00023 class histogram
00024 {
00025   T xlow;
00026   T xhigh;
00027   uint N;
00028   T dw;
00029 public:
00030 
00032   vector<T> counter;
00033 
00035   void reset();
00036 
00038   histogram
00039   (
00040     T const xlow_,
00041     T const xhigh_,
00042     uintc N_
00043   );
00044 
00046   void eval(T const x);
00047 
00050   void frequencydist
00051   (
00052     vector<T> & vd,
00053     boolc exclude_low=true,  // Ignore lowest interval
00054     boolc exclude_high=true  // Ignore highest interval
00055   ) const;
00056 
00059   void frequencydistpair
00060   (
00061     vector<T> & vd,
00062     boolc exclude_low=true,  // Ignore lowest interval
00063     boolc exclude_high=true  // Ignore highest interval
00064   ) const;
00065 
00068   ostream & printfrequencydistpair
00069   (
00070     ostream & os,
00071     boolc exclude_low=true,  // Ignore lowest interval
00072     boolc exclude_high=true  // Ignore highest interval
00073   ) const;
00074 
00076   ostream & print(ostream & os) const;
00077 };
00078 
00079 template< typename T >
00080 ostream & operator << (ostream & os, histogram<T> const & hg)
00081 {
00082   return hg.print(os);
00083 }
00084 
00085 
00086 //----------------------------------------------------------
00087 // Implementation
00088 
00089 
00090 template< typename T >
00091 inline void histogram<T>::eval(T const x)
00092 {
00093   assert(counter.size()==N+1);
00094   assert(N>0);
00095   if ((xlow<x)&&(x<xhigh))
00096   {
00097     uint k = (uint)((x-xlow)*dw+1);
00098     ++counter[k];
00099     return;
00100   }
00101 
00102   if ((xlow<x)==false)
00103   {
00104     ++counter[0];
00105     return;
00106   }
00107 
00108   ++counter[N];
00109 }
00110 
00111 template< typename T >
00112 ostream & histogram<T>::printfrequencydistpair
00113 (
00114   ostream & os,
00115   boolc exclude_low,  // Ignore lowest interval
00116   boolc exclude_high  // Ignore highest interval
00117 ) const
00118 {
00119   vector<T> fd;
00120   frequencydistpair(fd,exclude_low,exclude_high);
00121   uint imax=fd.size()/2;
00122   for (uint i=0; i<imax; ++i)
00123   {
00124     os << fd[2*i] << " ";
00125     os << fd[2*i+1] << endl;
00126   }
00127 
00128   return os;
00129 }
00130 
00131 template< typename T >
00132 void histogram<T>::frequencydistpair
00133 (
00134   vector<T> & vd,
00135   boolc exclude_low,
00136   boolc exclude_high
00137 ) const
00138 {
00139   if (counter.empty()||(N<3))
00140     return;
00141 
00142   vector<T> v;
00143   frequencydist(v,exclude_low,exclude_high);
00144 
00145   uintc imax = v.size();
00146   vd.resize(imax*2);
00147   for (uint i=0; i<imax; ++i)
00148   {
00149     vd[i*2] = xlow+(xhigh-xlow)*(.5+i)/(N-1);
00150     vd[i*2+1] = v[i];
00151   }
00152 
00153 }
00154 
00155 template< typename T >
00156 void histogram<T>::frequencydist
00157 (
00158   vector<T> & vd,
00159   boolc exclude_low,
00160   boolc exclude_high
00161 ) const
00162 {
00163   if (counter.empty()||(N<3))
00164     return;
00165 
00166   uint i=0;
00167   uint imax=counter.size();
00168   if (exclude_low)
00169     ++i;
00170 
00171   if (exclude_high)
00172     --imax;
00173 
00174   T sum = 0;
00175   for (uint k=i;k<imax;++k)
00176     sum += counter[k];
00177 
00178   vd.resize(imax-i);
00179 
00180   if (sum==0)
00181     return;
00182 
00183   T suminv = T(1.0) / sum;
00184   for (uint k=i;k<imax;++k)
00185     vd[k-i] = counter[k]*suminv;
00186 }
00187 
00188 template< typename T >
00189 ostream & histogram<T>::print(ostream & os) const
00190 {
00191   uint imax = counter.size();
00192   for (uint i=0; i<imax; ++i)
00193     os << counter[i] << " ";
00194 
00195   return os;
00196 }
00197 
00198 template< typename T >
00199 histogram<T>::histogram
00200 (
00201   T const xlow_,
00202   T const xhigh_,
00203   uintc N_
00204 )
00205   : xlow(xlow_), xhigh(xhigh_), N(N_)
00206 {
00207   assert(xlow<xhigh);
00208   assert(N>0);
00209 
00210   dw = (N-1.0)/(xhigh-xlow);
00211   reset();
00212 }
00213 
00214 template< typename T >
00215 void histogram<T>::reset()
00216 {
00217   counter.resize(N+1);
00218   assert(counter.size()==N+1);
00219   uintc imax=counter.size();
00220 
00221   for (uint i=0; i<imax; ++i)
00222     counter[i]=0;
00223 }
00224 
00225 #endif
00226 
00227 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8