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