![]() |
Reference Documentation |
00001 #ifndef _WEIGHTED_BIN_H_ 00002 #define _WEIGHTED_BIN_H_ 1 00003 00004 00005 #include <math.h> 00006 #include "HTL/I_Bin.h" 00007 #include "HTL/HTL_std.h" 00008 00009 #undef T_Value 00010 #define T_Value double 00011 00024 class Weighted_Bin: public I_Bin { 00025 public: 00027 H_IID_IMPLEMENT( Weighted_Bin ); 00028 public: 00029 typedef Weighted_Bin Like_Current; 00030 typedef I_Bin Like_Parent; 00031 00032 public: 00034 Weighted_Bin(): I_Bin(), value_(0), error_(0), count_(0) {} 00036 ~Weighted_Bin() {} 00037 00038 public: // Inherited from I_Bin: 00040 double value( Index = 0 ) const { return value_; } 00041 00043 double error( Index = 0 ) const { return sqrt(error_); } 00044 00046 Size count() const { return count_; } 00047 00049 void set_value( double other, Index = 0 ) { value_ = T_Value(other); } 00050 00052 void set_error( double other, Index = 0 ) { error_ = T_Value(other*other);} 00053 00055 void set_count( Size other ) { count_ = Size(other); } 00056 00058 void reset() { value_ = T_Value(0); error_ = T_Value(0); count_ = 0; } 00059 00060 public: 00062 void put( T_Value a_weight ) { 00063 value_ += a_weight; 00064 error_ += a_weight * a_weight; 00065 count_ ++; 00066 } 00067 00068 public: 00070 int operator==( const Like_Current &other ) const { 00071 return( 00072 (T_Value( value() ) == T_Value( other.value() )) && 00073 (T_Value( error() ) == T_Value( other.error() )) && 00074 (count() == other.count()) 00075 ); 00076 } 00077 00079 void make( const Like_Parent &other ) { 00080 value_ = T_Value( other.value() ); 00081 error_ = T_Value( other.error() * other.error() ); 00082 count_ = other.count(); 00083 } 00085 void add( const Like_Parent &other ) { 00086 value_ = T_Value( value_ + other.value() ); 00087 error_ = T_Value( other.error() * other.error() + error_ ); 00088 count_ += other.count(); 00089 } 00090 00092 void sub( const Like_Parent &other ) { 00093 value_ = T_Value( value_ - other.value() ); 00094 error_ = T_Value( other.error() * other.error() + error_ ); 00095 count_ += other.count(); 00096 } 00097 00099 void mul( const Like_Parent &other ) { 00100 value_ = T_Value( other.value() * value_ ); 00101 error_ = T_Value( error_ * other.value()*other.value() + 00102 other.error()*other.error() * value_*value_ ) ; 00103 count_ += other.count(); 00104 } 00105 00110 void div( const Like_Parent &other ) { 00111 T_Value e = T_Value( other.error() ); 00112 e = T_Value( e*e * e*e * e*e * e*e ); 00113 if( T_Value(other.value()) == T_Value(0.) ) { 00114 HTL_ERR( "WARNING: Division by 0 in Weighted_Bin" ); 00115 value_ = T_Value(0.); 00116 } else { 00117 value_ = T_Value( value_ / other.value() ); 00118 } 00119 error_ = T_Value( error_ * other.value()*other.value() + 00120 other.error()*other.error() * value_*value_ ) ; 00121 if( e == T_Value(0.) ) { 00122 HTL_ERR( "WARNING: Division by 0 in Weighted_Bin" ); 00123 error_ = T_Value(0.); 00124 } else { 00125 error_ = T_Value( error_/e ); 00126 } 00127 count_ += other.count(); 00128 } 00129 00134 void binomial_div( const Like_Parent &other ) { 00135 if( T_Value(other.value()) == T_Value(0.) ) { 00136 HTL_ERR( "WARNING: Division by 0 in Weighted_Bin" ); 00137 value_ = T_Value(0.); 00138 } else { 00139 value_ = T_Value( value_ / other.value() ); 00140 } 00141 T_Value e = T_Value( other.error() ); 00142 if( e == T_Value(0.) ) { 00143 HTL_ERR( "WARNING: Division by 0 in Weighted_Bin" ); 00144 error_ = T_Value(0.); 00145 } else { 00146 e = e * e; 00147 error_ = sqrt( error_ / e ); 00148 error_ = error_ * (1-error_); 00149 error_ = error_ * error_; 00150 error_ = T_Value( error_ / e ); 00151 } 00152 count_ += other.count(); 00153 } 00154 00156 void add( double x ) { value_ = T_Value( value_ + x ); } 00158 void sub( double x ) { value_ = T_Value( value_ - x ); } 00160 void mul( double x ) { value_ = T_Value( value_ * x ); } 00162 void div( double x ) { 00163 if( x != 0. ) { 00164 value_ = T_Value( value_ / x ); 00165 return; 00166 } 00167 HTL_ERR( "WARNING: Division by 0 in Weighted_Bin: div(x)." ); 00168 value_ = T_Value(0.); 00169 } 00170 00171 private: 00172 T_Value value_; // Real error. 00173 T_Value error_; // Square of the real error. 00174 Size count_; 00175 }; 00176 00177 #undef T_Value 00178 00179 #endif // _WEIGHTED_BIN_H_ 00180
Anaphe documentation generated by Doxygen (www.doxygen.org) |