00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_ACCU_RMS_HH
00027 # define MLN_ACCU_RMS_HH
00028 
00032 
00033 # include <mln/core/concept/meta_accumulator.hh>
00034 # include <mln/accu/internal/base.hh>
00035 # include <mln/math/sqrt.hh>
00036 
00037 
00038 namespace mln
00039 {
00040 
00041   namespace accu
00042   {
00043 
00044 
00050     
00051     template <typename T, typename V>
00052     struct rms : public mln::accu::internal::base<V, rms<T,V> >
00053     {
00054       typedef T argument;
00055 
00056       rms();
00057 
00060       void init();
00061       void take_as_init_(const T& p);
00062       void take(const T& p);
00063       void take(const rms<T,V>& other);
00065 
00067       V to_result() const;
00068 
00071       V hook_value_() const;
00072 
00075       bool is_valid() const;
00076 
00077     protected:
00078 
00079       V v_;
00080       unsigned count_;
00081     };
00082 
00083 
00084     namespace meta
00085     {
00086 
00088       struct rms : public Meta_Accumulator< rms >
00089       {
00090         template <typename T, typename V>
00091         struct with
00092         {
00093           typedef accu::rms<T,V> ret;
00094         };
00095       };
00096 
00097     } 
00098 
00099 
00100 # ifndef MLN_INCLUDE_ONLY
00101 
00102     template <typename T, typename V>
00103     inline
00104     rms<T,V>::rms()
00105     {
00106       init();
00107     }
00108 
00109     template <typename T, typename V>
00110     inline
00111     void
00112     rms<T,V>::init()
00113     {
00114       v_ = literal::zero;
00115       count_ = 0;
00116     }
00117 
00118     template <typename T, typename V>
00119     inline
00120     void
00121     rms<T,V>::take_as_init_(const T& t)
00122     {
00123       v_ += t * t;
00124       ++count_;
00125     }
00126 
00127     template <typename T, typename V>
00128     inline
00129     void
00130     rms<T,V>::take(const T& t)
00131     {
00132       v_ += t * t;
00133       ++count_;
00134     }
00135 
00136     template <typename T, typename V>
00137     inline
00138     void
00139     rms<T,V>::take(const rms<T,V>& other)
00140     {
00141       v_ += other.v_;
00142       count_ += other.count_;
00143     }
00144 
00145     template <typename T, typename V>
00146     inline
00147     V
00148     rms<T,V>::to_result() const
00149     {
00150       if (count_ == 0)
00151         return V(0);
00152       return math::sqrt<V>(v_ / count_);
00153     }
00154 
00155     template <typename T, typename V>
00156     inline
00157     V
00158     rms<T,V>::hook_value_() const
00159     {
00160       return v_;
00161     }
00162 
00163     template <typename T, typename V>
00164     inline
00165     bool
00166     rms<T,V>::is_valid() const
00167     {
00168       return true;
00169     }
00170 
00171 # endif // ! MLN_INCLUDE_ONLY
00172 
00173   } 
00174 
00175 } 
00176 
00177 
00178 #endif // ! MLN_ACCU_RMS_HH