$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
accu/stat/var.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_VAR_HH
27 # define MLN_ACCU_STAT_VAR_HH
28 
33 
34 # include <cmath>
35 # include <mln/accu/internal/base.hh>
36 # include <mln/algebra/vec.hh>
37 # include <mln/algebra/mat.hh>
38 # include <mln/fun/i2v/all_to.hh>
39 # include <mln/util/pix.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace accu
46  {
47 
48  namespace stat
49  {
50 
56  //
57  template <typename T>
58  struct var : public mln::accu::internal::base< algebra::mat< T::dim, T::dim, float >,
59  var<T> >
60  {
61  enum { dim = T::dim };
62  typedef T argument;
64 
65  var();
66 
69  void init();
70  void take(const argument& v);
71  void take(const var<T>& other);
72 
73  void take_as_init_(const argument& v);
74  void take_n_times_(unsigned n_times, const argument& v);
76 
78  result to_result() const;
79 
81  result variance() const;
82 
84  unsigned n_items() const;
85 
86 
89  // ...
90 
92  mean_t mean() const;
93 
95  bool is_valid() const;
96 
97  protected:
98 
99  unsigned n_;
102  };
103 
104 
105 
106 # ifndef MLN_INCLUDE_ONLY
107 
108  template <typename T>
109  inline
110  var<T>::var()
111  {
112  init();
113  }
114 
115  template <typename T>
116  inline
117  void
118  var<T>::init()
119  {
120  n_ = 0;
121  sum_.set_all(0);
122  cov_.set_all(0);
123  }
124 
125  template <typename T>
126  inline
127  void
128  var<T>::take(const argument& v)
129  {
130  ++n_;
131  sum_ += v;
132  cov_ += v * v.t();
133  }
134 
135  template <typename T>
136  inline
137  void
138  var<T>::take(const var<T>& other)
139  {
140  n_ += other.n_;
141  cov_ += other.cov_;
142  }
143 
144  template <typename T>
145  inline
146  void
147  var<T>::take_as_init_(const argument& v)
148  {
149  n_ = 1;
150  sum_ = v;
151  cov_ = v * v.t();
152  }
153 
154  template <typename T>
155  inline
156  void
157  var<T>::take_n_times_(unsigned n_times, const argument& v)
158  {
159  n_ += n_times;
160  sum_ += n_times * v;
161  cov_ += n_times * v * v.t();
162  }
163 
164 
165  template <typename T>
166  inline
167  mln_result(var<T>)
168  var<T>::to_result() const
169  {
170  static result null_ = literal::zero;
171 
172  if (n_ == 0u)
173  return null_; // Safety.
174 
175  return (cov_ - sum_ * sum_.t() / n_) / n_;
176  // Shorter than:
177  // mean_ = sum_ / n_
178  // var_ = cov_ / n_ - mean_ * mean_.t()
179  }
180 
181  template <typename T>
182  inline
183  mln_result(var<T>)
184  var<T>::variance() const
185  {
186  return to_result();
187  }
188 
189  template <typename T>
190  inline
191  unsigned
192  var<T>::n_items() const
193  {
194  return n_;
195  }
196 
197  template <typename T>
198  inline
199  typename var<T>::mean_t
200  var<T>::mean() const
201  {
202  static algebra::vec<dim,float> null_ = literal::zero;
203 
204  if (n_ == 0u)
205  return null_; // Safety.
206 
207  return sum_ / n_;
208  }
209 
210  template <typename T>
211  inline
212  bool
213  var<T>::is_valid() const
214  {
215  return n_ != 0;
216  }
217 
218 # endif // ! MLN_INCLUDE_ONLY
219 
220  } // end of namespace mln::accu::stat
221 
222  } // end of namespace mln::accu
223 
224 } // end of namespace mln
225 
226 
227 #endif // ! MLN_ACCU_STAT_VAR_HH