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_STAT_DEVIATION_HH
00027 # define MLN_ACCU_STAT_DEVIATION_HH
00028
00032
00033 # include <mln/accu/internal/base.hh>
00034 # include <mln/accu/math/count.hh>
00035 # include <mln/accu/math/sum.hh>
00036 # include <mln/math/sqr.hh>
00037 # include <mln/math/sqrt.hh>
00038
00039
00040 namespace mln
00041 {
00042
00043 namespace accu
00044 {
00045
00046 namespace stat
00047 {
00048
00050
00059 template <typename T,
00060 typename S = mln_sum(T),
00061 typename M = S>
00062 struct deviation : public mln::accu::internal::base< M , deviation<T,S,M> >
00063 {
00064 typedef T argument;
00065 typedef M result;
00066
00067 deviation(const T mean);
00068
00071 void init();
00072 void take(const argument& t);
00073 void take(const deviation<T,S,M>& other);
00075
00077 M to_result() const;
00078 operator M () const;
00079
00082 bool is_valid() const;
00083
00084 protected:
00085
00086 accu::math::count<T> count_;
00087
00088
00089
00090
00091 accu::math::sum<mln_sum(T),S> sum_;
00092 T mean_;
00093 };
00094
00095
00096
00097 template <typename I, typename S, typename M>
00098 struct deviation< util::pix<I>, S,M >;
00099
00100
00101 namespace meta
00102 {
00103
00105 struct deviation : public Meta_Accumulator< deviation >
00106 {
00107 template < typename T,
00108 typename S = mln_sum(T),
00109 typename M = S >
00110 struct with
00111 {
00112 typedef accu::stat::deviation<T,S,M> ret;
00113 };
00114 };
00115
00116 }
00117
00118
00119 # ifndef MLN_INCLUDE_ONLY
00120
00121 template <typename T, typename S, typename M>
00122 inline
00123 deviation<T,S,M>::deviation(const T mean)
00124 {
00125 init();
00126 mean_ = mean;
00127 }
00128
00129 template <typename T, typename S, typename M>
00130 inline
00131 void
00132 deviation<T,S,M>::init()
00133 {
00134 count_.init();
00135 sum_.init();
00136 }
00137
00138 template <typename T, typename S, typename M>
00139 inline
00140 void deviation<T,S,M>::take(const argument& t)
00141 {
00142 count_.take(t);
00143 sum_.take(mln::math::sqr(t - mean_));
00144 }
00145
00146 template <typename T, typename S, typename M>
00147 inline
00148 void
00149 deviation<T,S,M>::take(const deviation<T,S,M>& other)
00150 {
00151
00152 count_.take(other.count_);
00153 sum_.take(other.sum_);
00154 }
00155
00156 template <typename T, typename S, typename M>
00157 inline
00158 M
00159 deviation<T,S,M>::to_result() const
00160 {
00161 unsigned n = count_.to_result();
00162 if (n == 0u)
00163 return M();
00164 return static_cast<M>(mln::math::sqrt(sum_.to_result() / n));
00165 }
00166
00167 template <typename T, typename S, typename M>
00168 inline
00169 deviation<T,S,M>::operator M() const
00170 {
00171 return to_result();
00172 }
00173
00174 template <typename T, typename S, typename M>
00175 inline
00176 bool
00177 deviation<T,S,M>::is_valid() const
00178 {
00179 return count_.to_result() != 0;
00180 }
00181
00182 # endif // ! MLN_INCLUDE_ONLY
00183
00184 }
00185
00186 }
00187
00188 }
00189
00190
00191 #endif // ! MLN_ACCU_STAT_DEVIATION_HH