• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

deviation.hh

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_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       accu::math::sum<T,S>   sum_;
00088       T mean_;
00089     };
00090 
00091 
00092 
00093     template <typename I, typename S, typename M>
00094     struct deviation< util::pix<I>, S,M >;
00095 
00096 
00097     namespace meta
00098     {
00099 
00101       struct deviation : public Meta_Accumulator< deviation >
00102       {
00103         template < typename T,
00104                    typename S = mln_sum(T),
00105                    typename M = S >
00106         struct with
00107         {
00108           typedef accu::stat::deviation<T,S,M> ret;
00109         };
00110       };
00111 
00112     } // end of namespace mln::accu::meta
00113 
00114 
00115 # ifndef MLN_INCLUDE_ONLY
00116 
00117     template <typename T, typename S, typename M>
00118     inline
00119     deviation<T,S,M>::deviation(const T mean)
00120     {
00121       init();
00122       mean_ = mean;
00123     }
00124 
00125     template <typename T, typename S, typename M>
00126     inline
00127     void
00128     deviation<T,S,M>::init()
00129     {
00130       count_.init();
00131       sum_.init();
00132     }
00133 
00134     template <typename T, typename S, typename M>
00135     inline
00136     void deviation<T,S,M>::take(const argument& t)
00137     {
00138       count_.take(t);
00139       sum_.take(mln::math::sqr(t - mean_));
00140     }
00141 
00142     template <typename T, typename S, typename M>
00143     inline
00144     void
00145     deviation<T,S,M>::take(const deviation<T,S,M>& other)
00146     {
00147       // FIXME
00148       count_.take(other.count_);
00149       sum_.take(other.sum_);
00150     }
00151 
00152     template <typename T, typename S, typename M>
00153     inline
00154     M
00155     deviation<T,S,M>::to_result() const
00156     {
00157       unsigned n = count_.to_result();
00158       if (n == 0u)
00159         return M(); // Safety.
00160       return static_cast<M>(mln::math::sqrt(sum_.to_result() / n));
00161     }
00162 
00163     template <typename T, typename S, typename M>
00164     inline
00165     deviation<T,S,M>::operator M() const
00166     {
00167       return to_result();
00168     }
00169 
00170     template <typename T, typename S, typename M>
00171     inline
00172     bool
00173     deviation<T,S,M>::is_valid() const
00174     {
00175       return count_.to_result() != 0;
00176     }
00177 
00178 # endif // ! MLN_INCLUDE_ONLY
00179 
00180     } // end of namespace mln::accu::stat
00181 
00182   } // end of namespace mln::accu
00183 
00184 } // end of namespace mln
00185 
00186 
00187 #endif // ! MLN_ACCU_STAT_DEVIATION_HH

Generated on Thu Sep 8 2011 18:31:45 for Milena (Olena) by  doxygen 1.7.1