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

var.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_VAR_HH
00027 # define MLN_ACCU_STAT_VAR_HH
00028 
00033 
00034 # include <cmath>
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/algebra/vec.hh>
00037 # include <mln/algebra/mat.hh>
00038 # include <mln/fun/i2v/all_to.hh>
00039 # include <mln/util/pix.hh>
00040 
00041 
00042 namespace mln
00043 {
00044 
00045   namespace accu
00046   {
00047 
00048     namespace stat
00049     {
00050 
00056       //
00057       template <typename T>
00058       struct var : public mln::accu::internal::base< algebra::mat< T::dim, T::dim, float >,
00059                                                      var<T> >
00060       {
00061         enum { dim = T::dim };
00062         typedef T argument;
00063         typedef algebra::mat<dim,dim,float> result;
00064 
00065         var();
00066 
00069         void init();
00070         void take(const argument& v);
00071         void take(const var<T>& other);
00072 
00073         void take_as_init_(const argument& v);
00074         void take_n_times_(unsigned n_times, const argument& v);
00076 
00078         result to_result() const;
00079 
00081         result variance() const;
00082 
00084         unsigned n_items() const;
00085 
00086 
00088         typedef algebra::vec<dim,float> mean_t;
00089         // ...
00090 
00092         mean_t mean() const;
00093 
00095         bool is_valid() const;
00096 
00097       protected:
00098 
00099         unsigned n_;
00100         algebra::vec<dim,float>     sum_;
00101         algebra::mat<dim,dim,float> cov_;
00102       };
00103 
00104 
00105 
00106 # ifndef MLN_INCLUDE_ONLY
00107 
00108     template <typename T>
00109     inline
00110     var<T>::var()
00111     {
00112       init();
00113     }
00114 
00115     template <typename T>
00116     inline
00117     void
00118     var<T>::init()
00119     {
00120       n_ = 0;
00121       sum_.set_all(0);
00122       cov_.set_all(0);
00123     }
00124 
00125     template <typename T>
00126     inline
00127     void
00128     var<T>::take(const argument& v)
00129     {
00130       ++n_;
00131       sum_ += v;
00132       cov_ += v * v.t();
00133     }
00134 
00135     template <typename T>
00136     inline
00137     void
00138     var<T>::take(const var<T>& other)
00139     {
00140       n_   += other.n_;
00141       cov_ += other.cov_;
00142     }
00143 
00144     template <typename T>
00145     inline
00146     void
00147     var<T>::take_as_init_(const argument& v)
00148     {
00149       n_ = 1;
00150       sum_ = v;
00151       cov_ = v * v.t();
00152     }
00153 
00154     template <typename T>
00155     inline
00156     void
00157     var<T>::take_n_times_(unsigned n_times, const argument& v)
00158     {
00159       n_ += n_times;
00160       sum_ += n_times * v;
00161       cov_ += n_times * v * v.t();
00162     }
00163 
00164 
00165     template <typename T>
00166     inline
00167     mln_result(var<T>)
00168     var<T>::to_result() const
00169     {
00170       static result null_ = literal::zero;
00171 
00172       if (n_ == 0u)
00173         return null_; // Safety.
00174 
00175       return (cov_ - sum_ * sum_.t() / n_) / n_;
00176       // Shorter than:
00177       // mean_ = sum_ / n_
00178       // var_  = cov_ / n_ - mean_ * mean_.t() 
00179     }
00180 
00181     template <typename T>
00182     inline
00183     mln_result(var<T>)
00184     var<T>::variance() const
00185     {
00186       return to_result();
00187     }
00188 
00189     template <typename T>
00190     inline
00191     unsigned
00192     var<T>::n_items() const
00193     {
00194       return n_;
00195     }
00196 
00197     template <typename T>
00198     inline
00199     typename var<T>::mean_t
00200     var<T>::mean() const
00201     {
00202       static algebra::vec<dim,float> null_ = literal::zero;
00203 
00204       if (n_ == 0u)
00205         return null_; // Safety.
00206 
00207       return sum_ / n_;
00208     }
00209 
00210     template <typename T>
00211     inline
00212     bool
00213     var<T>::is_valid() const
00214     {
00215       return n_ != 0;
00216     }
00217 
00218 # endif // ! MLN_INCLUDE_ONLY
00219 
00220     } // end of namespace mln::accu::stat
00221 
00222   } // end of namespace mln::accu
00223 
00224 } // end of namespace mln
00225 
00226 
00227 #endif // ! MLN_ACCU_STAT_VAR_HH

Generated on Tue Oct 4 2011 15:25:05 for Milena (Olena) by  doxygen 1.7.1