proj home

Files   Classes   Functions   Hierarchy  

histogram< T > Class Template Reference

Count instances in 1D. Equally spaced intervals except the end intervals (-infinty,xlow) and (xhigh,infinity). More...

#include <histogram.h>

Inheritance diagram for histogram< T >:
Collaboration diagram for histogram< T >:

List of all members.

Public Member Functions

void reset ()
 Sets counts to zero.
 histogram (T const xlow_, T const xhigh_, uintc N_)
 Constructor defines the intervals.
void eval (T const x)
 Count the instance.
void frequencydist (vector< T > &vd, boolc exclude_low=true, boolc exclude_high=true) const
 Construct a frequency distribution from data.
void frequencydistpair (vector< T > &vd, boolc exclude_low=true, boolc exclude_high=true) const
 Interlace the middle of the range and the frequency distribution.
ostreamprintfrequencydistpair (ostream &os, boolc exclude_low=true, boolc exclude_high=true) const
 Same as frequencydistpair but prints a return after each pair.
ostreamprint (ostream &os) const
 Prints the counter including lowest and highest intervals.

Public Attributes

vector< Tcounter
 Keeps interval counts.


Detailed Description

template<typename T>
class histogram< T >

Count instances in 1D. Equally spaced intervals except the end intervals (-infinty,xlow) and (xhigh,infinity).

xlow is the lowest partition point. xhigh is the highest partition point. N is the number of inner divisions. ie N-1 partitions between xlow and xhigh. The other two partitions are for x>xhigh and x<xlow. n+1 partitions in total.

Definition at line 23 of file histogram.h.


Constructor & Destructor Documentation

template<typename T>
histogram< T >::histogram ( T const   xlow_,
T const   xhigh_,
uintc  N_ 
) [inline]

Constructor defines the intervals.

Definition at line 200 of file histogram.h.

References reset().

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 }


Member Function Documentation

template<typename T>
void histogram< T >::eval ( T const   x  )  [inline]

Count the instance.

Definition at line 91 of file histogram.h.

References histogram< T >::counter.

Referenced by histogramtest::test01().

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 }

template<typename T>
void histogram< T >::frequencydist ( vector< T > &  vd,
boolc  exclude_low = true,
boolc  exclude_high = true 
) const [inline]

Construct a frequency distribution from data.

ie c[i]/sum(c[i])

Definition at line 157 of file histogram.h.

Referenced by histogramtest::test01().

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 }

template<typename T>
void histogram< T >::frequencydistpair ( vector< T > &  vd,
boolc  exclude_low = true,
boolc  exclude_high = true 
) const [inline]

Interlace the middle of the range and the frequency distribution.

Useful for plots.

Definition at line 133 of file histogram.h.

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 }

template<typename T >
ostream & histogram< T >::print ( ostream os  )  const [inline]

Prints the counter including lowest and highest intervals.

Definition at line 189 of file histogram.h.

References histogram< T >::counter.

00190 {
00191   uint imax = counter.size();
00192   for (uint i=0; i<imax; ++i)
00193     os << counter[i] << " ";
00194 
00195   return os;
00196 }

template<typename T >
ostream & histogram< T >::printfrequencydistpair ( ostream os,
boolc  exclude_low = true,
boolc  exclude_high = true 
) const [inline]

Same as frequencydistpair but prints a return after each pair.

Definition at line 113 of file histogram.h.

Referenced by histogramtest::test01().

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 }

template<typename T >
void histogram< T >::reset (  )  [inline]

Sets counts to zero.

Definition at line 215 of file histogram.h.

References histogram< T >::counter.

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 }


Member Data Documentation

template<typename T>
vector<T> histogram< T >::counter

Keeps interval counts.

Definition at line 32 of file histogram.h.

Referenced by histogram< T >::eval(), histogram< T >::print(), and histogram< T >::reset().


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:50:03 2011 for Chelton Evans Source by  doxygen 1.5.8