cmtkValueSequence.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2152 $
00026 //
00027 //  $LastChangedDate: 2010-08-04 10:19:33 -0700 (Wed, 04 Aug 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkValueSequence_h_included_
00034 #define __cmtkValueSequence_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <math.h>
00039 
00040 namespace
00041 cmtk
00042 {
00043 
00046 
00058 template<class T=float> 
00059 class ValueSequence 
00060 {
00061 public:
00063   typedef ValueSequence<T> Self;
00064 
00066   ValueSequence() 
00067   { 
00068     this->Reset(); 
00069   }
00070 
00072   void Reset() 
00073   {
00074     NValues = 0; Sum = 0; SumAbs = 0; SumOfSquares = 0; 
00075     Minimum = Maximum = MinimumAbs = MaximumAbs = 0; 
00076   }
00077   
00079   void Proceed( const T v ) 
00080   {
00081     if ( ! NValues ) 
00082       {
00083       Minimum = Maximum = v;
00084       MinimumAbs = MaximumAbs = fabs( v );
00085       } 
00086     else
00087       {
00088       if ( v < Minimum ) Minimum = v;
00089       if ( v > Maximum ) Maximum = v;
00090       if ( fabs( v ) < MinimumAbs ) MinimumAbs = fabs( v );
00091       if ( fabs( v ) > MaximumAbs ) MaximumAbs = fabs( v );
00092       }
00093     ++NValues; Sum += v; SumAbs += fabs( v ); SumOfSquares += v*v; 
00094   }
00095 
00097   double GetMinimum() const { return Minimum; }
00098 
00100   double GetMaximum() const { return Maximum; }
00101 
00103   double GetMinimumAbs() const { return MinimumAbs; }
00104 
00106   double GetMaximumAbs() const { return MaximumAbs; }
00107 
00109   int GetNValues() const { return NValues; }
00110 
00112   double GetVariance( const bool unbiased = true ) const
00113   { 
00114     const double mu = this->GetAverage();
00115     return ( NValues * mu * mu - 2 * mu * Sum + SumOfSquares ) / ( unbiased ? (NValues-1) : NValues );
00116   }
00117 
00119   double GetSum() const { return static_cast<double>( Sum ); }
00120 
00122   double GetSumOfSquares() const { return static_cast<double>( SumOfSquares ); }
00123 
00125   double GetAverage() const { return static_cast<double>( Sum / NValues ); }
00126 
00128   double GetAverageAbs() const { return static_cast<double>( SumAbs / NValues ); }
00129 
00131   ValueSequence<T>& operator=( const ValueSequence<T>& other );
00132 
00133 private:
00135   T Sum;
00136 
00138   T SumAbs;
00139 
00141   T SumOfSquares;
00142 
00144   int NValues;
00145 
00147   T Minimum;
00148 
00150   T Maximum;
00151 
00153   T MinimumAbs;
00154 
00156   T MaximumAbs;
00157 
00159   template<class TT>
00160   friend ValueSequence<TT> operator+( const ValueSequence<TT>& a, const ValueSequence<TT>& b );
00161 };
00162 
00164 
00165 } // namespace cmtk
00166 
00167 #include "cmtkValueSequence.txx"
00168 
00169 #endif // #ifndef __cmtkValueSequence_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines