$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
deviation.hh
1 // Copyright (C) 2009, 2010 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_DEVIATION_HH
27 # define MLN_ACCU_STAT_DEVIATION_HH
28 
32 
33 # include <mln/accu/internal/base.hh>
34 # include <mln/accu/math/count.hh>
35 # include <mln/accu/math/sum.hh>
36 # include <mln/math/sqr.hh>
37 # include <mln/math/sqrt.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace accu
44  {
45 
46  namespace stat
47  {
48 
50 
59  template <typename T,
60  typename S = mln_sum(T),
61  typename M = S>
62  struct deviation : public mln::accu::internal::base< M , deviation<T,S,M> >
63  {
64  typedef T argument;
65  typedef M result;
66 
67  deviation(const T mean);
68 
71  void init();
72  void take(const argument& t);
73  void take(const deviation<T,S,M>& other);
75 
77  M to_result() const;
78  operator M () const;
79 
82  bool is_valid() const;
83 
84  protected:
85 
87 
88  // NOTE: accu::math::sum takes mln_sum(T) as first template
89  // parameter since in the core of the deviation::take method,
90  // this accumulator takes data of type mln_sum(T).
92  T mean_;
93  };
94 
95 
96 
97  template <typename I, typename S, typename M>
98  struct deviation< util::pix<I>, S,M >;
99 
100 
101  namespace meta
102  {
103 
105  struct deviation : public Meta_Accumulator< deviation >
106  {
107  template < typename T,
108  typename S = mln_sum(T),
109  typename M = S >
110  struct with
111  {
113  };
114  };
115 
116  } // end of namespace mln::accu::meta
117 
118 
119 # ifndef MLN_INCLUDE_ONLY
120 
121  template <typename T, typename S, typename M>
122  inline
124  {
125  init();
126  mean_ = mean;
127  }
128 
129  template <typename T, typename S, typename M>
130  inline
131  void
133  {
134  count_.init();
135  sum_.init();
136  }
137 
138  template <typename T, typename S, typename M>
139  inline
140  void deviation<T,S,M>::take(const argument& t)
141  {
142  count_.take(t);
143  sum_.take(mln::math::sqr(t - mean_));
144  }
145 
146  template <typename T, typename S, typename M>
147  inline
148  void
149  deviation<T,S,M>::take(const deviation<T,S,M>& other)
150  {
151  // FIXME
152  count_.take(other.count_);
153  sum_.take(other.sum_);
154  }
155 
156  template <typename T, typename S, typename M>
157  inline
158  M
160  {
161  unsigned n = count_.to_result();
162  if (n == 0u)
163  return M(); // Safety.
164  return static_cast<M>(mln::math::sqrt(sum_.to_result() / n));
165  }
166 
167  template <typename T, typename S, typename M>
168  inline
169  deviation<T,S,M>::operator M() const
170  {
171  return to_result();
172  }
173 
174  template <typename T, typename S, typename M>
175  inline
176  bool
178  {
179  return count_.to_result() != 0;
180  }
181 
182 # endif // ! MLN_INCLUDE_ONLY
183 
184  } // end of namespace mln::accu::stat
185 
186  } // end of namespace mln::accu
187 
188 } // end of namespace mln
189 
190 
191 #endif // ! MLN_ACCU_STAT_DEVIATION_HH