1 | #ifndef _theplu_yat_normalizer_centralizer_ |
---|
2 | #define _theplu_yat_normalizer_centralizer_ |
---|
3 | |
---|
4 | // $Id: Centralizer.h 1576 2008-10-14 07:29:21Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 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 | Creates a UnaryFunction using default constructor. |
---|
55 | */ |
---|
56 | Centralizer(void){} |
---|
57 | |
---|
58 | /** |
---|
59 | \param uf unary function defining the center. |
---|
60 | */ |
---|
61 | Centralizer(const UnaryFunction& uf) |
---|
62 | : func_(uf) {} |
---|
63 | |
---|
64 | /** |
---|
65 | Calculates the center \a c of the range [first, last) using |
---|
66 | UnaryFunction. This value, \a c, is then subtracted from each |
---|
67 | element in the range [first, last) and assigned to the |
---|
68 | corresponding element in range [result, result + (last-first) ). |
---|
69 | |
---|
70 | It is possible to centralize a range "in place"; it is |
---|
71 | permissible for the iterators \a first and \a result to be the |
---|
72 | same. \see std::transform |
---|
73 | |
---|
74 | \return result + (last-first) |
---|
75 | */ |
---|
76 | template<class InputIterator, class OutputIterator> |
---|
77 | OutputIterator operator()(InputIterator first, InputIterator last, |
---|
78 | OutputIterator result) const |
---|
79 | { |
---|
80 | typename utility::weighted_iterator_traits<InputIterator>::type tag; |
---|
81 | return normalize(first, last, result, tag); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | private: |
---|
86 | UnaryFunction func_; |
---|
87 | |
---|
88 | // unweighted version |
---|
89 | template<class InputIterator, class OutputIterator> |
---|
90 | OutputIterator normalize(InputIterator first, InputIterator last, |
---|
91 | OutputIterator result, |
---|
92 | utility::unweighted_iterator_tag tag) const |
---|
93 | { |
---|
94 | return std::transform(first, last, |
---|
95 | result, std::bind2nd(std::minus<double>(), |
---|
96 | func_(first, last))); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | // weighted version |
---|
101 | template<class InputIterator, class OutputIterator> |
---|
102 | OutputIterator normalize(InputIterator first, InputIterator last, |
---|
103 | OutputIterator result, |
---|
104 | utility::weighted_iterator_tag tag) const |
---|
105 | { |
---|
106 | std::copy(utility::weight_iterator(first), |
---|
107 | utility::weight_iterator(last), |
---|
108 | utility::weight_iterator(result)); |
---|
109 | std::transform(utility::data_iterator(first), |
---|
110 | utility::data_iterator(last), |
---|
111 | utility::data_iterator(result), |
---|
112 | std::bind2nd(std::minus<double>(), |
---|
113 | func_(first, last))); |
---|
114 | return result + std::distance(first, last); |
---|
115 | } |
---|
116 | |
---|
117 | }; |
---|
118 | |
---|
119 | }}} // end of namespace normalizer, yat and thep |
---|
120 | #endif |
---|