source: trunk/yat/normalizer/Centralizer.h @ 1739

Last change on this file since 1739 was 1739, checked in by Peter, 12 years ago

fixes #479 and removing some debug output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_normalizer_centralizer_
2#define _theplu_yat_normalizer_centralizer_
3
4// $Id: Centralizer.h 1739 2009-01-21 01:34:18Z peter $
5
6/*
7  Copyright (C) 2008, 2009 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "yat/statistics/Average.h"
26#include "yat/utility/DataIterator.h"
27#include "yat/utility/iterator_traits.h"
28#include "yat/utility/WeightIterator.h"
29
30#include <algorithm>
31#include <functional>
32
33namespace theplu {
34namespace yat {
35namespace normalizer {
36
37  /**
38     \brief Centralize a range
39
40     The center is calculated and then that value is subtracted from
41     each element. By default the center is defined as the arithmetic
42     mean, but this can be changed by providing a suitable
43     UnaryFunction.
44
45     \since New in yat 0.5
46   */
47  template<class UnaryFunction = statistics::Average>
48  class Centralizer
49  {
50  public:
51    /**
52       \brief default constructor
53
54       Internal UnaryFunction is created using its default
55       constructor.
56     */
57    Centralizer(void){}
58
59    /**
60       \param uf unary function defining the center.
61     */
62    Centralizer(const UnaryFunction& uf)
63      : func_(uf) {}
64
65    /**
66       Calculates the center \a c of the range [first, last) using
67       UnaryFunction. This value, \a c, is then subtracted from each
68       element in the range [first, last) and assigned to the
69       corresponding element in range [result, result + (last-first) ).
70
71       It is possible to centralize a range "in place"; it is
72       permissible for the iterators \a first and \a result to be the
73       same.
74
75       \see std::transform
76     */
77    template<class InputIterator, class OutputIterator>
78    void operator()(InputIterator first, InputIterator last,
79                    OutputIterator result) const
80    {
81      typename utility::weighted_iterator_traits<InputIterator>::type tag;
82      normalize(first, last, result, tag);
83    }
84
85  private:
86    UnaryFunction func_;
87
88    // unweighted version
89    template<class InputIterator, class OutputIterator>
90    void normalize(InputIterator first,InputIterator last,OutputIterator result,
91                   utility::unweighted_iterator_tag tag) const
92    {
93      std::transform(first, last, result, 
94                     std::bind2nd(std::minus<double>(), func_(first, last)));
95    }
96
97
98    // weighted version
99    template<class InputIterator, class OutputIterator>
100    void normalize(InputIterator first,InputIterator last,OutputIterator result,
101                   utility::weighted_iterator_tag tag) const
102    {
103      std::copy(utility::weight_iterator(first), 
104                utility::weight_iterator(last), 
105                utility::weight_iterator(result));
106      std::transform(utility::data_iterator(first), 
107                     utility::data_iterator(last), 
108                     utility::data_iterator(result),
109                     std::bind2nd(std::minus<double>(),func_(first, last)));
110    }
111
112  };
113
114}}} // end of namespace normalizer, yat and thep
115#endif
Note: See TracBrowser for help on using the repository browser.