3587: Change histogram printing.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Change histogram printing. * mln/debug/histo.hh: Remove overload for std::vector. Use values instead of indices. * mln/histo/array.hh (operator=), (cpy ctor): New. * mln/accu/stat/variance.hh: New. accu/stat/variance.hh | 201 +++++++++++++++++++++++++++++++------------------- debug/histo.hh | 28 +++--- histo/array.hh | 22 +++++ 3 files changed, 162 insertions(+), 89 deletions(-) Index: mln/debug/histo.hh --- mln/debug/histo.hh (revision 3586) +++ mln/debug/histo.hh (working copy) @@ -31,7 +31,9 @@ /// \file mln/debug/histo.hh /// -/// save a histogram to a plot data file. +/// Save a histogram to a plot data file. +/// +/// \todo Move to io::histo::save. # include <iostream> # include <fstream> @@ -41,6 +43,7 @@ # include <mln/core/image/image2d.hh> # include <mln/draw/line.hh> + namespace mln { @@ -48,32 +51,27 @@ { + template <typename T> void - histo(const histo::array<unsigned>& h, const std::string& filename); + histo(const histo::array<T>& h, const std::string& filename); - void - histo(const std::vector<unsigned>& h, const std::string& filename); -# ifndef MLN_INCLUDE_ONLY - void - histo(const histo::array<unsigned>& h, const std::string& filename) - { - histo(h.vect(), filename); - } +# ifndef MLN_INCLUDE_ONLY + template <typename T> void - histo(const std::vector<unsigned>& h, const std::string& filename) + histo(const histo::array<T>& h, const std::string& filename) { std::ofstream file(filename.c_str()); if (! file) { std::cerr << "error: cannot open file '" << filename << "'!"; - abort(); + std::abort(); } - - for (unsigned i = 0; i < h.size(); ++i) - file << i << ' ' << h[i] << std::endl; + mln_viter(value::set<T>) v(h.vset()); + for_all(v) + file << v << ' ' << h(v) << std::endl; } Index: mln/histo/array.hh --- mln/histo/array.hh (revision 3586) +++ mln/histo/array.hh (working copy) @@ -54,6 +54,9 @@ array(); + array(const array& other); + array& operator=(const array& other); + void clear(); unsigned operator()(const T& v) const; @@ -91,6 +94,25 @@ template <typename T> inline + array<T>::array(const array& other) + : s_(other.s_), + h_(other.h_) + { + } + + template <typename T> + inline + array<T>& + array<T>::operator=(const array& other) + { + if (&other == this) + return *this; + h_ = other.h_; + return *this; + } + + template <typename T> + inline void array<T>::clear() { Index: mln/accu/stat/variance.hh --- mln/accu/stat/variance.hh (revision 3570) +++ mln/accu/stat/variance.hh (working copy) @@ -1,5 +1,4 @@ -// Copyright (C) 2009 EPITA Research and Development Laboratory -// (LRDE) +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) // // This file is part of the Olena Library. This library is free // software; you can redistribute it and/or modify it under the terms @@ -26,18 +25,15 @@ // reasons why the executable file might be covered by the GNU General // Public License. -#ifndef MLN_ACCU_STAT_DEVIATION_HH -# define MLN_ACCU_STAT_DEVIATION_HH +#ifndef MLN_ACCU_STAT_VARIANCE_HH +# define MLN_ACCU_STAT_VARIANCE_HH -/// \file mln/accu/stat/deviation.hh +/// \file mln/accu/stat/variance.hh /// -/// Define an accumulator that computes a standard deviation. +/// Define an accumulator that computes the variance of a set of values. +# include <cmath> # include <mln/accu/internal/base.hh> -# include <mln/accu/count.hh> -# include <mln/accu/sum.hh> -# include <mln/math/sqr.hh> -# include <mln/math/sqrt.hh> namespace mln @@ -49,34 +45,51 @@ namespace stat { - /// Generic standard deviation accumulator class. + /// Variance accumulator class. /*! - * Parameter \c T is the type of values that we sum. Parameter \c - * S is the type to store the standard deviation; the default type of - * \c S is the summation type (property) of \c T. Parameter \c M - * is the type of the mean value; the default type of \c M is \c - * S. + * Parameter \c T is the type of values that we sum. Parameter + * \c S is the type to store the value sum and the sum of value + * * value; the default type of \c S is the summation type + * (property) of \c T. Parameter \c R is the type of the mean + * and variance values; the default type of \c R is \c S. */ template <typename T, typename S = mln_sum(T), - typename M = S> - struct deviation : public mln::accu::internal::base< M , deviation<T,S,M> > + typename R = S> + struct variance : public mln::accu::internal::base< R , variance<T,S,R> > { typedef T argument; - typedef M result; + typedef R result; - deviation(const T mean); + variance(); /// Manipulators. /// \{ void init(); + void take_as_init(const argument& t); void take(const argument& t); - void take(const deviation<T,S,M>& other); + void take(const variance<T,S,R>& other); + + void take(unsigned n_times, const argument& t); // Extra. /// \} - /// Get the value of the accumulator. - M to_result() const; - operator M () const; + /// Get the accumulator result (the variance value). + R to_result() const; + + /// Get the variance value. + R var() const; + + /// Get the standard deviation value. + R standard_deviation() const; + + /// Get the mean value. + R mean() const; + + /// Get the sum value. + S sum() const; + + /// Get the number of items. + unsigned n_items() const; /// Check whether this accu is able to return a result. /// Always true here. @@ -84,96 +97,136 @@ protected: - accu::count<T> count_; - accu::sum<T,S> sum_; - T mean_; + unsigned n_; + S sum_, sum2_; }; - template <typename I, typename S, typename M> - struct deviation< util::pix<I>, S,M >; + template <typename I, typename S, typename R> + struct variance< util::pix<I>, S,R >; - namespace meta - { - /// Meta accumulator for deviation. - struct deviation : public Meta_Accumulator< deviation > - { - template < typename T, - typename S = mln_sum(T), - typename M = S > - struct with +# ifndef MLN_INCLUDE_ONLY + + template <typename T, typename S, typename R> + inline + variance<T,S,R>::variance() { - typedef accu::stat::deviation<T,S,M> ret; - }; - }; + init(); + } - } // end of namespace mln::accu::meta + template <typename T, typename S, typename R> + inline + void + variance<T,S,R>::init() + { + n_ = 0; + sum_ = sum2_ = 0; + } + template <typename T, typename S, typename R> + inline + void + variance<T,S,R>::take(const argument& t) + { + ++n_; + sum_ += t; + sum2_ += t * t; + } -# ifndef MLN_INCLUDE_ONLY + template <typename T, typename S, typename R> + inline + void + variance<T,S,R>::take(unsigned n_times, const argument& t) + { + if (n_times == 0u) + return; + n_ += n_times; + sum_ += n_times * t; + sum2_ += n_times * t * t; + } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline - deviation<T,S,M>::deviation(const T mean) + void + variance<T,S,R>::take_as_init(const argument& t) { - init(); - mean_ = mean; + n_ = 1; + sum_ = t; + sum2_ = t * t; } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline void - deviation<T,S,M>::init() + variance<T,S,R>::take(const variance<T,S,R>& other) { - count_.init(); - sum_.init(); + n_ += other.n_; + sum_ += other.sum_; + sum2_ += other.sum2_; } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline - void deviation<T,S,M>::take(const argument& t) + R + variance<T,S,R>::to_result() const { - count_.take(t); - sum_.take(math::sqr(t - mean_)); + if (n_ == 0u) + return 0; // Safety. + S m_ = sum_ / n_; + return sum2_ / n_ - m_ * m_; } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline - void - deviation<T,S,M>::take(const deviation<T,S,M>& other) + R + variance<T,S,R>::mean() const + { + if (n_ == 0u) + return 0; // Safety. + return sum_ / n_; + } + + template <typename T, typename S, typename R> + inline + S + variance<T,S,R>::sum() const { - // FIXME - count_.take(other.count_); - sum_.take(other.sum_); + return sum_; } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline - M - deviation<T,S,M>::to_result() const + unsigned + variance<T,S,R>::n_items() const { - unsigned n = count_.to_result(); - if (n == 0u) - return M(); // Safety. - return static_cast<M>(math::sqrt(sum_.to_result() / n)); + return n_; } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> inline - deviation<T,S,M>::operator M() const + R + variance<T,S,R>::var() const { return to_result(); } - template <typename T, typename S, typename M> + template <typename T, typename S, typename R> + inline + R + variance<T,S,R>::standard_deviation() const + { + return std::sqrt(to_result()); + } + + template <typename T, typename S, typename R> inline bool - deviation<T,S,M>::is_valid() const + variance<T,S,R>::is_valid() const { - return count_.to_result() != 0; + return n_ != 0; } # endif // ! MLN_INCLUDE_ONLY @@ -185,4 +238,4 @@ } // end of namespace mln -#endif // ! MLN_ACCU_STAT_DEVIATION_HH +#endif // ! MLN_ACCU_STAT_VARIANCE_HH Property changes on: mln/accu/stat/variance.hh ___________________________________________________________________ Added: svn:mergeinfo
participants (1)
-
Thierry Geraud