1 | #ifndef _theplu_yat_normalization_centralizer_ |
---|
2 | #define _theplu_yat_normalization_centralizer_ |
---|
3 | |
---|
4 | /* |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "yat/statistics/Average.h" |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <functional> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace normalization { |
---|
33 | |
---|
34 | /** |
---|
35 | \brief Centralize a range |
---|
36 | |
---|
37 | The center is calculated and then that value is subtracted from |
---|
38 | each element. By default the center is defined as the arithmetic |
---|
39 | mean, but this can be changed by providing a suitable |
---|
40 | UnaryFunction. |
---|
41 | |
---|
42 | \since New in yat 0.5 |
---|
43 | */ |
---|
44 | template<class UnaryFunction = statistics::Average> |
---|
45 | class Centralizer |
---|
46 | { |
---|
47 | public: |
---|
48 | /** |
---|
49 | \brief default constructor |
---|
50 | |
---|
51 | Creates a UnaryFunction using default constructor. |
---|
52 | */ |
---|
53 | Centralizer(void){} |
---|
54 | |
---|
55 | /** |
---|
56 | \param uf unary function defining the center. |
---|
57 | */ |
---|
58 | Centralizer(const UnaryFunction& uf) |
---|
59 | : func_(uf) {} |
---|
60 | |
---|
61 | /** |
---|
62 | Calculates the center \a c of the range [first, last) using |
---|
63 | UnaryFunction. This value, \a c, is then subtracted from each |
---|
64 | element in the range [first, last) and assigned to the |
---|
65 | corresponding element in range [result, result + (last-first) ). |
---|
66 | |
---|
67 | It is possible to centralize a range "in place"; it is |
---|
68 | permissible for the iterators \a first and \a result to be the |
---|
69 | same. \see std::transform |
---|
70 | |
---|
71 | \return result + (last-first) |
---|
72 | */ |
---|
73 | template<class InputIterator, class OutputIterator> |
---|
74 | OutputIterator operator()(InputIterator first, InputIterator last, |
---|
75 | OutputIterator result) const |
---|
76 | { |
---|
77 | return std::transform(first, last, |
---|
78 | result, std::bind2nd(std::minus<double>(), |
---|
79 | func_(first, last))); |
---|
80 | } |
---|
81 | |
---|
82 | private: |
---|
83 | UnaryFunction func_; |
---|
84 | }; |
---|
85 | |
---|
86 | }}} // end of namespace normalization, yat and thep |
---|
87 | #endif |
---|