source: branches/0.7-stable/yat/normalizer/Centralizer.h @ 2407

Last change on this file since 2407 was 2283, checked in by Peter, 12 years ago

fix Centralizer so it works with mixed iterators. Make it clear that result iterator must be a mutable forward iterator (output iterator does not work). refs #631

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1#ifndef _theplu_yat_normalizer_centralizer_
2#define _theplu_yat_normalizer_centralizer_
3
4// $Id: Centralizer.h 2283 2010-06-26 02:14:39Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "utility.h"
27
28#include "yat/statistics/Average.h"
29#include "yat/utility/DataIterator.h"
30#include "yat/utility/iterator_traits.h"
31#include "yat/utility/WeightIterator.h"
32
33#include <boost/concept_check.hpp>
34
35#include <algorithm>
36#include <functional>
37
38namespace theplu {
39namespace yat {
40namespace normalizer {
41
42  /**
43     \brief Centralize a range
44
45     The center is calculated and then that value is subtracted from
46     each element. By default the center is defined as the arithmetic
47     mean, but this can be changed by providing a suitable
48     UnaryFunction.
49
50     Type Requirements:
51     - InputIterator must be an \input_iterator
52     - ForwardIterator must be a mutable \forward_iterator
53
54     \since New in yat 0.5
55   */
56  template<class UnaryFunction = statistics::Average>
57  class Centralizer
58  {
59  public:
60    /**
61       \brief default constructor
62
63       Internal UnaryFunction is created using its default
64       constructor.
65     */
66    Centralizer(void){}
67
68    /**
69       \param uf unary function defining the center.
70     */
71    Centralizer(const UnaryFunction& uf)
72      : func_(uf) {}
73
74    /**
75       Calculates the center \a c of the range [first, last) using
76       UnaryFunction. This value, \a c, is then subtracted from each
77       element in the range [first, last) and assigned to the
78       corresponding element in range [result, result + (last-first) ).
79
80       It is possible to centralize a range "in place"; it is
81       permissible for the iterators \a first and \a result to be the
82       same.
83
84       \see std::transform
85     */
86    template<class InputIterator, class ForwardIterator>
87    void operator()(InputIterator first, InputIterator last,
88                    ForwardIterator result) const
89    {
90      BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
91      BOOST_CONCEPT_ASSERT((boost::Mutable_ForwardIterator<ForwardIterator>));
92      // copy weight if result is weighted
93      detail::copy_weight_if_weighted(first, last, result); 
94      std::transform(utility::data_iterator(first), 
95                     utility::data_iterator(last), 
96                     utility::data_iterator(result),
97                     std::bind2nd(std::minus<double>(),func_(first, last)));
98    }
99
100  private:
101    UnaryFunction func_;
102  };
103
104}}} // end of namespace normalizer, yat and thep
105#endif
Note: See TracBrowser for help on using the repository browser.