1 | #ifndef _theplu_yat_normalizer_centralizer_ |
---|
2 | #define _theplu_yat_normalizer_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 | #include "yat/utility/DataIterator.h" |
---|
25 | #include "yat/utility/iterator_traits.h" |
---|
26 | #include "yat/utility/WeightIterator.h" |
---|
27 | |
---|
28 | #include <algorithm> |
---|
29 | #include <functional> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace normalizer { |
---|
34 | |
---|
35 | /** |
---|
36 | \brief Centralize a range |
---|
37 | |
---|
38 | The center is calculated and then that value is subtracted from |
---|
39 | each element. By default the center is defined as the arithmetic |
---|
40 | mean, but this can be changed by providing a suitable |
---|
41 | UnaryFunction. |
---|
42 | |
---|
43 | \since New in yat 0.5 |
---|
44 | */ |
---|
45 | template<class UnaryFunction = statistics::Average> |
---|
46 | class Centralizer |
---|
47 | { |
---|
48 | public: |
---|
49 | /** |
---|
50 | \brief default constructor |
---|
51 | |
---|
52 | Creates a UnaryFunction using default constructor. |
---|
53 | */ |
---|
54 | Centralizer(void){} |
---|
55 | |
---|
56 | /** |
---|
57 | \param uf unary function defining the center. |
---|
58 | */ |
---|
59 | Centralizer(const UnaryFunction& uf) |
---|
60 | : func_(uf) {} |
---|
61 | |
---|
62 | /** |
---|
63 | Calculates the center \a c of the range [first, last) using |
---|
64 | UnaryFunction. This value, \a c, is then subtracted from each |
---|
65 | element in the range [first, last) and assigned to the |
---|
66 | corresponding element in range [result, result + (last-first) ). |
---|
67 | |
---|
68 | It is possible to centralize a range "in place"; it is |
---|
69 | permissible for the iterators \a first and \a result to be the |
---|
70 | same. \see std::transform |
---|
71 | |
---|
72 | \return result + (last-first) |
---|
73 | */ |
---|
74 | template<class InputIterator, class OutputIterator> |
---|
75 | OutputIterator operator()(InputIterator first, InputIterator last, |
---|
76 | OutputIterator result) const |
---|
77 | { |
---|
78 | typename utility::weighted_iterator_traits<InputIterator>::type tag; |
---|
79 | return normalize(first, last, result, tag); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | private: |
---|
84 | UnaryFunction func_; |
---|
85 | |
---|
86 | // unweighted version |
---|
87 | template<class InputIterator, class OutputIterator> |
---|
88 | OutputIterator normalize(InputIterator first, InputIterator last, |
---|
89 | OutputIterator result, |
---|
90 | utility::unweighted_iterator_tag tag) const |
---|
91 | { |
---|
92 | return std::transform(first, last, |
---|
93 | result, std::bind2nd(std::minus<double>(), |
---|
94 | func_(first, last))); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | // weighted version |
---|
99 | template<class InputIterator, class OutputIterator> |
---|
100 | OutputIterator normalize(InputIterator first, InputIterator last, |
---|
101 | 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>(), |
---|
111 | func_(first, last))); |
---|
112 | return result + std::distance(first, last); |
---|
113 | } |
---|
114 | |
---|
115 | }; |
---|
116 | |
---|
117 | }}} // end of namespace normalizer, yat and thep |
---|
118 | #endif |
---|