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

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

fixes #580

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