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

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

converted files to utf-8. fixes #577

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1#ifndef _theplu_yat_normalizer_centralizer_
2#define _theplu_yat_normalizer_centralizer_
3
4// $Id: Centralizer.h 2119 2009-12-12 23:11:43Z 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 <algorithm>
32#include <functional>
33
34namespace theplu {
35namespace yat {
36namespace normalizer {
37
38  /**
39     \brief Centralize a range
40
41     The center is calculated and then that value is subtracted from
42     each element. By default the center is defined as the arithmetic
43     mean, but this can be changed by providing a suitable
44     UnaryFunction.
45
46     \since New in yat 0.5
47   */
48  template<class UnaryFunction = statistics::Average>
49  class Centralizer
50  {
51  public:
52    /**
53       \brief default constructor
54
55       Internal UnaryFunction is created using its default
56       constructor.
57     */
58    Centralizer(void){}
59
60    /**
61       \param uf unary function defining the center.
62     */
63    Centralizer(const UnaryFunction& uf)
64      : func_(uf) {}
65
66    /**
67       Calculates the center \a c of the range [first, last) using
68       UnaryFunction. This value, \a c, is then subtracted from each
69       element in the range [first, last) and assigned to the
70       corresponding element in range [result, result + (last-first) ).
71
72       It is possible to centralize a range "in place"; it is
73       permissible for the iterators \a first and \a result to be the
74       same.
75
76       \see std::transform
77     */
78    template<class InputIterator, class OutputIterator>
79    void operator()(InputIterator first, InputIterator last,
80                    OutputIterator result) const
81    {
82      typename utility::weighted_iterator_traits<InputIterator>::type tag;
83      normalize(first, last, result, tag);
84    }
85
86  private:
87    UnaryFunction func_;
88
89    // unweighted version
90    template<class InputIterator, class OutputIterator>
91    void normalize(InputIterator first,InputIterator last,OutputIterator result,
92                   utility::unweighted_iterator_tag tag) const
93    {
94      std::transform(first, last, result, 
95                     std::bind2nd(std::minus<double>(), func_(first, last)));
96    }
97
98
99    // weighted version
100    template<class InputIterator, class OutputIterator>
101    void normalize(InputIterator first,InputIterator last,OutputIterator result,
102                   utility::weighted_iterator_tag tag) const
103    {
104      std::copy(utility::weight_iterator(first), 
105                utility::weight_iterator(last), 
106                utility::weight_iterator(result));
107      std::transform(utility::data_iterator(first), 
108                     utility::data_iterator(last), 
109                     utility::data_iterator(result),
110                     std::bind2nd(std::minus<double>(),func_(first, last)));
111    }
112
113  };
114
115}}} // end of namespace normalizer, yat and thep
116#endif
Note: See TracBrowser for help on using the repository browser.