Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009 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_VARIANCE_HH 00027 # define MLN_ACCU_STAT_VARIANCE_HH 00028 00032 00033 # include <cmath> 00034 # include <mln/accu/internal/base.hh> 00035 # include <mln/util/pix.hh> 00036 00037 00038 namespace mln 00039 { 00040 00041 namespace accu 00042 { 00043 00044 namespace stat 00045 { 00046 00048 00058 template <typename T, 00059 typename S = mln_sum(T), 00060 typename R = S> 00061 struct variance : public mln::accu::internal::base< R , variance<T,S,R> > 00062 { 00063 typedef T argument; 00064 typedef R result; 00065 00066 variance(); 00067 00070 void init(); 00071 void take_as_init(const argument& t); 00072 void take(const argument& t); 00073 void take(const variance<T,S,R>& other); 00074 00075 void take(unsigned n_times, const argument& t); // Extra. 00077 00079 R to_result() const; 00080 00082 R var() const; 00083 00085 R standard_deviation() const; 00086 00088 R mean() const; 00089 00091 S sum() const; 00092 00094 unsigned n_items() const; 00095 00098 bool is_valid() const; 00099 00100 protected: 00101 00102 unsigned n_; 00103 S sum_, sum2_; 00104 }; 00105 00106 00107 00108 template <typename I, typename S, typename R> 00109 struct variance< util::pix<I>, S,R >; 00110 00111 00112 00113 # ifndef MLN_INCLUDE_ONLY 00114 00115 template <typename T, typename S, typename R> 00116 inline 00117 variance<T,S,R>::variance() 00118 { 00119 init(); 00120 } 00121 00122 template <typename T, typename S, typename R> 00123 inline 00124 void 00125 variance<T,S,R>::init() 00126 { 00127 n_ = 0; 00128 sum_ = sum2_ = literal::zero; 00129 } 00130 00131 template <typename T, typename S, typename R> 00132 inline 00133 void 00134 variance<T,S,R>::take(const argument& t) 00135 { 00136 ++n_; 00137 sum_ += t; 00138 sum2_ += t * t; 00139 } 00140 00141 template <typename T, typename S, typename R> 00142 inline 00143 void 00144 variance<T,S,R>::take(unsigned n_times, const argument& t) 00145 { 00146 if (n_times == 0u) 00147 return; 00148 n_ += n_times; 00149 sum_ += n_times * t; 00150 sum2_ += n_times * t * t; 00151 } 00152 00153 template <typename T, typename S, typename R> 00154 inline 00155 void 00156 variance<T,S,R>::take_as_init(const argument& t) 00157 { 00158 n_ = 1; 00159 sum_ = t; 00160 sum2_ = t * t; 00161 } 00162 00163 template <typename T, typename S, typename R> 00164 inline 00165 void 00166 variance<T,S,R>::take(const variance<T,S,R>& other) 00167 { 00168 n_ += other.n_; 00169 sum_ += other.sum_; 00170 sum2_ += other.sum2_; 00171 } 00172 00173 template <typename T, typename S, typename R> 00174 inline 00175 R 00176 variance<T,S,R>::to_result() const 00177 { 00178 if (n_ == 0u) 00179 return 0; // Safety. 00180 S m_ = sum_ / n_; 00181 return sum2_ / n_ - m_ * m_; 00182 } 00183 00184 template <typename T, typename S, typename R> 00185 inline 00186 R 00187 variance<T,S,R>::mean() const 00188 { 00189 if (n_ == 0u) 00190 return literal::zero; // Safety. 00191 return sum_ / n_; 00192 } 00193 00194 template <typename T, typename S, typename R> 00195 inline 00196 S 00197 variance<T,S,R>::sum() const 00198 { 00199 return sum_; 00200 } 00201 00202 template <typename T, typename S, typename R> 00203 inline 00204 unsigned 00205 variance<T,S,R>::n_items() const 00206 { 00207 return n_; 00208 } 00209 00210 template <typename T, typename S, typename R> 00211 inline 00212 R 00213 variance<T,S,R>::var() const 00214 { 00215 return to_result(); 00216 } 00217 00218 template <typename T, typename S, typename R> 00219 inline 00220 R 00221 variance<T,S,R>::standard_deviation() const 00222 { 00223 return std::sqrt(to_result()); 00224 } 00225 00226 template <typename T, typename S, typename R> 00227 inline 00228 bool 00229 variance<T,S,R>::is_valid() const 00230 { 00231 return n_ != 0; 00232 } 00233 00234 # endif // ! MLN_INCLUDE_ONLY 00235 00236 } // end of namespace mln::accu::stat 00237 00238 } // end of namespace mln::accu 00239 00240 } // end of namespace mln 00241 00242 00243 #endif // ! MLN_ACCU_STAT_VARIANCE_HH