$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
variance.hh
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ACCU_STAT_VARIANCE_HH
27 # define MLN_ACCU_STAT_VARIANCE_HH
28 
32 
33 # include <cmath>
34 # include <mln/accu/internal/base.hh>
35 # include <mln/util/pix.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace accu
42  {
43 
44  namespace stat
45  {
46 
48 
58  template <typename T,
59  typename S = mln_sum(T),
60  typename R = S>
61  struct variance : public mln::accu::internal::base< R , variance<T,S,R> >
62  {
63  typedef T argument;
64  typedef R result;
65 
66  variance();
67 
70  void init();
71  void take_as_init(const argument& t);
72  void take(const argument& t);
73  void take(const variance<T,S,R>& other);
74 
75  void take(unsigned n_times, const argument& t); // Extra.
77 
79  R to_result() const;
80 
82  R var() const;
83 
85  R standard_deviation() const;
86 
88  R mean() const;
89 
91  S sum() const;
92 
94  unsigned n_items() const;
95 
98  bool is_valid() const;
99 
100  protected:
101 
102  unsigned n_;
104  };
105 
106 
107 
108  template <typename I, typename S, typename R>
109  struct variance< util::pix<I>, S,R >;
110 
111 
112 
113 # ifndef MLN_INCLUDE_ONLY
114 
115  template <typename T, typename S, typename R>
116  inline
117  variance<T,S,R>::variance()
118  {
119  init();
120  }
121 
122  template <typename T, typename S, typename R>
123  inline
124  void
126  {
127  n_ = 0;
128  sum_ = sum2_ = literal::zero;
129  }
130 
131  template <typename T, typename S, typename R>
132  inline
133  void
134  variance<T,S,R>::take(const argument& t)
135  {
136  ++n_;
137  sum_ += t;
138  sum2_ += t * t;
139  }
140 
141  template <typename T, typename S, typename R>
142  inline
143  void
144  variance<T,S,R>::take(unsigned n_times, const argument& t)
145  {
146  if (n_times == 0u)
147  return;
148  n_ += n_times;
149  sum_ += n_times * t;
150  sum2_ += n_times * t * t;
151  }
152 
153  template <typename T, typename S, typename R>
154  inline
155  void
156  variance<T,S,R>::take_as_init(const argument& t)
157  {
158  n_ = 1;
159  sum_ = t;
160  sum2_ = t * t;
161  }
162 
163  template <typename T, typename S, typename R>
164  inline
165  void
166  variance<T,S,R>::take(const variance<T,S,R>& other)
167  {
168  n_ += other.n_;
169  sum_ += other.sum_;
170  sum2_ += other.sum2_;
171  }
172 
173  template <typename T, typename S, typename R>
174  inline
175  R
177  {
178  if (n_ == 0u)
179  return 0; // Safety.
180  S m_ = sum_ / n_;
181  return sum2_ / n_ - m_ * m_;
182  }
183 
184  template <typename T, typename S, typename R>
185  inline
186  R
187  variance<T,S,R>::mean() const
188  {
189  if (n_ == 0u)
190  return literal::zero; // Safety.
191  return sum_ / n_;
192  }
193 
194  template <typename T, typename S, typename R>
195  inline
196  S
197  variance<T,S,R>::sum() const
198  {
199  return sum_;
200  }
201 
202  template <typename T, typename S, typename R>
203  inline
204  unsigned
206  {
207  return n_;
208  }
209 
210  template <typename T, typename S, typename R>
211  inline
212  R
213  variance<T,S,R>::var() const
214  {
215  return to_result();
216  }
217 
218  template <typename T, typename S, typename R>
219  inline
220  R
222  {
223  return std::sqrt(to_result());
224  }
225 
226  template <typename T, typename S, typename R>
227  inline
228  bool
230  {
231  return n_ != 0;
232  }
233 
234 # endif // ! MLN_INCLUDE_ONLY
235 
236  } // end of namespace mln::accu::stat
237 
238  } // end of namespace mln::accu
239 
240 } // end of namespace mln
241 
242 
243 #endif // ! MLN_ACCU_STAT_VARIANCE_HH