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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "yat/statistics/Average.h" |
---|
24 | |
---|
25 | #include <algorithm> |
---|
26 | #include <functional> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace normalization { |
---|
31 | |
---|
32 | /** |
---|
33 | \brief Centralize a range |
---|
34 | |
---|
35 | The center is calculated and then that value is subtracted from |
---|
36 | each element. By default the center is defined as the arithmetic |
---|
37 | mean, but this can be changed by providing a suitable |
---|
38 | UnaryFunction. |
---|
39 | |
---|
40 | \since New in yat 0.5 |
---|
41 | */ |
---|
42 | template<class UnaryFunction = statistics::Average> |
---|
43 | class Centralizer |
---|
44 | { |
---|
45 | public: |
---|
46 | /** |
---|
47 | \brief default constructor |
---|
48 | |
---|
49 | Creates a UnaryFunction using default constructor. |
---|
50 | */ |
---|
51 | Centralizer(void){} |
---|
52 | |
---|
53 | /** |
---|
54 | \param uf unary function defining the center. |
---|
55 | */ |
---|
56 | Centralizer(const UnaryFunction& uf) |
---|
57 | : func_(uf) {} |
---|
58 | |
---|
59 | /** |
---|
60 | Calculates the center \a c of the range [first, last) using |
---|
61 | UnaryFunction. This value, \a c, is then subtracted from each |
---|
62 | element in the range [first, last) and assigned to the |
---|
63 | corresponding element in range [result, result + (last-first) ). |
---|
64 | |
---|
65 | It is possible to centralize a range "in place"; it is |
---|
66 | permissible for the iterators \a first and \a result to be the |
---|
67 | same. \see std::transform |
---|
68 | |
---|
69 | \return result + (last-first) |
---|
70 | */ |
---|
71 | template<class InputIterator, class OutputIterator> |
---|
72 | OutputIterator operator()(InputIterator first, InputIterator last, |
---|
73 | OutputIterator result) const |
---|
74 | { |
---|
75 | return std::transform(first, last, |
---|
76 | result, std::bind2nd(std::minus<double>(), |
---|
77 | func_(first, last))); |
---|
78 | } |
---|
79 | |
---|
80 | private: |
---|
81 | UnaryFunction func_; |
---|
82 | }; |
---|
83 | |
---|
84 | }}} // end of namespace normalization, yat and thep |
---|
85 | #endif |
---|