00001 #ifndef mbl_sum_1d_h_ 00002 #define mbl_sum_1d_h_ 00003 00004 //: 00005 // \file 00006 // \brief Computes running sum of 1D variable, so mean accessible. 00007 // \author Tim Cootes 00008 00009 #include <vcl_iosfwd.h> 00010 #include <vsl/vsl_binary_io.h> 00011 00012 //: Computes running sum of 1D variable, so mean accessible. 00013 // \code 00014 // // A rather trivial example 00015 // mbl_sum_1d stats,stats2; 00016 // 00017 // const int n = 10; 00018 // for (int i=0;i<n;i++) 00019 // { 00020 // stats.obs(i); 00021 // stats.obs(i+10); 00022 // } 00023 // 00024 // vcl_cout<<stats; 00025 // vcl_cout<<"Mean of numbers 0..9:"<<stats.mean()<<vcl_endl; 00026 // mbl_sum_1d stats3 = stats+stats2; 00027 // vcl_cout<<"Mean of numbers 0..19:"<<stats3.mean()<<vcl_endl; 00028 // \endcode 00029 class mbl_sum_1d 00030 { 00031 double sum_; 00032 int n_obs_; 00033 public: 00034 mbl_sum_1d() ; 00035 00036 //: Remove all data 00037 void clear(); 00038 00039 //: Add given observation 00040 inline void obs(double v) { sum_+=v; n_obs_++; } 00041 00042 //: Number of observations 00043 int nObs() const { return n_obs_; } 00044 00045 //: Mean of current observations 00046 double mean() const { return n_obs_>0 ? sum_/n_obs_ : 0; } 00047 00048 //: Sum of current observations 00049 double sum() const { return sum_; } 00050 00051 //: Add statistics together 00052 mbl_sum_1d& operator+=(const mbl_sum_1d& s1); 00053 00054 00055 void print_summary(vcl_ostream& os) const; 00056 00057 //: Version number for I/O 00058 short version_no() const; 00059 void b_write(vsl_b_ostream& bfs) const; 00060 void b_read(vsl_b_istream& bfs); 00061 00062 //: Test for equality 00063 bool operator==(const mbl_sum_1d& s) const; 00064 00065 friend 00066 mbl_sum_1d operator+(const mbl_sum_1d& s1, const mbl_sum_1d& s2); 00067 }; 00068 00069 //: Binary file stream output operator for class reference 00070 void vsl_b_write(vsl_b_ostream& bfs, const mbl_sum_1d& b); 00071 00072 //: Binary file stream input operator for class reference 00073 void vsl_b_read(vsl_b_istream& bfs, mbl_sum_1d& b); 00074 00075 //: Stream output operator for class reference 00076 vcl_ostream& operator<<(vcl_ostream& os,const mbl_sum_1d& stats); 00077 00078 //: Stream output operator for class reference 00079 void vsl_print_summary(vcl_ostream& os,const mbl_sum_1d& stats); 00080 00081 #endif