Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009, 2010 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 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 // NOTE: accu::math::sum takes mln_sum(T) as first template 00089 // parameter since in the core of the deviation::take method, 00090 // this accumulator takes data of type mln_sum(T). 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 } // end of namespace mln::accu::meta 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 // FIXME 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(); // Safety. 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 } // end of namespace mln::accu::stat 00185 00186 } // end of namespace mln::accu 00187 00188 } // end of namespace mln 00189 00190 00191 #endif // ! MLN_ACCU_STAT_DEVIATION_HH